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

Issue #2939688 by bruno.bicudo, MegaChriz: Fixed coding standards in EntityProcessorBase class.

parent 649ac0ac
Loading
Loading
Loading
Loading
+73 −13
Original line number Diff line number Diff line
@@ -2,8 +2,11 @@

namespace Drupal\feeds\Feeds\Processor;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
@@ -13,6 +16,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\feeds\Entity\FeedType;
use Drupal\feeds\Event\FeedsEvents;
use Drupal\feeds\Exception\EmptyFeedException;
@@ -31,6 +35,7 @@ use Drupal\feeds\StateInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\user\EntityOwnerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
@@ -82,6 +87,41 @@ abstract class EntityProcessorBase extends ProcessorBase implements EntityProces
   */
  protected $languageManager;

  /**
   * The datetime interface for getting the system time.
   *
   * @var Drupal\Component\Datetime\TimeInterface
   */
  protected $dateTime;

  /**
   * The action plugin manager.
   *
   * @var Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $actionManager;

  /**
   * The renderer service.
   *
   * @var Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The logger for feeds channel.
   *
   * @var Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The database service.
   *
   * @var Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Constructs an EntityProcessorBase object.
   *
@@ -97,13 +137,28 @@ abstract class EntityProcessorBase extends ProcessorBase implements EntityProces
   *   The entity type bundle info.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Component\Datetime\TimeInterface $date_time
   *   The datetime service for getting the system time.
   * @param \Drupal\Component\Plugin\PluginManagerInterface $action_manager
   *   The action plugin manager.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger for the feeds channel.
   * @param \Drupal\Core\Database\Connection $database
   *   The database service.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, LanguageManagerInterface $language_manager) {
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, LanguageManagerInterface $language_manager, TimeInterface $date_time, PluginManagerInterface $action_manager, RendererInterface $renderer, LoggerInterface $logger, Connection $database) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityType = $entity_type_manager->getDefinition($plugin_definition['entity_type']);
    $this->storageController = $entity_type_manager->getStorage($plugin_definition['entity_type']);
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
    $this->languageManager = $language_manager;
    $this->dateTime = $date_time;
    $this->actionManager = $action_manager;
    $this->renderer = $renderer;
    $this->logger = $logger;
    $this->database = $database;

    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }
@@ -118,7 +173,12 @@ abstract class EntityProcessorBase extends ProcessorBase implements EntityProces
      $plugin_definition,
      $container->get('entity_type.manager'),
      $container->get('entity_type.bundle.info'),
      $container->get('language_manager')
      $container->get('language_manager'),
      $container->get('datetime.time'),
      $container->get('plugin.manager.action'),
      $container->get('renderer'),
      $container->get('logger.factory')->get('feeds'),
      $container->get('database'),
    );
  }

@@ -187,7 +247,7 @@ abstract class EntityProcessorBase extends ProcessorBase implements EntityProces
      // This will throw an exception on failure.
      $this->entitySaveAccess($entity);
      // Set imported time.
      $entity->get('feeds_item')->imported = \Drupal::service('datetime.time')->getRequestTime();
      $entity->get('feeds_item')->imported = $this->dateTime->getRequestTime();

      // And... Save! We made it.
      $this->storageController->save($entity);
@@ -268,12 +328,12 @@ abstract class EntityProcessorBase extends ProcessorBase implements EntityProces
      default:
        try {
          // Apply action on entity.
          \Drupal::service('plugin.manager.action')
          $this->actionManager
            ->createInstance($update_non_existent)
            ->execute($entity);
        }
        catch (PluginNotFoundException $e) {
          $state->setMessage(t('Cleaning %entity failed because of non-existing action plugin %name.', [
          $state->setMessage($this->t('Cleaning %entity failed because of non-existing action plugin %name.', [
            '%entity' => $entity->label(),
            '%name' => $update_non_existent,
          ]), 'error');
@@ -587,7 +647,7 @@ abstract class EntityProcessorBase extends ProcessorBase implements EntityProces
      '@entity' => mb_strtolower($this->entityTypeLabel()),
      '%label' => $label,
      '%guid' => $guid,
      '@errors' => \Drupal::service('renderer')->renderRoot($element),
      '@errors' => $this->renderer->renderRoot($element),
      ':url' => $this->url('entity.feeds_feed_type.mapping', ['feeds_feed_type' => $this->feedType->id()]),
    ];
    if ($label || $label === '0') {
@@ -605,7 +665,7 @@ abstract class EntityProcessorBase extends ProcessorBase implements EntityProces
    $message_element = [
      '#markup' => implode("\n", $messages),
    ];
    $message = \Drupal::service('renderer')->renderRoot($message_element);
    $message = $this->renderer->renderRoot($message_element);

    throw new ValidationException($message);
  }
@@ -813,7 +873,7 @@ abstract class EntityProcessorBase extends ProcessorBase implements EntityProces
    if ($time == static::EXPIRE_NEVER) {
      return;
    }
    $expire_time = \Drupal::service('datetime.time')->getRequestTime() - $time;
    $expire_time = $this->dateTime->getRequestTime() - $time;
    return $this->entityTypeManager
      ->getStorage($this->entityType())
      ->getQuery()
@@ -1142,8 +1202,8 @@ abstract class EntityProcessorBase extends ProcessorBase implements EntityProces
  /**
   * {@inheritdoc}
   *
   * @todo Sort this out so that we aren't calling \Drupal::database()->delete()
   * here.
   * @todo Avoid using the database service. Find an other way to clean up
   * references to feeds that are being removed.
   */
  public function onFeedDeleteMultiple(array $feeds) {
    $fids = [];
@@ -1151,7 +1211,7 @@ abstract class EntityProcessorBase extends ProcessorBase implements EntityProces
      $fids[] = $feed->id();
    }
    $table = $this->entityType() . '__feeds_item';
    \Drupal::database()->delete($table)
    $this->database->delete($table)
      ->condition('feeds_item_target_id', $fids, 'IN')
      ->execute();
  }
