Helpers::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aidphp\Template;
6
7
use InvalidArgumentException;
8
9
class Helpers implements HelpersInterface
10
{
11
    protected $factories = [];
12
    protected $helpers   = [];
13
14 4
    public function __construct(array $factories = [])
15
    {
16 4
        foreach ($factories as $name => $factory)
17
        {
18 2
            $this->register($name, $factory);
19
        }
20 4
    }
21
22 3
    public function register(string $name, callable $factory): self
23
    {
24 3
        $this->factories[$name] = $factory;
25 3
        return $this;
26
    }
27
28 2
    public function get(string $name)
29
    {
30 2
        if (! isset($this->factories[$name]))
31
        {
32 1
            throw new InvalidArgumentException('The helper "' . $name . '" does not exist');
33
        }
34
35 1
        if (! isset($this->helpers[$name]))
36
        {
37 1
            $this->helpers[$name] = $this->factories[$name]();
38
        }
39
40 1
        return $this->helpers[$name];
41
    }
42
43 3
    public function has(string $name): bool
44
    {
45 3
        return isset($this->factories[$name]);
46
    }
47
}