From 58eb4e77cab6dc20a69249045e421043b87da74c Mon Sep 17 00:00:00 2001 From: Thijs De Paepe Date: Mon, 10 Jun 2024 18:28:04 +0200 Subject: [PATCH] added: new skip with uniqueness option, more dynamic context --- src/Component/Action/SkipAction.php | 29 ++++++++++++++----- src/Component/Akeneo/Client/ApiWriter.php | 3 +- src/Component/Common/Pipeline/Pipeline.php | 2 +- .../Configurator/ConfigurationManager.php | 26 ++++++++++++----- src/Component/Logger/OutputLogger.php | 2 +- 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/Component/Action/SkipAction.php b/src/Component/Action/SkipAction.php index 01756dee..13d17f4c 100644 --- a/src/Component/Action/SkipAction.php +++ b/src/Component/Action/SkipAction.php @@ -15,15 +15,16 @@ class SkipAction implements OptionsInterface, ActionInterface /** @var array */ private $options = [ 'field' => null, - 'state' => 'EMPTY', + 'state' => ['EMPTY'], 'skip_message' => '' ]; + private array $values = []; public function apply(array $item): array { $field = $this->getOption('field'); $state = $this->getOption('state'); - $message = $this->getOption('skip_message', ''); + $states = is_string($state) ? [$state]: $state; $message = $this->getOption('skip_message', ''); $forceSkip = $this->getOption('force_skip', false); @@ -38,14 +39,28 @@ public function apply(array $item): array $value = $item[$field] ?? null; } - if (empty($value) && $state === 'EMPTY') { - throw new SkipPipeLineException($message); - } + foreach ($states as $state) { + if ($state === 'EMPTY' && empty($value)) { + throw new SkipPipeLineException($message); + } - if ($value === $state) { - throw new SkipPipeLineException($message); + if ($state === 'UNIQUE') { + if (in_array($value, $this->values)) { + throw new SkipPipeLineException($message); + } + $this->values[] = $value; + } + + if ($value === $state) { + throw new SkipPipeLineException($message); + } } return $item; } + + public function __destruct() + { + $this->values = []; + } } \ No newline at end of file diff --git a/src/Component/Akeneo/Client/ApiWriter.php b/src/Component/Akeneo/Client/ApiWriter.php index 19b061b0..47b1d7b6 100644 --- a/src/Component/Akeneo/Client/ApiWriter.php +++ b/src/Component/Akeneo/Client/ApiWriter.php @@ -9,6 +9,7 @@ use Misery\Component\Common\Pipeline\Exception\InvalidItemException; use Misery\Component\Common\Processor\BatchSizeProcessor; use Misery\Component\Writer\ItemWriterInterface; +use mysql_xdevapi\XSession; class ApiWriter implements ItemWriterInterface { @@ -69,7 +70,7 @@ private function doWrite(array $data) $response = $this->execute($data); } catch (\RuntimeException $e) { // do nothing - throw new InvalidItemException('API Runtime exception', [], $data); + throw new InvalidItemException('API Runtime exception', ['message' => 'Unexpected Runtime exception thrown'], $data); } catch (UnauthorizedException $e) { $this->client->refreshToken(); $response = $this->execute($data); diff --git a/src/Component/Common/Pipeline/Pipeline.php b/src/Component/Common/Pipeline/Pipeline.php index b4fb4bfb..237b743c 100644 --- a/src/Component/Common/Pipeline/Pipeline.php +++ b/src/Component/Common/Pipeline/Pipeline.php @@ -86,7 +86,7 @@ public function run(int $amount = -1, int $lineNumber = -1) 'msg' => $exception->getMessage(), 'item' => json_encode($exception->getInvalidItem()), ]); - $this->logger->error($exception->getMessage()); + $this->logger->error($exception->getMessage(), $exception->getInvalidItem()); continue; } if ($i === $lineNumber) { diff --git a/src/Component/Configurator/ConfigurationManager.php b/src/Component/Configurator/ConfigurationManager.php index a59fb754..50132f2f 100644 --- a/src/Component/Configurator/ConfigurationManager.php +++ b/src/Component/Configurator/ConfigurationManager.php @@ -130,16 +130,28 @@ public function addTransformationSteps(array $transformationSteps, array $master # list of transformations foreach ($transformationSteps as $transformationFile) { - # this code detect the run | with option - # and creates virtual steps, so you don't need to repeat yourself + // this code detects the run | with option + // and creates virtual steps, so you don't need to repeat yourself if (isset($transformationFile['run'])) { $file = $transformationFile['run']; - $keys = array_keys($transformationFile['with']); - foreach ($transformationFile['with'][$key = current($keys)] as $i => $context) { - // make virtual steps + $withArray = $transformationFile['with']; - // @TODO make a context from all keys and pass it through - $masterConfiguration['context'][$key] = $context; + // Get the number of iterations needed + $iterationCount = count(current($withArray)); + + // Iterate over each index + for ($i = 0; $i < $iterationCount; $i++) { + $context = []; + + // Build the context for the current index + foreach ($withArray as $key => $values) { + if (isset($values[$i])) { + $context[$key] = $values[$i]; + + // Save the context in the master configuration + $masterConfiguration['context'][$key] = $values[$i]; + } + } $this->addTransformationSteps([$file], $masterConfiguration); } diff --git a/src/Component/Logger/OutputLogger.php b/src/Component/Logger/OutputLogger.php index 97280acd..4aa165a1 100644 --- a/src/Component/Logger/OutputLogger.php +++ b/src/Component/Logger/OutputLogger.php @@ -48,6 +48,6 @@ public function debug($message, array $context = []): void public function log($level, $message, array $context = []): void { - echo $message . PHP_EOL; + echo $message . ' : ' . json_encode($context). PHP_EOL; } } \ No newline at end of file