CollectionStrategy   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 78
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A extract() 0 21 5
A hydrate() 0 19 4
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Doctrine\Hydrator\Strategy;
5
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Zend\Hydrator\Exception\InvalidArgumentException;
9
use Zend\Hydrator\HydratorInterface;
10
use Zend\Hydrator\Strategy\StrategyInterface;
11
12
final class CollectionStrategy implements StrategyInterface
13
{
14
    /**
15
     * @var HydratorInterface
16
     */
17
    private $objectHydrator;
18
    /**
19
     * @var string
20
     */
21
    private $objectClassName;
22
23
    /**
24
     * @param HydratorInterface $objectHydrator
25
     * @param string $objectClassName
26
     *
27
     * @throws InvalidArgumentException
28
     */
29 58
    public function __construct(HydratorInterface $objectHydrator, string $objectClassName)
30
    {
31 58
        if (! class_exists($objectClassName)) {
32 2
            throw new InvalidArgumentException(sprintf(
33 2
                'Object class name does not exist: "%s".',
34 2
                $objectClassName
35
            ));
36
        }
37
38 58
        $this->objectHydrator = $objectHydrator;
39 58
        $this->objectClassName = $objectClassName;
40 58
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 36
    public function extract($value)
46
    {
47 36
        if (! ($value instanceof Collection)) {
48 2
            throw new InvalidArgumentException(sprintf(
49 2
                'Value needs to be a Doctrine Collection, got %s instead.',
50 2
                is_object($value) ? get_class($value) : gettype($value)
51
            ));
52
        }
53
54
        return array_map(function ($object) {
55 34
            if (! $object instanceof $this->objectClassName) {
56 2
                throw new InvalidArgumentException(sprintf(
57 2
                    'Value needs to be an instance of "%s", got "%s" instead.',
58 2
                    $this->objectClassName,
59 2
                    is_object($object) ? get_class($object) : gettype($object)
60
                ));
61
            }
62
63 34
            return $this->objectHydrator->extract($object);
64 34
        }, $value->toArray());
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 4
    public function hydrate($value): Collection
71
    {
72 4
        if (! is_iterable($value)) {
73 2
            throw new InvalidArgumentException(sprintf(
74 2
                'Value needs to be an Iterable, got %s instead.',
75 2
                is_object($value) ? get_class($value) : gettype($value)
76
            ));
77
        }
78
79 2
        $collection = new ArrayCollection();
80 2
        foreach ($value as $item) {
81 2
            $collection->add($this->objectHydrator->hydrate(
82 2
                $item,
83 2
                new $this->objectClassName
84
            ));
85
        }
86
87 2
        return $collection;
88
    }
89
}
90