Skip to content

Commit

Permalink
fixing: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijzer committed Dec 17, 2024
1 parent 6b86a52 commit f2f94dc
Show file tree
Hide file tree
Showing 31 changed files with 401 additions and 157 deletions.
4 changes: 2 additions & 2 deletions src/Component/Action/ActionItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Misery\Model\DataStructure\ItemInterface;

interface ActionItemInterface
interface ActionItemInterface extends ActionInterface
{
public function applyAsItem(ItemInterface $item): ItemInterface;
public function applyAsItem(ItemInterface $item): void;
}
27 changes: 23 additions & 4 deletions src/Component/Action/ConcatAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,40 @@
use Misery\Component\Common\Options\OptionsInterface;
use Misery\Component\Common\Options\OptionsTrait;
use Misery\Component\Common\Utils\ValueFormatter;
use Misery\Model\DataStructure\ItemInterface;

class ConcatAction implements OptionsInterface
class ConcatAction implements OptionsInterface, ActionItemInterface
{
use OptionsTrait;
private $repo;
private $prepReader;

public const NAME = 'concat';

/** @var array */
private $options = [
'key' => null,
'field' => null,
'format' => '%s',
];

public function applyAsItem(ItemInterface $item): void
{
$format = $this->getOption('format');
$field = $this->getOption('field', $this->getOption('key'));
if (null == $field) {
return;
}

$dataValues = [];
foreach (ValueFormatter::getKeys($format) as $key) {
$dataValues[$key] = $item->getItem($key)?->getDataValue();
}

$item->addItem(
$field,
ValueFormatter::format($format, $dataValues)
);
}

public function apply(array $item): array
{
$field = $this->getOption('field', $this->getOption('key'));
Expand All @@ -28,7 +47,7 @@ public function apply(array $item): array
}

// don't check if array_key_exist here, concat should always work, if the field doesn't exist
// somewhat the point to form a new field from concatination
// somewhat the point to form a new field from concatenation
$item[$field] = ValueFormatter::format($this->getOption('format'), $item);

return $item;
Expand Down
11 changes: 7 additions & 4 deletions src/Component/Action/CopyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Misery\Component\Converter\Matcher;
use Misery\Model\DataStructure\ItemInterface;

class CopyAction implements ActionInterface, OptionsInterface
class CopyAction implements OptionsInterface, ActionItemInterface
{
use OptionsTrait;

Expand All @@ -19,13 +19,16 @@ class CopyAction implements ActionInterface, OptionsInterface
'to' => null,
];

public function applyAsItem(ItemInterface $item): ItemInterface
public function applyAsItem(ItemInterface $item): void
{
$from = $this->getOption('from');
$to = $this->getOption('to');
$item->copyItem($from, $to);

return $item;
if (null === $from || null === $to) {
return;
}

$item->copyItem($from, $to);
}

public function apply(array $item): array
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Action/DebugAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DebugAction implements OptionsInterface, ActionInterface, ActionItemInterf
'until_field' => null,
];

#[NoReturn] public function applyAsItem(ItemInterface $item): ItemInterface
public function applyAsItem(ItemInterface $item): void
{
dd($item);
}
Expand Down
6 changes: 2 additions & 4 deletions src/Component/Action/ExpandAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ class ExpandAction implements OptionsInterface, ActionItemInterface
'list' => null,
];

public function applyAsItem(ItemInterface $item): ItemInterface
public function applyAsItem(ItemInterface $item): void
{
$list = $this->getOption('set', $this->getOption('list', []));
if (empty($list)) {
return $item;
return;
}

foreach ($list as $itemCode => $itemValue) {
$item->addItem($itemCode, $itemValue);
}

return $item;
}

public function apply(array $item): array
Expand Down
9 changes: 6 additions & 3 deletions src/Component/Action/ExtensionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ class ExtensionAction implements OptionsInterface, ConfigurationAwareInterface,
'extension' => null,
];

