Passed
Push — 4 ( 77a45c...ec956a )
by Damian
07:50 queued 13s
created

SapphireTestState   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getStates() 0 3 1
A getStateByClass() 0 9 3
A getStateByName() 0 7 2
A setStates() 0 4 1
A setUp() 0 4 2
A tearDown() 0 6 2
A tearDownOnce() 0 6 2
A setUpOnce() 0 4 2
1
<?php
2
3
namespace SilverStripe\Dev\State;
4
5
use SilverStripe\Core\Injector\Injectable;
6
use SilverStripe\Dev\SapphireTest;
7
8
class SapphireTestState implements TestState
9
{
10
    use Injectable;
11
12
    /**
13
     * @var TestState[]
14
     */
15
    protected $states = [];
16
17
    /**
18
     * @return TestState[]
19
     */
20
    public function getStates()
21
    {
22
        return $this->states;
23
    }
24
25
    /**
26
     * @param string $name
27
     *
28
     * @return bool|TestState
29
     */
30
    public function getStateByName($name)
31
    {
32
        $states = $this->getStates();
33
        if (array_key_exists($name, $states)) {
34
            return $states[$name];
35
        }
36
        return false;
37
    }
38
39
    /**
40
     * @param string $class
41
     *
42
     * @return bool|TestState
43
     */
44
    public function getStateByClass($class)
45
    {
46
        $lClass = strtolower($class);
47
        foreach ($this->getStates() as $state) {
48
            if ($lClass === strtolower(get_class($state))) {
49
                return $state;
50
            }
51
        }
52
        return false;
53
    }
54
55
    /**
56
     * @param TestState[] $states
57
     * @return $this
58
     */
59
    public function setStates(array $states)
60
    {
61
        $this->states = $states;
62
        return $this;
63
    }
64
65
    public function setUp(SapphireTest $test)
66
    {
67
        foreach ($this->states as $state) {
68
            $state->setUp($test);
69
        }
70
    }
71
72
    public function tearDown(SapphireTest $test)
73
    {
74
        // Tear down in reverse order
75
        /** @var TestState $state */
76
        foreach (array_reverse($this->states) as $state) {
77
            $state->tearDown($test);
78
        }
79
    }
80
81
    public function setUpOnce($class)
82
    {
83
        foreach ($this->states as $state) {
84
            $state->setUpOnce($class);
85
        }
86
    }
87
88
    /**
89
     * Called once on tear down
90
     *
91
     * @param string $class Class being torn down
92
     */
93
    public function tearDownOnce($class)
94
    {
95
        // Tear down in reverse order
96
        /** @var TestState $state */
97
        foreach (array_reverse($this->states) as $state) {
98
            $state->tearDownOnce($class);
99
        }
100
    }
101
}
102