HydrationCollector::hydrate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 19
ccs 11
cts 11
cp 1
crap 3
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PanicLabCore\Services\Hydrators;
6
7
use PanicLabCore\Services\Exceptions\InvalidTileTypeException;
8
use PanicLabCore\Structs\Tile;
9
use Webmozart\Assert\Assert;
10
11
class HydrationCollector implements HydrationCollectorInterface
12
{
13
    /** @var \Traversable */
14
    private $tileHydrators;
15
16 18
    public function __construct(\Traversable $tileHydrators)
17
    {
18 18
        $this->tileHydrators = $tileHydrators;
19 18
    }
20
21 1
    public function supports(string $type): bool
22
    {
23
        // Nothing to be done here
24 1
        return true;
25
    }
26
27 17
    public function hydrate(array $tile): Tile
28
    {
29 17
        $this->validate($tile);
30
31
        /** @var HydratorInterface $tileHydrator */
32 16
        foreach ($this->tileHydrators as $tileHydrator) {
33 16
            if (!$tileHydrator->supports($tile['type'])) {
34 14
                continue;
35
            }
36
37 15
            $tileHydrator->validate($tile);
38
39 14
            return $tileHydrator->hydrate($tile);
40
        }
41
42 1
        throw new InvalidTileTypeException(
43 1
            \sprintf(
44 1
                'The provided type \'%s\’ seems to be invalid. No matching hydrator found.',
45 1
                $tile['type']
46
            )
47
        );
48
    }
49
50 17
    public function validate(array $tile): void
51
    {
52 17
        Assert::keyExists($tile, 'type');
53 16
    }
54
}
55