Skip to content

added: item-tracker implementation #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Component/Akeneo/Client/ApiWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ private function doWrite(array $data)
'API exception',
[
'message' => $response->getContent(),
'errors' => AkeneoErrorFactory::createErrors($response->getContent())
'errors' => AkeneoErrorFactory::createErrors($response->getContent()),
'identifier' => $data['identifier'] ?? $data['code'] ?? $data['sku'] ?? $data['id'] ?? '',
'identityClass' => $this->endpoint::NAME,
'method' => $this->method,
],
$data
);
Expand Down
34 changes: 27 additions & 7 deletions src/Component/Common/Pipeline/Exception/InvalidItemException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,48 @@

class InvalidItemException extends \Exception
{
private $invalidItem;
private $invalidItemData;
private ?AkeneoErrors $errors;
private $item;
private mixed $identifier;
private ?string $identityClass;
private ?string $method;

public function __construct(string $message = null, array $invalidItem, array $item = [])
public function __construct(string $message = null, array $invalidItemData, array $item = [])
{
$this->invalidItem = $invalidItem;
$this->errors = $this->invalidItem['errors'] ?? null;
unset($this->invalidItem['errors']);
$this->invalidItemData = $invalidItemData;

$this->errors = $this->invalidItemData['errors'] ?? null;
$this->identifier = $this->invalidItemData['identifier'] ?? null;
$this->identityClass = $this->invalidItemData['identityClass'] ?? null;
$this->method = $this->invalidItemData['method'] ?? null;
unset($this->invalidItemData['errors']);
unset($this->invalidItemData['identifier']);
unset($this->invalidItemData['identityClass']);
unset($this->invalidItemData['method']);

parent::__construct($message);
$this->item = $item;
}

public function getInvalidIdentifier(): mixed
{
return $this->identifier;
}

public function getInvalidIdentityClass(): ?string
{
return $this->identityClass;
}

public function getItem(): array
{
return $this->item;
}

public function getInvalidItem(): array
public function getInvalidItemData(): array
{
return $this->invalidItem;
return $this->invalidItemData;
}

public function hasErrors(): bool
Expand Down
41 changes: 41 additions & 0 deletions src/Component/Common/Pipeline/PipeItemLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Misery\Component\Common\Pipeline;

use Misery\Component\Logger\ItemLoggerInterface;
use Misery\Component\Writer\ItemWriterInterface;

class PipeItemLogger implements PipeWriterInterface
{
public function __construct(
private readonly ItemLoggerInterface $itemLogger,
private readonly string $identityClass,
private readonly string $method
) {}

public function write(array $data): void
{
$identifier = $data['identifier'] ?? $data['code'] ?? $data['sku'] ?? $data['id'] ?? '';

// swith case for HTTP method type
switch ($this->method) {
case 'POST':
$this->itemLogger->logCreate($this->identyClass, $identifier);
break;
case 'PATCH':
case 'MULTI_PATCH':
$this->itemLogger->logUpdate($this->identityClass, $identifier);
break;
case 'DELETE':
$this->itemLogger->logRemove($this->identityClass, $identifier);
break;
break;
default:
$this->itemLogger->log($this->identityClass, $identifier);
}
}

public function stop(): void
{
}
}
10 changes: 8 additions & 2 deletions src/Component/Common/Pipeline/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Misery\Component\Common\Pipeline;

use Misery\Component\Logger\ItemLoggerAwareTrait;
use Psr\Log\LoggerAwareTrait;
use Misery\Component\Common\Pipeline\Exception\InvalidItemException;
use Misery\Component\Common\Pipeline\Exception\SkipPipeLineException;
Expand All @@ -11,6 +12,7 @@
class Pipeline
{
use LoggerAwareTrait;
use ItemLoggerAwareTrait;

/** @var PipeReaderInterface */
private $in;
Expand Down Expand Up @@ -62,13 +64,17 @@ private function handleException(InvalidItemException $exception, int $lineNumbe
{
if ($exception->hasErrors()) {
foreach ($exception->getErrors()->getErrorMessages() as $errorMessage) {
$this->logger->warning(sprintf('WARNING: %s', $errorMessage));
$this->getItemLogger()->logFailed(
$exception->getInvalidIdentityClass(),
$exception->getInvalidIdentifier(),
$errorMessage
);
}
}
$this->invalid->write([
'line' => $lineNumber,
'msg' => $exception->getMessage(),
'item' => json_encode($exception->getInvalidItem()),
'item' => json_encode($exception->getInvalidItemData()),
]);

// WE need a silent LOGGER here
Expand Down
11 changes: 11 additions & 0 deletions src/Component/Common/Pipeline/PipelineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,21 @@ public function createFromConfiguration(
$pipeline->output(new PipeWriter(
$configurationManager->createBufferWriter($configuration['output']['http']['buffer_file'])
));

$pipeline->output(new PipeItemLogger(
$configurationManager->getConfig()->getItemLogger(),
$configuration['output']['http']['endpoint'],
$configuration['output']['http']['method']
));
break;
}

$pipeline->output(new PipeWriter($writer));
$pipeline->output(new PipeItemLogger(
$configurationManager->getConfig()->getItemLogger(),
$configuration['output']['http']['endpoint'],
$configuration['output']['http']['method']
));

$pipeline->invalid(
new PipeWriter($this->createInvalid($configurationManager, $configuration))
Expand Down
2 changes: 2 additions & 0 deletions src/Component/Configurator/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Component\ChangeManager\ChangeManager;
use Misery\Component\Action\ItemActionProcessorFactory;
use Misery\Component\Logger\ItemLoggerAwareTrait;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Misery\Component\Action\ItemActionProcessor;
Expand All @@ -27,6 +28,7 @@
class Configuration
{
use LoggerAwareTrait;
use ItemLoggerAwareTrait;
private $pipeline = null;
private $actions = null;
private $groupedActions = null;
Expand Down
8 changes: 8 additions & 0 deletions src/Component/Configurator/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Misery\Component\Configurator;

use App\Component\ChangeManager\ChangeManager;
use Misery\Component\Logger\ItemLoggerInterface;
use Misery\Component\Logger\NullItemLogger;
use Psr\Log\LoggerInterface;
use Misery\Component\Common\FileManager\LocalFileManager;
use Misery\Component\Common\Pipeline\ActionPipe;
Expand Down Expand Up @@ -37,6 +39,7 @@ public function init(
) {
$this->config = new Configuration();
$this->config->setLogger($logger);
$this->config->setItemLogger(new NullItemLogger());
$sources = ($source) ? $this->getFactory('source')->createFromFileManager($source) : null;
$this->manager = new ConfigurationManager(
$this->config,
Expand All @@ -49,6 +52,11 @@ public function init(
);
}

public function setItemLogger(ItemLoggerInterface $itemLogger)
{
$this->config->setItemLogger($itemLogger);
}

public function setChangeManager(ChangeManager $changeManager)
{
$this->config->changeManager = $changeManager;
Expand Down
41 changes: 41 additions & 0 deletions src/Component/Logger/AppItemLoggerAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Misery\Component\Logger;

use App\Component\ItemTracker\ItemTrackerInterface;
use App\Component\Logger\ItemLogger;

class AppItemLoggerAdapter implements ItemLoggerInterface
{
public function __construct(private readonly ItemTrackerInterface $tracker) {}

public function logCreate(string $entityClass, string $identifier): void
{
$this->tracker->trackCreate($entityClass, $identifier);
}

public function logUpdate(string $entityClass, string $identifier, string $updateMessage = ''): void
{
$this->tracker->trackUpdate($entityClass, $identifier, $updateMessage);
}

public function logRemove(string $entityClass, string $identifier): void
{
$this->tracker->trackRemove($entityClass, $identifier);
}

public function logFailed(string $entityClass, string $identifier, string $failMessage): void
{
$this->tracker->trackFailed($entityClass, $identifier, $failMessage);
}

public function logSkipped(string $entityClass, string $identifier, string $failMessage): void
{
$this->tracker->trackSkipped($entityClass, $identifier, $failMessage);
}

public function log(string $entityClass, string $identifier): void
{
$this->tracker->track($entityClass, $identifier);
}
}
26 changes: 26 additions & 0 deletions src/Component/Logger/ItemLoggerAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Misery\Component\Logger;

use Psr\Log\LoggerInterface;

/**
* Basic Implementation
*/
trait ItemLoggerAwareTrait
{
/**
* The logger instance.
*/
protected ItemLoggerInterface $itemLogger;

public function setItemLogger(ItemLoggerInterface $itemLogger): void
{
$this->itemLogger = $itemLogger;
}

public function getItemLogger(): ItemLoggerInterface
{
return $this->itemLogger;
}
}
35 changes: 35 additions & 0 deletions src/Component/Logger/ItemLoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Misery\Component\Logger;

use App\Component\Akeneo\Api\Objects\ReferencedItemInterface;
use App\Domain\Common\Model\OperationProgress;

interface ItemLoggerInterface
{
// This is App only section

// public function logCreateItem(ReferencedItemInterface $entity): void;
//
// public function logUpdateItem(ReferencedItemInterface $entity, string $updateMessage = ''): void;
//
// public function logRemoveItem(ReferencedItemInterface $entity): void;
//
// public function logFailedItem(ReferencedItemInterface $entity, string $failMessage): void;
//
// public function logItem(ReferencedItemInterface $entity): void;

/** @deprecated */
public function logCreate(string $entityClass, string $identifier): void;

/** @deprecated */
public function logUpdate(string $entityClass, string $identifier, string $updateMessage = ''): void;

/** @deprecated */
public function logRemove(string $entityClass, string $identifier): void;
/** @deprecated */
public function logFailed(string $entityClass, string $identifier, string $failMessage): void;

/** @deprecated */
public function log(string $entityClass, string $identifier): void;
}
33 changes: 33 additions & 0 deletions src/Component/Logger/NullItemLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Misery\Component\Logger;

class NullItemLogger implements ItemLoggerInterface
{
public function logCreate(string $entityClass, string $identifier): void
{
// Do nothing
}

public function logUpdate(string $entityClass, string $identifier, string $updateMessage = ''): void
{
// Do nothing
}

public function logRemove(string $entityClass, string $identifier): void
{
// Do nothing
}

public function logFailed(string $entityClass, string $identifier, string $failMessage): void
{
// Do nothing
}

public function log(string $entityClass, string $identifier): void
{
// Do nothing
}
}