Passed
Pull Request — master (#11)
by Igor
08:42
created

CollectionNormalizer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
eloc 26
c 1
b 0
f 1
dl 0
loc 53
ccs 25
cts 25
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A denormalize() 0 15 3
A supportsDenormalization() 0 11 3
A createAndFillCollection() 0 19 5
1
<?php
2
/*
3
 * This file is part of Goodwix Doctrine JSON ODM.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Goodwix\DoctrineJsonOdm\Serialization\RamseyCollection;
10
11
use Ramsey\Collection\CollectionInterface;
12
use Ramsey\Collection\Exception\InvalidArgumentException as RamseyInvalidArgumentException;
13
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
14
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
15
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
16
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
17
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
18
19
class CollectionNormalizer implements DenormalizerInterface, DenormalizerAwareInterface
20
{
21
    use DenormalizerAwareTrait;
22
23 8
    public function supportsDenormalization($data, $type, $format = null): bool
24
    {
25 8
        $supports = false;
26
27 8
        if (class_exists($type)) {
28 7
            $reflectionClass = new \ReflectionClass($type);
29 7
            $inheritsClass   = $reflectionClass->isSubclassOf(CollectionInterface::class);
30 7
            $supports        = is_array($data) && $inheritsClass;
31
        }
32
33 8
        return $supports;
34
    }
35
36 6
    public function denormalize($data, $class, $format = null, array $context = []): CollectionInterface
37
    {
38 6
        if (!is_array($data)) {
39 1
            throw new UnexpectedValueException(
40 1
                sprintf('Expected value of type "array", value of type "%s" is given.', gettype($data))
41
            );
42
        }
43
44
        try {
45 5
            $collection = $this->createAndFillCollection($data, $class, $format, $context);
46 1
        } catch (RamseyInvalidArgumentException $exception) {
47 1
            throw new InvalidArgumentException($exception->getMessage(), $exception->getCode(), $exception);
48
        }
49
50 4
        return $collection;
51
    }
52
53 5
    private function createAndFillCollection(array $data, string $class, string $format, array $context): CollectionInterface
54
    {
55
        /** @var CollectionInterface $collection */
56 5
        $collection = new $class();
57
58 5
        $itemType = $collection->getType();
59
60 5
        if (class_exists($itemType) || interface_exists($itemType)) {
61 4
            foreach ($data as $item) {
62 4
                $item = $this->denormalizer->denormalize($item, $itemType, $format, $context);
63 4
                $collection->add($item);
64
            }
65
        } else {
66 1
            foreach ($data as $item) {
67 1
                $collection->add($item);
68
            }
69
        }
70
71 4
        return $collection;
72
    }
73
}
74