Completed
Push — master ( 2cee6a...9d457f )
by Jesse
03:27
created

OneOfTheseHydrators::currentInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Stratadox\Hydration\Hydrator;
6
7
use Stratadox\Hydration\Hydrates;
8
use Stratadox\Hydration\UnmappableInput;
9
10
/**
11
 * Delegates hydration, selecting a hydrator based on an input value.
12
 *
13
 * @package Stratadox\Hydrate
14
 * @author Stratadox
15
 */
16
final class OneOfTheseHydrators implements Hydrates
17
{
18
    private $decisionKey;
19
    private $hydratorMap;
20
    private $current;
21
22
    private function __construct(string $decisionKey, array $hydratorMap)
23
    {
24
        $this->decisionKey = $decisionKey;
25
        $this->hydratorMap = $hydratorMap;
26
        foreach ($hydratorMap as $key => $instance) {
27
            $this->mustBeAString($key);
28
            $this->mustBeAHydrator($instance);
29
        }
30
    }
31
32
    public static function decideBasedOnThe(string $key, array $map) : Hydrates
33
    {
34
        return new OneOfTheseHydrators($key, $map);
35
    }
36
37
    /**
38
     * @throws UnmappableInput
39
     * @inheritdoc
40
     */
41
    public function fromArray(array $input)
42
    {
43
        try {
44
            $this->current = $this->hydratorBasedOn($this->keyFromThe($input));
45
            return $this->hydrateAnInstanceUsing($this->current, $input);
46
        } finally {
47
            $this->current = null;
48
        }
49
    }
50
51
    public function currentInstance()
52
    {
53
        if (!$this->current instanceof Hydrates) {
54
            return null;
55
        }
56
        return $this->current->currentInstance();
57
    }
58
59
    private function hydrateAnInstanceUsing(Hydrates $hydrator, array $input)
60
    {
61
        return $hydrator->fromArray($input);
62
    }
63
64
    /** @throws UnmappableInput */
65
    private function hydratorBasedOn(string $key) : Hydrates
66
    {
67
        if (!isset($this->hydratorMap[$key])) {
68
            throw CannotDecideOnAHydrator::withThis($key);
69
        }
70
        return $this->hydratorMap[$key];
71
    }
72
73
    /** @throws UnmappableInput */
74
    private function keyFromThe(array $input) : string
75
    {
76
        if (!isset($input[$this->decisionKey])) {
77
            throw CannotDecideOnAHydrator::without($this->decisionKey);
78
        }
79
        return $input[$this->decisionKey];
80
    }
81
82
    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

82
    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...
83
    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

83
    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...
84
}
85