Skip to content

Commit

Permalink
Replace Serializer by SerializeInterface (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrePetrone committed Jul 25, 2023
1 parent 00bec02 commit c9f6860
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/Bundle/Command/AbstractImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

abstract class AbstractImportCommand extends Command
{
Expand All @@ -37,7 +37,7 @@ abstract class AbstractImportCommand extends Command

abstract protected function handleItem(): callable;

abstract protected function getReader(?string $filename = null): ReaderInterface;
abstract protected function getReader(string $filename = null): ReaderInterface;

protected function configure(): void
{
Expand Down Expand Up @@ -105,7 +105,7 @@ protected function getArchiver(): ?ArchiverInterface
return null;
}

protected function getSerializer(): ?Serializer
protected function getSerializer(): ?SerializerInterface
{
return null;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Bundle/Processor/CliProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

class CliProcessor implements BatchProcessorInterface
{
Expand All @@ -45,7 +47,7 @@ public function __construct(
private readonly \Closure $handleItem,
private readonly \Closure $handleBatch,
private readonly \Closure $handleEnd,
private ?Serializer $serializer = null,
private ?SerializerInterface $serializer = null,
) {
$this->serializer = $serializer ?? new Serializer([new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter())]);

Expand Down Expand Up @@ -144,7 +146,9 @@ private function formatDataToDebug(mixed $data): array
if (\is_object($data)) {
$this->serializer ??= new Serializer([new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter())]);

$data = $this->serializer->normalize($data);
if ($this->serializer instanceof NormalizableInterface) {
$data = $this->serializer->normalize($data);
}
}

$result = [];
Expand Down
10 changes: 7 additions & 3 deletions src/DataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

class DataImporter
{
private readonly Serializer $serializer;
private readonly SerializerInterface $serializer;

public function __construct(
private readonly ReaderInterface $reader,
private readonly ProcessorInterface $processor,
private readonly ?ArchiverInterface $archiver = null,
?Serializer $serializer = null,
SerializerInterface $serializer = null,
private readonly ?MessageBusInterface $bus = null,
) {
$this->serializer = $serializer ?? new Serializer([new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter())]);
Expand Down Expand Up @@ -76,7 +77,10 @@ public function execute(): void
private function serializeData(array $data): mixed
{
try {
return $this->serializer->denormalize($data, $this->reader->getDto());
/** @var Serializer $serializer */
$serializer = $this->serializer;

return $serializer->denormalize($data, $this->reader->getDto());
} catch (\Exception $exception) {
throw new \InvalidArgumentException('An error occurred while denormalizing data: '.$exception->getMessage(), $exception->getCode(), $exception);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exchange/MessageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class MessageFactory
public static function create(
ReaderInterface $reader,
mixed $data = null,
?string $archiveFilePath = null,
string $archiveFilePath = null,
): Message {
return new Message(
$reader->getFile()->getFilename(),
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/Command/BookImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function handleItem(): callable
};
}

protected function getReader(?string $filename = null): ReaderInterface
protected function getReader(string $filename = null): ReaderInterface
{
return new CsvReader($filename, null, [CsvReader::CONTEXT_DELIMITER => ';']);
}
Expand Down

0 comments on commit c9f6860

Please sign in to comment.