From 79d39602331ed5807941a871827c19badde1cc43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Laugks?= Date: Sat, 2 Nov 2024 19:33:46 +0100 Subject: [PATCH] Improvements - Simplify logic in custom Normalizer/Denormalizer - Set readonly in constructors --- .github/workflows/tests.yml | 21 +++++++ app/composer.json | 3 +- app/composer.lock | 62 ++++++++++++++++++- app/src/Normalizer/MappingTableNormalizer.php | 43 ++++--------- app/src/Normalizer/Value/BooleanValue.php | 2 +- app/src/Normalizer/Value/StringValue.php | 2 +- app/src/Service/CrmSerializerService.php | 7 +-- docker-compose.yml | 1 - 8 files changed, 97 insertions(+), 44 deletions(-) create mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..a2ba704 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,21 @@ +name: "Tests" +run-name: Tests [${{ github.ref_name }}] +on: + push: + branches: + - main + - dev + - property-normalizer + workflow_dispatch: +jobs: + tests: + name: Tests + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + + - name: "Run Tests" + run: | + docker compose up -d + docker exec article_symfony_serializer composer install + docker exec article_symfony_serializer bin/phpunit diff --git a/app/composer.json b/app/composer.json index 1673970..1247c46 100644 --- a/app/composer.json +++ b/app/composer.json @@ -17,7 +17,8 @@ "symfony/runtime": "6.4.*", "symfony/serializer": "6.4.*", "symfony/twig-bundle": "6.4.*", - "symfony/yaml": "6.4.*" + "symfony/yaml": "6.4.*", + "webmozart/assert": "^1.11" }, "config": { "allow-plugins": { diff --git a/app/composer.lock b/app/composer.lock index ed05b1c..3d06e8e 100644 --- a/app/composer.lock +++ b/app/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2c438d823a0b741f57a3fc97d27dc2c7", + "content-hash": "cdb72ce0fcf370ee835eef8718c6ce93", "packages": [ { "name": "doctrine/annotations", @@ -3255,6 +3255,64 @@ } ], "time": "2024-09-09T17:55:12+00:00" + }, + { + "name": "webmozart/assert", + "version": "1.11.0", + "source": { + "type": "git", + "url": "https://github.com/webmozarts/assert.git", + "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webmozarts/assert/zipball/11cb2199493b2f8a3b53e7f19068fc6aac760991", + "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991", + "shasum": "" + }, + "require": { + "ext-ctype": "*", + "php": "^7.2 || ^8.0" + }, + "conflict": { + "phpstan/phpstan": "<0.12.20", + "vimeo/psalm": "<4.6.1 || 4.6.2" + }, + "require-dev": { + "phpunit/phpunit": "^8.5.13" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.10-dev" + } + }, + "autoload": { + "psr-4": { + "Webmozart\\Assert\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Assertions to validate method input/output with nice error messages.", + "keywords": [ + "assert", + "check", + "validate" + ], + "support": { + "issues": "https://github.com/webmozarts/assert/issues", + "source": "https://github.com/webmozarts/assert/tree/1.11.0" + }, + "time": "2022-06-03T18:03:27+00:00" } ], "packages-dev": [ @@ -3563,7 +3621,7 @@ "description": "Provides utilities for PHPUnit, especially user deprecation notices management", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/phpunit-bridge/tree/v6.4.11" + "source": "https://github.com/symfony/phpunit-bridge/tree/v6.2.7" }, "funding": [ { diff --git a/app/src/Normalizer/MappingTableNormalizer.php b/app/src/Normalizer/MappingTableNormalizer.php index 988cccd..5af22f1 100644 --- a/app/src/Normalizer/MappingTableNormalizer.php +++ b/app/src/Normalizer/MappingTableNormalizer.php @@ -5,12 +5,13 @@ use App\Normalizer\Value\BooleanValue; use App\Normalizer\Value\StringValue; use App\Normalizer\Value\ValueInterface; -use InvalidArgumentException; -use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use Webmozart\Assert\Assert; -class MappingTableNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface +use function get_class; + +class MappingTableNormalizer implements NormalizerInterface, DenormalizerInterface { public const TABLE = 'mapping_table'; @@ -22,16 +23,13 @@ class MappingTableNormalizer implements NormalizerInterface, DenormalizerInterfa public function normalize(mixed $object, string $format = null, array $context = []): ?string { if (null === $object?->getValue() || '' === $object?->getValue()) { - return ''; // Reset value in CRM + return ''; // Reset value from property in Emarsys CRM } - $mappingTable = $this->getMappingTable($context); + Assert::keyExists($context, self::TABLE, sprintf('MappingTable not set (%s)', get_class($object))); - $key = array_search($object->getValue(), $mappingTable); - if($key) { - return (string)$key; // Force string - } - return null; + $key = array_search($object->getValue(), $context[self::TABLE], true); + return $key ? (string)$key : null; } public function supportsNormalization(mixed $data, string $format = null): bool @@ -41,33 +39,14 @@ public function supportsNormalization(mixed $data, string $format = null): bool public function denormalize($data, $type, $format = null, array $context = array()): mixed { - $mappingTable = $this->getMappingTable($context); - - foreach ($mappingTable as $key => $value) { - if ((string)$key === $data) { - return new $type($value); - } - } + Assert::keyExists($context, self::TABLE, sprintf('MappingTable not set (%s)', $type)); + $mappingTable = $context[self::TABLE]; - return new $type(null); + return isset($mappingTable[$data]) ? new $type($mappingTable[$data]) : new $type(null); } public function supportsDenormalization($data, $type, $format = null): bool { return in_array($type, self::SUPPORTED_TYPES); } - - private function getMappingTable(array $context): array - { - if (!isset($context[self::TABLE]) || !is_array($context[self::TABLE])) { - throw new InvalidArgumentException('mapping_table not defined'); - } - - return $context[self::TABLE]; - } - - public function hasCacheableSupportsMethod(): bool - { - return __CLASS__ === static::class; - } } diff --git a/app/src/Normalizer/Value/BooleanValue.php b/app/src/Normalizer/Value/BooleanValue.php index 502feec..3611565 100644 --- a/app/src/Normalizer/Value/BooleanValue.php +++ b/app/src/Normalizer/Value/BooleanValue.php @@ -4,7 +4,7 @@ class BooleanValue implements ValueInterface { - public function __construct(private ?bool $value = null) {} + public function __construct(private readonly ?bool $value = null) {} public function getValue(): ?bool { diff --git a/app/src/Normalizer/Value/StringValue.php b/app/src/Normalizer/Value/StringValue.php index 4926ac2..6d3b41f 100644 --- a/app/src/Normalizer/Value/StringValue.php +++ b/app/src/Normalizer/Value/StringValue.php @@ -4,7 +4,7 @@ class StringValue implements ValueInterface { - public function __construct(private ?string $value = null) {} + public function __construct(private readonly ?string $value = null) {} public function getValue(): ?string { diff --git a/app/src/Service/CrmSerializerService.php b/app/src/Service/CrmSerializerService.php index a879e53..2c6b57f 100644 --- a/app/src/Service/CrmSerializerService.php +++ b/app/src/Service/CrmSerializerService.php @@ -3,16 +3,11 @@ namespace App\Service; use App\Dto\ContactDto; -use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\SerializerInterface; class CrmSerializerService { - private Serializer $serializer; // Only Symfony Serializer - - public function __construct(SerializerInterface $serializer) { - $this->serializer = $serializer; - } + public function __construct(private readonly SerializerInterface $serializer) {} public function normalize(ContactDto $contactDto): array { diff --git a/docker-compose.yml b/docker-compose.yml index 4656de4..d1c9760 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,3 @@ -version: '3.6' services: php: container_name: article_symfony_serializer