You are here

class FeedTypeTamperMeta in Feeds Tamper 8.2

Class for managing tamper plugins for a feed type.

Hierarchy

Expanded class hierarchy of FeedTypeTamperMeta

2 files declare their use of FeedTypeTamperMeta
FeedTypeTamperMetaTest.php in tests/src/Unit/FeedTypeTamperMetaTest.php
FeedTypeTamperMetaTest.php in tests/src/Kernel/FeedTypeTamperMetaTest.php

File

src/FeedTypeTamperMeta.php, line 14

Namespace

Drupal\feeds_tamper
View source
class FeedTypeTamperMeta implements FeedTypeTamperMetaInterface {

  /**
   * The Uuid generator.
   *
   * @var \Drupal\Component\Uuid\UuidInterface
   */
  protected $uuidGenerator;

  /**
   * The Tamper plugin manager.
   *
   * @var \Drupal\tamper\TamperManagerInterface
   */
  protected $tamperManager;

  /**
   * The feed type to manage tamper plugins for.
   *
   * @var \Drupal\feeds\Entity\FeedType
   */
  protected $feedType;

  /**
   * Holds the collection of tampers that are used by the feed type.
   *
   * @var \Drupal\feeds_tamper\TamperPluginCollection
   */
  protected $tamperCollection;

  /**
   * Constructs a new FeedTypeTamperMeta object.
   *
   * @param \Drupal\Component\Uuid\UuidInterface $uuid_generator
   *   The Uuid generator.
   * @param \Drupal\tamper\TamperManagerInterface $tamper_manager
   *   The Tamper plugin manager.
   * @param \Drupal\feeds\FeedTypeInterface $feed_type
   *   The feed type to manage tamper plugins for.
   */
  public function __construct(UuidInterface $uuid_generator, TamperManagerInterface $tamper_manager, FeedTypeInterface $feed_type) {
    $this->uuidGenerator = $uuid_generator;
    $this->tamperManager = $tamper_manager;
    $this->feedType = $feed_type;
  }

  /**
   * Creates a new instance.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The service container.
   * @param \Drupal\feeds\FeedTypeInterface $feed_type
   *   The feed type to manage tamper plugins for.
   *
   * @return static
   *   A new FeedTypeTamperMeta instance.
   */
  public static function create(ContainerInterface $container, FeedTypeInterface $feed_type) {
    return new static($container
      ->get('uuid'), $container
      ->get('plugin.manager.tamper'), $feed_type);
  }

  /**
   * {@inheritdoc}
   */
  public function getTamper($instance_id) {
    return $this
      ->getTampers()
      ->get($instance_id);
  }

  /**
   * {@inheritdoc}
   */
  public function getTampers() {
    if (!isset($this->tamperCollection)) {
      $tampers = $this->feedType
        ->getThirdPartySetting('feeds_tamper', 'tampers');
      $tampers = empty($tampers) ? [] : $tampers;
      $this->tamperCollection = new TamperPluginCollection($this->tamperManager, $this
        ->getSourceDefinition(), $tampers);
      $this->tamperCollection
        ->sort();
    }
    return $this->tamperCollection;
  }