public function applyAsItem(ItemInterface $item): ItemInterface
public function applyAsItem(ItemInterface $item): void
{
$extension = $this->getOption('extension');
if (null === $extension) {
return $item;
return;
}

// loadExtension
if (null === $this->extension) {
$extensionFile = $this->configuration->getExtensions()[$extension.'.php'] ?? null;
$this->extension = $this->loadExtension($extensionFile, 'Extensions\\'.$extension);
}

return $this->extension->applyAsItem($item);
if (method_exists($this->extension, 'applyAsItem')) {
$this->extension->applyAsItem($item);
}
}

public function apply($item): array
Expand Down
17 changes: 17 additions & 0 deletions src/Component/Action/FirstValueAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Misery\Component\Common\Options\OptionsTrait;
use Misery\Component\Converter\Matcher;
use Misery\Model\DataStructure\ItemInterface;

class FirstValueAction implements ActionInterface
{
Expand All @@ -20,6 +21,22 @@ class FirstValueAction implements ActionInterface
'default_value' => null,
];

public function applyAsItem(ItemInterface $item): void
{
$defaultValue = $this->getOption('default_value');
$storeField = $this->getOption('store_field');
$fields = $this->getOption('fields');

foreach ($fields as $field) {
if (!empty($item->getItem($field)->getDataValue())) {
$item->copyItem($field, $storeField);
return;
}
}

$item->addItem($storeField, $defaultValue);
}

public function apply(array $item): array
{
$defaultValue = $this->getOption('default_value');
Expand Down
6 changes: 2 additions & 4 deletions src/Component/Action/FrameAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@ class FrameAction implements OptionsInterface, ActionItemInterface
'list' => [],
];

public function applyAsItem(ItemInterface $item): ItemInterface
public function applyAsItem(ItemInterface $item): void
{
$fields = $this->getOption('fields', $this->getOption('list'));
if (empty($fields)) {
return $item;
return;
}

// lets generate a multi-dimensional array
if (isset($fields[0])) {
$fields = array_fill_keys($fields, null);
}
$item->reFrame($fields);

return $item;
}

public function apply(array $item): array
Expand Down
17 changes: 16 additions & 1 deletion src/Component/Action/GroupAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
use Misery\Component\Common\Options\OptionsTrait;
use Misery\Component\Configurator\ConfigurationAwareInterface;
use Misery\Component\Configurator\ConfigurationTrait;
use Misery\Model\DataStructure\ItemInterface;

class GroupAction implements OptionsInterface, ConfigurationAwareInterface
class GroupAction implements OptionsInterface, ConfigurationAwareInterface, ActionInterface
{
use OptionsTrait;
use ConfigurationTrait;
Expand All @@ -20,6 +21,20 @@ class GroupAction implements OptionsInterface, ConfigurationAwareInterface
'actionProcessor' => null,
];

public function applyAsItem(ItemInterface $item): void
{
if ($this->getOption('name') && !$this->getOption('actionProcessor')) {
$this->setOption(
'actionProcessor',
$this->getConfiguration()->getGroupedActions($this->getOption('name'))
);
}

if ($this->getOption('actionProcessor')) {
$this->getOption('actionProcessor')->process($item);
}
}

