Use dependency injection when possible

Created on 15 May 2023, about 1 year ago
Updated 24 May 2023, about 1 year ago

Problem/Motivation

Use dependency injection instead of the \Drupal::service();

1. AutoWebhook.php

\Drupal::request()->getHost() 
\Drupal::messenger()->addError(t('Could not enable webhook for localhost'));
\Drupal::configFactory();
\Drupal::logger('RazorpayAutoWebhook')->info('Updating razorpay webhook');
\Drupal::messenger()->addError(t('RazorpayAutoWebhook: ' . $exception->getMessage()));
\Drupal::logger('RazorpayAutoWebhook')->error($exception->getMessage());

2. RazorpayForm.php

 \Drupal::logger('RazorpayCheckoutForm')->error($exception->getMessage());
 \Drupal::messenger();
 Drupal::routeMatch()->getParameter('commerce_order')->id();
 \Drupal::configFactory();
 \Drupal::logger('RazorpayAutoWebhook')->error($exception->getMessage());

3. RazorpayCheckout.php

\Drupal::logger('RazorpayCapturePayment')->error($exception->getMessage());
\Drupal::config('drupal_commerce_razorpay.settings');
 \Drupal::entityTypeManager()->getStorage('commerce_order');

4. IPNController.php
\Drupal::service('plugin.manager.commerce_payment_gateway')->createInstance($plugin_id);

Use dependency injection for all above services.

Steps to reproduce

Proposed resolution

How to dependency injection example.

 \Drupal::entityTypeManager()->getStorage('commerce_order');

Replace:

add class.

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;


Define variable.

 /**
   * Entity manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

Define Constructor class

/**
   * Class Constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity Type Manager.
**/


 public function __construct(
    EntityTypeManagerInterface $entity_type_manager,
    ) {
    $this->entityTypeManager = $entity_type_manager; 
    
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    // Instantiates this form class.
    return new static(
      // Load the service required to construct this class.
      $container->get('entity_type.manager'),
     
    );
  }


$this->entityTypeManager->getStorage('commerce_order');

Remaining tasks

User interface changes

API changes

Data model changes

📌 Task
Status

Needs review

Version

1.0

Component

Code

Created by

