Completed
Push — master ( c2d23a...f43081 )
by Jesse
02:42
created

OneOfTheseHydrators::decideOnAClassUsing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydrator;
5
6
/**
7
 * Delegates hydration, selecting a hydrator based on an input value.
8
 *
9
 * @package Stratadox\Hydrate
10
 * @author  Stratadox
11
 */
12
final class OneOfTheseHydrators implements Hydrates
13
{
14
    private $decisionKey;
15
    private $hydratorMap;
16
17
    private function __construct(string $decisionKey, array $hydratorMap)
18
    {
19
        $this->decisionKey = $decisionKey;
20
        $this->hydratorMap = $hydratorMap;
21
        foreach ($hydratorMap as $key => $instance) {
22
            $this->mustBeAString($key);
23
            $this->mustBeAHydrator($instance);
24
        }
25
    }
26
27
    /**
28
     * Creates a new selective hydrator.
29
     *
30
     * @param string $key The key in which the decision key is stored.
31
     * @param array  $map Map of hydrators as [string $key => Hydrates $hydrator].
32
     * @return Hydrates   The delegating hydrator.
33
     */
34
    public static function decideBasedOnThe(string $key, array $map): Hydrates
35
    {
36
        return new self($key, $map);
37
    }
38
39
    /** @inheritdoc */
40
    public function fromArray(array $input)
41
    {
42
        return $this->hydrateAnInstanceUsing(
43
            $this->hydratorBasedOn($this->keyFromThe($input)),
44
            $input
45
        );
46
    }
47
48
    /** @inheritdoc */
49
    public function classFor(array $input): string
50
    {
51
        return $this->decideOnAClassUsing(
52
            $this->hydratorBasedOn($this->keyFromThe($input)),
53
            $input
54
        );
55
    }
56
57
    /**
58
     * Delegate the hydration to the given hydrator.
59
     *
60
     * @param Hydrates $hydrator The hydrator to delegate to.
61
     * @param array    $input    The input data.
62
     * @return mixed|object      The hydrated instance.
63
     * @throws CannotHydrate     When the input data could not be hydrated.
64
     */
65
    private function hydrateAnInstanceUsing(Hydrates $hydrator, array $input)
66
    {
67
        return $hydrator->fromArray($input);
68
    }
69
70
    /**
71
     * Ask the hydrator for this input which class it would hydrate.
72
     *
73
     * @param Hydrates $hydrator The hydrator to delegate to.
74
     * @param array    $input    The input data.
75
     * @return string            The class for this data.
76
     * @throws CannotHydrate     In very unlikely cases.
77
     */
78
    private function decideOnAClassUsing(Hydrates $hydrates, array $input): string
79
    {
80
        return $hydrates->classFor($input);
81
    }
82
83
    /**
84
     * Selects a hydrator based on a key.
85
     *
86
     * @param string $key    The key to use for hydrator selection.
87
     * @return Hydrates      The hydrator to use.
88
     * @throws CannotHydrate When the selection key is invalid.
89
     */
90
    private function hydratorBasedOn(string $key): Hydrates
91
    {
92
        if (!isset($this->hydratorMap[$key])) {
93
            throw CannotDecideOnAHydrator::withThis($key);
94
        }
95
        return $this->hydratorMap[$key];
96
    }
97
98
    /**
99
     * Retrieves the key from the input data.
100
     *
101
     * @param array $input   The input data.
102
     * @return string        The key to use in selecting a hydrator.
103
     * @throws CannotHydrate When the key is not present in the input data.
104
     */
105
    private function keyFromThe(array $input): string
106
    {
107
        if (!isset($input[$this->decisionKey])) {
108
            throw CannotDecideOnAHydrator::without($this->decisionKey);
109
        }
110
        return $input[$this->decisionKey];
111
    }
112
113
    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

113
    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...
114
    {
115
    }
116
117
    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

117
    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...
118
    {
119
    }
120
}
121