Completed
Push — master ( 0d1096...abc53f )
by Brad
9s
created

Config::transitionsForRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Transitions;
4
5
use Generator;
6
7
class Config
8
{
9
10
    /**
11
     * The header key for the applicable transition version.
12
     * @var string
13
     */
14
    protected $headerKey;
15
16
    /**
17
     * An array of transitions to apply, keyed by version date.
18
     * @var array
19
     */
20
    protected $transitions;
21
22
    /**
23
     * Config constructor.
24
     * @param array $attributes
25
     */
26
    public function __construct(array $attributes)
27
    {
28
29
        $this->setHeaderKey($attributes)
30
             ->setTransitions($attributes);
31
    }
32
33
    /**
34
     * @return string
35
     */
36
    public function headerKey()
37
    {
38
39
        return $this->headerKey;
40
    }
41
42
    /**
43
     * Get the applicable transitions for the given version.
44
     *
45
     * @param string|int $version
46
     * @return array
47
     */
48
    public function transitionsForVersion($version) : array
49
    {
50
51
        $applicable = [];
52
        foreach ($this->filterStages($version) as $key => $classes) {
53
            $applicable = array_merge($applicable, $classes);
54
        }
55
56
        return $applicable;
57
    }
58
59
    /**
60
     * @param string|int $version
61
     * @return Generator
62
     */
63
    private function filterStages($version) : Generator
64
    {
65
66
        foreach ($this->transitions as $key => $class) {
67
            if ($version <= $key) {
68
                yield $key => $class;
69
            }
70
        }
71
    }
72
73
    /**
74
     * @param array $attributes
75
     * @return Config
76
     */
77
    public function setHeaderKey(array $attributes) : Config
78
    {
79
80
        if (! isset($attributes['headerKey'])) {
81
            throw HeaderKeyNotDefined::new();
82
        }
83
        $this->headerKey = $attributes['headerKey'];
84
85
        return $this;
86
    }
87
88
    /**
89
     * @param array $attributes
90
     * @return Config
91
     */
92
    public function setTransitions(array $attributes) : Config
93
    {
94
95
        $this->transitions = (array) $attributes['transitions'] ?? [];
96
        return $this;
97
    }
98
}
99