<?php
namespace App\Api\EventListener;
use App\Api\Event\ImportEvent;
use App\Entity\Sale;
use App\Entity\Vehicle;
use Psr\Log\LoggerInterface;
class ImportLogListener
{
private $counter;
public function __construct(private readonly LoggerInterface $logger)
{
$this->counter = ['ignored' => 0, 'persisted' => 0];
}
public function onNewData(ImportEvent $event): void
{
$data = (array) $event->getObject();
if (isset($data['saleid'])) {
$this->logger->info('Received Sale '.$data['saleid'].'...');
} elseif (isset($data['id'])) {
dump('pouet');
$this->logger->info('Received Vehicle '.$data['id'].'...');
} else {
$this->logger->info('Received some data...');
}
}
public function onIgnoredData(ImportEvent $event): void
{
$data = (array) $event->getObject();
if (isset($data['status']) && 'D' === $data['status']) {
$this->logger->info('Vehicle deleted.');
} else {
$this->logger->warning('The object could not be resolved. This might mean something is wrong with the data.');
}
++$this->counter['ignored'];
}
public function onPersistedData(ImportEvent $event): void
{
$entity = $event->getObject();
if ($entity instanceof Sale) {
$this->logger->info(sprintf('Sale %s imported.', $entity->getId()));
} elseif ($entity instanceof Vehicle) {
$this->logger->info(sprintf('Vehicle imported with new id %s.', $entity->getId()));
}
++$this->counter['persisted'];
}
public function onErrorData(ImportEvent $event): void
{
$this->logger->error(sprintf('Vehicle %s error', $event->getId() ?? ''));
}
public function onEnd(ImportEvent $event): void
{
$this->logger->info(sprintf('Summary sale %s: persisted %s, ignored/deleted %s.', $event->getId() ?? '', $this->counter['persisted'], $this->counter['ignored']));
}
}