@@ -1179,7 +1239,7 @@ abstract class EntityProcessorBase extends ProcessorBase implements EntityProces

      default:
        try {
          $definition = \Drupal::service('plugin.manager.action')->getDefinition($this->getConfiguration('update_non_existent'));
          $definition = $this->actionManager->getDefinition($this->getConfiguration('update_non_existent'));
          if (isset($definition['provider'])) {
            $this->addDependency('module', $definition['provider']);
          }
@@ -1187,7 +1247,7 @@ abstract class EntityProcessorBase extends ProcessorBase implements EntityProces
        catch (PluginNotFoundException $e) {
          // It's possible that the selected action plugin no longer exists. Log
          // an error about it.
          \Drupal::logger('feeds')->warning('The selected option for the setting "Previously imported items" in the feed type %feed_type_id no longer exists. Please edit the feed type and select a different option for that setting.', [
          $this->logger->warning('The selected option for the setting "Previously imported items" in the feed type %feed_type_id no longer exists. Please edit the feed type and select a different option for that setting.', [
            '%feed_type_id' => $this->feedType->id(),
          ]);
        }
+5 −0
Original line number Diff line number Diff line
@@ -91,6 +91,11 @@ class EntityProcessorBaseTest extends FeedsKernelTestBase {
      \Drupal::service('entity_type.manager'),
      \Drupal::service('entity_type.bundle.info'),
      \Drupal::service('language_manager'),
      \Drupal::service('datetime.time'),
      \Drupal::service('plugin.manager.action'),
      \Drupal::service('renderer'),
      \Drupal::service('logger.factory')->get('feeds'),
      \Drupal::service('database'),
    ]);

    $this->feed = $this->createMock(FeedInterface::class);