Completed
Push — master ( 9d457f...574e8f )
by Jesse
02:48
created

OneOfTheseHydrators   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 61
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A decideBasedOnThe() 0 3 1
A hydrateAnInstanceUsing() 0 3 1
A mustBeAString() 0 1 1
A mustBeAHydrator() 0 1 1
A hydratorBasedOn() 0 6 2
A keyFromThe() 0 6 2
A fromArray() 0 5 1
A __construct() 0 7 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Stratadox\Hydrator;
6
7
/**
8
 * Delegates hydration, selecting a hydrator based on an input value.
9
 *
10
 * @package Stratadox\Hydrate
11
 * @author Stratadox
12
 */
13
final class OneOfTheseHydrators implements Hydrates
14
{
15
    private $decisionKey;
16
    private $hydratorMap;
17
18
    private function __construct(string $decisionKey, array $hydratorMap)
19
    {
20
        $this->decisionKey = $decisionKey;
21
        $this->hydratorMap = $hydratorMap;
22
        foreach ($hydratorMap as $key => $instance) {
23
            $this->mustBeAString($key);
24
            $this->mustBeAHydrator($instance);
25
        }
26
    }
27
28
    /**
29
     * Creates a new selective hydrator.
30
     *
31
     * @param string $key The key in which the decision key is stored.
32
     * @param array  $map Map of hydrators as [string $key => Hydrates $hydrator].
33
     * @return self       The delegating hydrator.
34
     */
35
    public static function decideBasedOnThe(string $key, array $map) : self
36
    {
37
        return new self($key, $map);
38
    }
39
40
    /** @inheritdoc */
41
    public function fromArray(array $input)
42
    {
43
        return $this->hydrateAnInstanceUsing(
44
            $this->hydratorBasedOn($this->keyFromThe($input)),
45
            $input
46
        );
47
    }
48
49
    private function hydrateAnInstanceUsing(Hydrates $hydrator, array $input)
50
    {
51
        return $hydrator->fromArray($input);
52
    }
53
54
    /** @throws CouldNotHydrate */
55
    private function hydratorBasedOn(string $key) : Hydrates
56
    {
57
        if (!isset($this->hydratorMap[$key])) {
58
            throw CannotDecideOnAHydrator::withThis($key);
59
        }
60
        return $this->hydratorMap[$key];
61
    }
62
63
    /** @throws CouldNotHydrate */
64
    private function keyFromThe(array $input) : string
65
    {
66
        if (!isset($input[$this->decisionKey])) {
67
            throw CannotDecideOnAHydrator::without($this->decisionKey);
68
        }
69
        return $input[$this->decisionKey];
70
    }
71
72
    private function mustBeAString(string $key) : void {}
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

72
    private function mustBeAString(/** @scrutinizer ignore-unused */ string $key) : void {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
    private function mustBeAHydrator(Hydrates $instance) : void {}
0 ignored issues
show
Unused Code introduced by
The parameter $instance is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

73
    private function mustBeAHydrator(/** @scrutinizer ignore-unused */ Hydrates $instance) : void {}

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
}
75