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

InstanceHelperRegistry   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 94.44%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 51
ccs 17
cts 18
cp 0.9444
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addHelper() 0 4 1
A hasHelper() 0 4 1
A getHelper() 0 12 3
A setDefaultHelper() 0 4 1
A getDefaultHelper() 0 8 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