1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lamoda\IsmpClient\Impl\Serializer\V3; |
6
|
|
|
|
7
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeCisItemResponse; |
8
|
|
|
use Lamoda\IsmpClient\V3\Dto\FacadeCisListResponse; |
9
|
|
|
use Symfony\Component\Serializer\Exception\BadMethodCallException; |
10
|
|
|
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
11
|
|
|
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; |
12
|
|
|
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; |
13
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface; |
14
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
15
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
16
|
|
|
|
17
|
|
View Code Duplication |
final class FacadeCisListResponseDenormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface, CacheableSupportsMethodInterface |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var DenormalizerInterface |
21
|
|
|
*/ |
22
|
|
|
private $denormalizer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
* |
27
|
|
|
* @throws NotNormalizableValueException |
28
|
|
|
*/ |
29
|
4 |
|
public function denormalize($data, $class, $format = null, array $context = []) |
30
|
|
|
{ |
31
|
4 |
|
if (null === $this->denormalizer) { |
32
|
1 |
|
throw new BadMethodCallException('Please set a serializer before calling denormalize()!'); |
33
|
|
|
} |
34
|
3 |
|
if (!\is_array($data)) { |
35
|
1 |
|
throw new InvalidArgumentException('Data expected to be an array, ' . \gettype($data) . ' given.'); |
36
|
|
|
} |
37
|
2 |
|
if (FacadeCisListResponse::class !== $class) { |
38
|
1 |
|
throw new InvalidArgumentException('Unsupported class: ' . $class); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
$items = []; |
42
|
1 |
|
foreach ($data as $datum) { |
43
|
1 |
|
$items[] = $this->denormalizer->denormalize($datum, FacadeCisItemResponse::class, $format, $context); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
return new FacadeCisListResponse(...$items); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
10 |
|
public function supportsDenormalization($data, $type, $format = null, array $context = []) |
53
|
|
|
{ |
54
|
10 |
|
return FacadeCisListResponse::class === $type; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
18 |
|
public function setDenormalizer(DenormalizerInterface $denormalizer) |
61
|
|
|
{ |
62
|
18 |
|
$this->denormalizer = $denormalizer; |
63
|
18 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
9 |
|
public function hasCacheableSupportsMethod(): bool |
69
|
|
|
{ |
70
|
9 |
|
return $this->denormalizer instanceof CacheableSupportsMethodInterface && $this->denormalizer->hasCacheableSupportsMethod(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.