public function apply(array $item): array
{
if ($this->getOption('name') && !$this->getOption('actionProcessor')) {
Expand Down
17 changes: 7 additions & 10 deletions src/Component/Action/ItemActionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@

class ItemActionProcessor
{
private array $configurationRules;

public function __construct(array $configurationRules)
{
$this->configurationRules = $configurationRules;
}
public function __construct(private readonly array $configurationRules) {}

public function process(ItemInterface|array $item): ItemInterface|array
{
foreach ($this->configurationRules as $action) {
if ($item instanceof ItemInterface) {
$item = $action->applyAsItem($item);
continue;
if ($item instanceof ItemInterface) {
foreach ($this->configurationRules as $action) {
$action->applyAsItem($item);
}
}

foreach ($this->configurationRules as $name => $action) {
$item = $action->apply($item);
}

Expand Down
8 changes: 1 addition & 7 deletions src/Component/Action/ItemActionProcessorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@
use Misery\Component\Common\Registry\RegistryInterface;
use Misery\Component\Configurator\Configuration;
use Misery\Component\Configurator\ConfigurationAwareInterface;
use Misery\Component\Configurator\ConfigurationManager;
use Misery\Component\Reader\ItemReaderAwareInterface;
use Misery\Component\Source\SourceCollection;

class ItemActionProcessorFactory implements RegisteredByNameInterface
{
private $registry;

public function __construct(RegistryInterface $registry)
{
$this->registry = $registry;
}
public function __construct(private readonly RegistryInterface $registry) {}

public function createActionProcessor(SourceCollection $sources, array $configuration): ItemActionProcessor
{
Expand Down
6 changes: 2 additions & 4 deletions src/Component/Action/KeyMapperAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct()
'reverse' => false,
];

public function applyAsItem(ItemInterface $item): ItemInterface
public function applyAsItem(ItemInterface $item): void
{
$reverse = $this->getOption('reverse');
$list = array_filter($this->getOption('list'));
Expand All @@ -44,16 +44,14 @@ public function applyAsItem(ItemInterface $item): ItemInterface
foreach ($keys as $keyToUnset) {
$item->removeItem($keyToUnset);
}
return $item;
return;
}

foreach ($list as $match => $replacer) {
if ($item->hasItem($match)) {
$item->moveItem($match, $replacer);
}
}

return $item;
}

public function apply(array $item): array
Expand Down
10 changes: 4 additions & 6 deletions src/Component/Action/ListMapperAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Misery\Component\Common\Options\OptionsTrait;
use Misery\Model\DataStructure\ItemInterface;

class ListMapperAction implements ActionInterface, OptionsInterface, ActionItemInterface
class ListMapperAction implements OptionsInterface, ActionItemInterface
{
use OptionsTrait;

Expand All @@ -19,14 +19,14 @@ class ListMapperAction implements ActionInterface, OptionsInterface, ActionItemI
'list' => [],
];

public function applyAsItem(ItemInterface $item): ItemInterface
public function applyAsItem(ItemInterface $item): void
{
if ([] === $this->getOption('list')) {
return $item;
return;
}

if (null === $this->getOption('field')) {
return $item;
return;
}
$field = $this->getOption('field');
$storeField = $this->getOption('store_field');
Expand All @@ -41,8 +41,6 @@ public function applyAsItem(ItemInterface $item): ItemInterface
$item->editItemValue($field, $newValue);
}
}

return $item;
}

public function apply(array $item): array
Expand Down
4 changes: 2 additions & 2 deletions src/Component/Action/MakeItemAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Misery\Component\Action;

use Misery\Component\Akeneo\DataStructure\AkeneoItemFactory;
use Misery\Component\Akeneo\DataStructure\AkeneoItemBuilder;
use Misery\Component\Common\Options\OptionsInterface;
use Misery\Component\Common\Options\OptionsTrait;
use Misery\Component\Configurator\ConfigurationAwareInterface;
Expand Down Expand Up @@ -50,6 +50,6 @@ public function apply(array $item): ItemInterface
$attributeTypes = $this->configuration->getList($attributeTypes);
}

return AkeneoItemFactory::create($item, ['attribute_types' => $attributeTypes]);
return AkeneoItemBuilder::fromProductApiPayload($item, ['attribute_types' => $attributeTypes]);
}
}
6 changes: 2 additions & 4 deletions src/Component/Action/RemoveAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Misery\Component\Common\Options\OptionsTrait;
use Misery\Model\DataStructure\ItemInterface;

class RemoveAction implements ActionInterface, OptionsInterface, ActionItemInterface
class RemoveAction implements OptionsInterface, ActionItemInterface
{
use OptionsTrait;

Expand All @@ -18,14 +18,12 @@ class RemoveAction implements ActionInterface, OptionsInterface, ActionItemInter
'fields' => [],
];

public function applyAsItem(ItemInterface $item): ItemInterface
public function applyAsItem(ItemInterface $item): void
{
$fields = $this->getOption('keys', $this->getOption('fields'));
foreach ($fields as $field) {
$item->removeItem($field);
}

return $item;
}

public function apply(array $item): array
Expand Down
Loading

0 comments on commit f2f94dc

Please sign in to comment.