  /**
   * {@inheritdoc}
   */
  public function getTampersGroupedBySource() {
    $grouped_tampers = [];
    $this
      ->getTampers()
      ->sort();
    foreach ($this
      ->getTampers() as $id => $tamper) {
      $grouped_tampers[(string) $tamper
        ->getSetting('source')][$id] = $tamper;
    }
    return $grouped_tampers;
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginCollections() {
    return [
      'tampers' => $this
        ->getTampers(),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function addTamper(array $configuration) {
    $configuration['uuid'] = $this->uuidGenerator
      ->generate();
    $configuration['source_definition'] = $this
      ->getSourceDefinition();
    $this
      ->getTampers()
      ->addInstanceId($configuration['uuid'], $configuration);
    $this
      ->updateFeedType();
    return $configuration['uuid'];
  }

  /**
   * {@inheritdoc}
   */
  public function setTamperConfig($instance_id, array $configuration) {
    $configuration['uuid'] = $instance_id;
    $this
      ->getTampers()
      ->setInstanceConfiguration($instance_id, $configuration);
    $this
      ->updateFeedType();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function removeTamper($instance_id) {
    $this
      ->getTampers()
      ->removeInstanceId($instance_id);
    $this
      ->updateFeedType();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function rectifyInstances() {

    // Check the difference between the tampers grouped by source and a list of
    // all sources used in mapping. By diffing we keep an array of tampers
    // belonging to a source that is no longer used in the mapping.
    $tampers_by_source_to_remove = array_diff_key($this
      ->getTampersGroupedBySource(), $this
      ->getUniqueSourceList());

    // Remove these tamper instances.
    foreach ($tampers_by_source_to_remove as $tampers) {
      foreach ($tampers as $uuid => $tamper) {
        $this
          ->removeTamper($uuid);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getUniqueSourceList() {

    // Extract used sources from mappings.
    $sources = [];
    foreach ($this->feedType
      ->getMappings() as $mapping) {
      foreach ($mapping['map'] as $column => $source) {
        if ($source == '') {
          continue;
        }
        $sources[$source] = $source;
      }
    }
    return $sources;
  }

  /**
   * {@inheritdoc}
   */
  public function getSourceDefinition() {
    $source_list = [];
    foreach ($this->feedType
      ->getMappingSources() as $key => $source) {
      $source_list[$key] = isset($source['label']) ? $source['label'] : $key;
    }
    return new SourceDefinition($source_list);
  }

  /**
   * Writes tampers back on the feed type.
   */
  protected function updateFeedType() {
    $this
      ->getTampers()
      ->sort();
    foreach ($this
      ->getPluginCollections() as $plugin_config_key => $plugin_collection) {
      $this->feedType
        ->setThirdPartySetting('feeds_tamper', $plugin_config_key, $plugin_collection
        ->getConfiguration());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedTypeTamperMeta::$feedType protected property The feed type to manage tamper plugins for.
FeedTypeTamperMeta::$tamperCollection protected property Holds the collection of tampers that are used by the feed type.
FeedTypeTamperMeta::$tamperManager protected property The Tamper plugin manager.
FeedTypeTamperMeta::$uuidGenerator protected property The Uuid generator.
FeedTypeTamperMeta::addTamper public function Adds a tamper plugin instance for this feed type. Overrides FeedTypeTamperMetaInterface::addTamper
FeedTypeTamperMeta::create public static function Creates a new instance.
FeedTypeTamperMeta::getPluginCollections public function Gets the plugin collections used by this object. Overrides ObjectWithPluginCollectionInterface::getPluginCollections
FeedTypeTamperMeta::getSourceDefinition public function Returns a definition of sources. Overrides FeedTypeTamperMetaInterface::getSourceDefinition
FeedTypeTamperMeta::getTamper public function Returns a specific Tamper plugin. Overrides FeedTypeTamperMetaInterface::getTamper
FeedTypeTamperMeta::getTampers public function Returns the tamper plugin instances for this feed type. Overrides FeedTypeTamperMetaInterface::getTampers
FeedTypeTamperMeta::getTampersGroupedBySource public function Returns the tamper plugin instances for this feed type, keyed by source. Overrides FeedTypeTamperMetaInterface::getTampersGroupedBySource
FeedTypeTamperMeta::getUniqueSourceList public function Returns an unique list of sources used in mappings. Overrides FeedTypeTamperMetaInterface::getUniqueSourceList
FeedTypeTamperMeta::rectifyInstances public function Removes tamper instances whose source was removed from the mapping. Overrides FeedTypeTamperMetaInterface::rectifyInstances
FeedTypeTamperMeta::removeTamper public function Removes a tamper plugin instance from this feed type. Overrides FeedTypeTamperMetaInterface::removeTamper
FeedTypeTamperMeta::setTamperConfig public function Sets the configuration for a tamper plugin instance. Overrides FeedTypeTamperMetaInterface::setTamperConfig
FeedTypeTamperMeta::updateFeedType protected function Writes tampers back on the feed type.
FeedTypeTamperMeta::__construct public function Constructs a new FeedTypeTamperMeta object.