Passed
Branch master (e714e6)
by Gabriel
13:58 queued 11:36
created

HelpersCollection::initHelper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 3
cts 5
cp 0.6
crap 2.2559
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\View\Helpers;
4
5
use Nip\Collections\AbstractCollection;
6
use Nip\View;
7
use Nip\View\Extensions\Helpers\HelperNotFoundException;
8
9
/**
10
 * Class MethodsCollection
11
 * @package Nip\View\Methods
12
 */
13
class HelpersCollection extends AbstractCollection
14
{
15
    protected static $instance = null;
16
17
    /**
18
     * @var View
19
     */
20
    protected $engine = null;
21
22
23
    /**
24
     * @param $name
25
     * @return mixed
26
     */
27 1
    public function getHelper($name)
28
    {
29 1
        if (!$this->has($name)) {
30
            $this->initHelper($name);
31
        }
32
33 1
        return $this->get($name, null);
34
    }
35
36
    /**
37
     * @param $name
38
     * @return bool
39
     */
40 1
    public function hasHelper($name)
41
    {
42 1
        if (!$this->has($name)) {
43 1
            $this->initHelper($name);
44
        }
45
46 1
        return is_object($this->get($name, null));
47
    }
48
49
    /**
50
     * @param $name
51
     */
52 1
    public function initHelper($name)
53
    {
54
        try {
55 1
            $helper = HelpersFactory::create($this->getEngine(), $name);
56
        } catch (HelperNotFoundException $exception) {
57
            $helper = null;
58
        }
59 1
        $this->set($name, $helper);
60 1
    }
61
62
    /**
63
     * @return View
64
     */
65 1
    public function getEngine(): View
66
    {
67 1
        return $this->engine;
68
    }
69
70
    /**
71
     * @param View $engine
72
     */
73 6
    public function setEngine(View $engine): void
74
    {
75 6
        $this->engine = $engine;
76 6
    }
77
78
    /**
79
     * @return self
80
     */
81 6
    public static function getInstance()
82
    {
83 6
        if (!isset(static::$instance)) {
84 1
            static::$instance = new static;
85
        }
86 6
        return static::$instance;
87
    }
88
}
89