Completed
Pull Request — master (#5)
by Thibaud
02:13
created

InstanceHelperRegistry::getHelper()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Alchemy\Phraseanet\Helper;
4
5
class InstanceHelperRegistry
6
{
7
8
    private $defaultInstance = null;
9
10
    /**
11
     * @var InstanceHelper[]
12
     */
13
    private $instanceHelpers = [];
14
15 3
    public function addHelper($name, InstanceHelper $helper)
16
    {
17 3
        $this->instanceHelpers[$name] = $helper;
18 3
    }
19
20 4
    public function hasHelper($name)
21
    {
22 4
        return isset($this->instanceHelpers[$name]);
23
    }
24
25 3
    /**
26
     * @param $name
27 3
     * @return InstanceHelper
28
     */
29
    public function getHelper($name)
30
    {
31 3
        if ($name == null) {
32 1
            return $this->getDefaultHelper();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->getDefaultHelper(); of type Alchemy\Phraseanet\Helper\InstanceHelper|false adds false to the return on line 32 which is incompatible with the return type documented by Alchemy\Phraseanet\Helpe...lperRegistry::getHelper of type Alchemy\Phraseanet\Helper\InstanceHelper. It seems like you forgot to handle an error condition.
Loading history...
33
        }
34
35 2
        if (! $this->hasHelper($name)) {
36
            throw new \OutOfBoundsException("Helper '$name' is not defined.");
37
        }
38 1
39
        return $this->instanceHelpers[$name];
40 1
    }
41 1
42
    public function setDefaultHelper($name)
43 2
    {
44
        $this->defaultInstance = $name;
45 2
    }
46 1
47
    public function getDefaultHelper()
48
    {
49 1
        if ($this->defaultInstance == null) {
50
            return reset($this->instanceHelpers);
51
        }
52
53
        return $this->getHelper($this->defaultInstance);
54
    }
55
}
56