Commit 41a17e33 authored by Bruno Branco Bicudo's avatar Bruno Branco Bicudo Committed by Youri van Koppen
Browse files

Issue #3248925 by MegaChriz, bruno.bicudo, aldairsoares: Use dependency...

Issue #3248925 by MegaChriz, bruno.bicudo, aldairsoares: Use dependency injection in Feeds test modules.
parent da6bc22c
Loading
Loading
Loading
Loading
+43 −3
Original line number Diff line number Diff line
@@ -3,7 +3,10 @@
namespace Drupal\feeds_test_files\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;

/**
@@ -20,6 +23,43 @@ class CsvController extends ControllerBase {
   */
  const DATE_RFC7231 = 'D, d M Y H:i:s \G\M\T';

  /**
   * The state handler service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The module extension list service.
   *
   * @var Drupal\Core\Extension\ModuleExtensionList|null
   */
  protected $extensionList;

  /**
   * Constructs a CsvController object.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The state handler service.
   * @param Drupal\Core\Extension\ModuleExtensionList $extensionList
   *   The module extension list service.
   */
  public function __construct(StateInterface $state, ModuleExtensionList $extensionList = NULL) {
    $this->state = $state;
    $this->extensionList = $extensionList;
  }

  /**
   * {@inheritDoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('state'),
      $container->has('extension.list.module') ? $container->get('extension.list.module') : NULL,
    );
  }

  /**
   * Generates an absolute url to the resources folder.
   *
@@ -77,7 +117,7 @@ class CsvController extends ControllerBase {
   *   A HTTP response.
   */
  public function nodes() {
    $last_modified = \Drupal::state()->get('feeds_test_nodes_last_modified');
    $last_modified = $this->state->get('feeds_test_nodes_last_modified');
    if (!$last_modified) {
      $file = 'nodes.csv';
      $last_modified = strtotime('Sun, 19 Nov 1978 05:00:00 GMT');
@@ -127,11 +167,11 @@ class CsvController extends ControllerBase {
   */
  protected function getModulePath(string $module_name): string {
    // @todo Remove drupal_get_path() when Drupal 9.2 is no longer supported.
    if (!\Drupal::hasService('extension.list.module')) {
    if ($this->extensionList == NULL) {
      return drupal_get_path('module', $module_name);
    }

    return \Drupal::service('extension.list.module')->getPath($module_name);
    return $this->extensionList->getPath($module_name);
  }

}
+1 −0
Original line number Diff line number Diff line
services:
  feeds_test_multiple_cron_runs.feed_subscriber:
    class: Drupal\feeds_test_multiple_cron_runs\EventSubscriber\FeedSubscriber
    arguments: ['@config.factory']
    tags:
      - {name: event_subscriber}
+19 −1
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\feeds_test_multiple_cron_runs\EventSubscriber;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\feeds\Event\FeedsEvents;
use Drupal\feeds\Event\ProcessEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -11,6 +12,23 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 */
class FeedSubscriber implements EventSubscriberInterface {

  /**
   * The settings of the 'Feeds test multiple cron runs' module.
   *
   * @var Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * Constructs a new FeedSubscriber object.
   *
   * @param Drupal\Core\Config\ConfigFactoryInterface $config
   *   The configuration factory.
   */
  public function __construct(ConfigFactoryInterface $config) {
    $this->config = $config->get('feeds_test_multiple_cron_runs.settings');
  }

  /**
   * {@inheritdoc}
   */
@@ -29,7 +47,7 @@ class FeedSubscriber implements EventSubscriberInterface {
    static $processed = 0;
    $processed++;

    $limit = \Drupal::config('feeds_test_multiple_cron_runs.settings')->get('import_queue_time');
    $limit = $this->config->get('import_queue_time');
    if ($processed == $limit) {
      sleep($limit);
    }