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

CollectionNormalizer::createAndFillCollection()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.6111
cc 5
nc 4
nop 4
crap 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