🇮🇳India shashank5563 New Delhi

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @shashank5563
  • Assigned to shashank5563
  • Assigned to Nishant
  • 🇮🇳India Nishant

    I am working on this.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    Actually, this issue is about dependency injection, not coding standards.

  • @nishant opened merge request.
  • Status changed to Needs review about 1 year ago
  • Issue was unassigned.
  • Status changed to Needs work about 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
    +   /**
    +   * Config Factory.
    +   *
    +   * @var \Drupal\Core\Config\ConfigFactory
    +   */
    +  protected $configFactory;
    +
    +  /**
    +   * Messenger.
    +   *
    +   * @var \Drupal\Core\Messenger\Messenger
    +   */
    +  protected $messenger;
    +
    +  /**
    +   * Logger.
    +   *
    +   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
    +   */
    +  protected $loggerFactory;

    The descriptions are each missing a definite article.

    +   * Class Constructor.
    +   * @param \Drupal\Core\Config\ConfigFactory $configFactory
    +   *   Config Factory.
    +   * @param \Drupal\Core\Messenger\Messenger $messenger
    +   *   Messenger. 
    +   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
    +   *   Logger.
    +   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
    +   *   The request stack.

    The description for a constructor must start with Constructs a new followed by the class name (including its namespace), and end with object.
    There must be an empty line between the short description and the list of parameters.

    +  public function __construct(
    +    ConfigFactory $configFactory,  
    +    Messenger $messenger,
    +    LoggerChannelFactoryInterface $loggerFactory,
    +    RequestStack $request_stack
    +    ) {
    +    $this->configFactory = $configFactory;
    +    $this->messenger = $messenger;
    +    $this->loggerFactory = $loggerFactory;
    +    $this->requestStack = $request_stack;
    +  }

    Method declarations are written on a single line.

    +  public static function create(ContainerInterface $container) {
    +        return new static(
    +          $container->get('config.factory'),
    +          $container->get('messenger'),
    +          $container->get('logger.factory'),
    +          $container->get('request_stack')
    +        );
    +      }

    As per Drupal coding standards, indentation is two spaces.

  • Assigned to Nishant
  • Issue was unassigned.
  • Status changed to Needs review about 1 year ago
  • 🇮🇳India Nishant

    @apaderno,

    Pushed the latest changes as per your comment, Also I have run phpcbf commands to fix some automatic changes. Please review.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Status changed to Needs work about 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
    +  /**
    +   * The config factory service.
    +   *
    +   * @var \Drupal\Core\Config\ConfigFactory
    +   */
    +  protected $configFactory;
    +
    +  /**
    +   * The messenger service.
    +   *
    +   * @var \Drupal\Core\Messenger\Messenger
    +   */
    +  protected $messenger;

    In both the cases, there is no need to say it is a service.

    +  /**
    +   * A logger instance.
    +   *
    +   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
    +   */
    +  protected $loggerFactory;
    +

    It is sufficient to say The logger.

    +  /**
    +   * Constructs a new AutoWebhook object.
    +   *
    +   * @param \Drupal\Core\Config\ConfigFactory $configFactory
    +   *   Config Factory.
    +   * @param \Drupal\Core\Messenger\Messenger $messenger
    +   *   Messenger.
    +   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
    +   *   Logger.

    The class name is missing the namespace.
    The parameter descriptions are each missing a definite article.

    +  protected $supportedWebhookEvents = [
    +        'payment.authorized' => TRUE,
    +        'payment.failed'     => TRUE,
    +        'refund.created'     => TRUE
         ];

    The indentation is not correct. Anyway, since this issue is for using dependency injection where possible, that change is off-topic.

    +  protected function generateWebhookSecret() {
    +    $alphanumericString = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-=~!@#$%^&*()_+,./<>?;:[]{}|abcdefghijklmnopqrstuvwxyz';
     
    -        return substr(str_shuffle($alphanumericString), 0, 20);
    -    }
    +    return substr(str_shuffle($alphanumericString), 0, 20);
    +  }

    This change is not replacing calls to \Drupal methods, and it is off-topic.

    +  /**
    +   * Constructs a new RazorpayCheckout object.

    The class namespace is missing.

    +    $form['display_label']['#prefix'] = 'First <a href="https://easy.razorpay.com/onboarding?recommended_product=payment_gateway&source=drupal" target="_blank">signup</a> for a Razorpay account or
    +            <a href="https://dashboard.razorpay.com/signin?screen=sign_in&source=drupal" target="_blank">login</a> if you have an existing account.</p>';

    Strings shown in the user interface must be translatable.

    +  /**
    +   * {@inheritdoc}
    +   */
    +  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    +    parent::validateConfigurationForm($form, $form_state);
    +
    +    if ($form_state->getErrors()) {
    +      return;
    +  public function onReturn(OrderInterface $order, Request $request) {
    +    $keyId = $this->configuration['key_id'];
    +    $keySecret = $this->configuration['key_secret'];
    +    $api = new Api($keyId, $keySecret);
    +
    +    // Validate Rzp signature.
    +    try {
    +      $attributes = [
    +            'razorpay_order_id' => $request->get('razorpay_order_id'),
    +            'razorpay_payment_id' => $request->get('razorpay_payment_id'),
    +            'razorpay_signature' => $request->get('razorpay_signature')

    None of those lines have been changed to avoid calls to \Drupal methods.

    +    if (substr($values['key_id'], 0, 8) !== 'rzp_' . $values['mode']) {
    +      $this->messenger()->addError($this->t('Invalid Key ID or Key Secret for ' . $values['mode'] . ' mode.'));
    +      $form_state->setError($form['mode']);

    Translatable strings must use placeholders.

  • First commit to issue fork.
  • Status changed to Needs review about 1 year ago
  • 🇮🇳India sakthi_dev

    Please review.

  • Status changed to Needs work about 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    Also, the MR changes three files, but the issue summary says four files should be changed.

  • 🇮🇳India Nishant

    Hi @apaderno,

    I have fixed all the dependency injection related issues raised by you.
    Please review.

    Thanks

  • Status changed to Needs review about 1 year ago
  • Status changed to Needs work about 1 year ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • Status changed to Needs review about 1 year ago
Production build 0.69.0 2024