Completed
Push — master ( 1c57a7...75ff44 )
by Thibaud
8s
created

InstanceHelperRegistry::getDefaultHelper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 1
cts 1
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
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