FeedTypeTamperMeta.php in Feeds Tamper 8.2
File
src/FeedTypeTamperMeta.php
View source
<?php
namespace Drupal\feeds_tamper;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\feeds\FeedTypeInterface;
use Drupal\tamper\SourceDefinition;
use Drupal\tamper\TamperManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FeedTypeTamperMeta implements FeedTypeTamperMetaInterface {
protected $uuidGenerator;
protected $tamperManager;
protected $feedType;
protected $tamperCollection;
public function __construct(UuidInterface $uuid_generator, TamperManagerInterface $tamper_manager, FeedTypeInterface $feed_type) {
$this->uuidGenerator = $uuid_generator;
$this->tamperManager = $tamper_manager;
$this->feedType = $feed_type;
}
public static function create(ContainerInterface $container, FeedTypeInterface $feed_type) {
return new static($container
->get('uuid'), $container
->get('plugin.manager.tamper'), $feed_type);
}
public function getTamper($instance_id) {
return $this
->getTampers()
->get($instance_id);
}
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;
}
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;
}
public function getPluginCollections() {
return [
'tampers' => $this
->getTampers(),
];
}
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'];
}
public function setTamperConfig($instance_id, array $configuration) {
$configuration['uuid'] = $instance_id;
$this
->getTampers()
->setInstanceConfiguration($instance_id, $configuration);
$this
->updateFeedType();
return $this;
}
public function removeTamper($instance_id) {
$this
->getTampers()
->removeInstanceId($instance_id);
$this
->updateFeedType();
return $this;
}
public function rectifyInstances() {
$tampers_by_source_to_remove = array_diff_key($this
->getTampersGroupedBySource(), $this
->getUniqueSourceList());
foreach ($tampers_by_source_to_remove as $tampers) {
foreach ($tampers as $uuid => $tamper) {
$this
->removeTamper($uuid);
}
}
}
public function getUniqueSourceList() {
$sources = [];
foreach ($this->feedType
->getMappings() as $mapping) {
foreach ($mapping['map'] as $column => $source) {
if ($source == '') {
continue;
}
$sources[$source] = $source;
}
}
return $sources;
}
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);
}
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());
}
}
}