Skip to content

Commit

Permalink
added: new skip with uniqueness option, more dynamic context
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijzer committed Jun 10, 2024
1 parent 07ac2ac commit 58eb4e7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
29 changes: 22 additions & 7 deletions src/Component/Action/SkipAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = [];
}
}
3 changes: 2 additions & 1 deletion src/Component/Akeneo/Client/ApiWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Common/Pipeline/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 19 additions & 7 deletions src/Component/Configurator/ConfigurationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Logger/OutputLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 58eb4e7

Please sign in to comment.