HelpersCollection   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 18
c 1
b 0
f 0
dl 0
loc 63
ccs 16
cts 19
cp 0.8421
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHelper() 0 7 2
A initHelper() 0 8 2
A hasHelper() 0 7 2
A getEngine() 0 3 1
A setEngine() 0 3 1
A getInstance() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nip\View\Helpers;
6
7
use Nip\Collections\AbstractCollection;
8
use Nip\View\Extensions\Helpers\HelperNotFoundException;
9
use Nip\View\View;
10
use function is_object;
11
12
/**
13
 * Class MethodsCollection.
14
 */
15
class HelpersCollection extends AbstractCollection
16
{
17
    protected static $instance = null;
18
19
    /**
20
     * @var View
21
     */
22
    protected $engine = null;
23
24
    /**
25
     * @return mixed
26
     */
27 2
    public function getHelper($name)
28
    {
29 2
        if (!$this->has($name)) {
30
            $this->initHelper($name);
31
        }
32
33 2
        return $this->get($name, null);
34
    }
35
36
    /**
37
     * @return bool
38
     */
39
    public function hasHelper($name)
40 2
    {
41
        if (!$this->has($name)) {
42 2
            $this->initHelper($name);
43 2
        }
44
45
        return is_object($this->get($name, null));
46 2
    }
47
48
    public function initHelper($name)
49
    {
50
        try {
51
            $helper = HelpersFactory::create($this->getEngine(), $name);
52 2
        } catch (HelperNotFoundException $exception) {
53
            $helper = null;
54
        }
55 2
        $this->set($name, $helper);
56
    }
57
58
    public function getEngine(): View
59 2
    {
60 2
        return $this->engine;
61
    }
62
63
    public function setEngine(View $engine): void
64
    {
65 2
        $this->engine = $engine;
66
    }
67 2
68
    /**
69
     * @return self
70
     */
71
    public static function getInstance()
72
    {
73 11
        if (!isset(static::$instance)) {
74
            static::$instance = new static();
75 11
        }
76 11
77
        return static::$instance;
78
    }
79
}
80