HelpersFactory::getHelperClassVariations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nip\View\Helpers;
6
7
use Nip\View\Extensions\Helpers\HelperNotFoundException;
8
9
/**
10
 * Class HelpersFactory.
11
 */
12
class HelpersFactory
13
{
14
    /**
15
     * @return mixed
16
     *
17
     * @throws HelperNotFoundException
18
     */
19 2
    public static function create($view, $name)
20
    {
21 2
        $class = self::getHelperClass($name);
22 2
        if (!class_exists($class)) {
23
            throw new HelperNotFoundException("No helper name [{$name}] found for view engine.");
24
        }
25 2
        $helper = new $class();
26 2
        $helper->setView($view);
27
28 2
        return $helper;
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public static function getHelperClass($name)
35 2
    {
36
        $classNameVariations = self::getHelperClassVariations($name);
37 2
        foreach ($classNameVariations as $nameVariation) {
38 2
            if (class_exists($nameVariation)) {
39 2
                return $nameVariation;
40 1
            }
41
        }
42
43 1
        return '\Nip\Helpers\View\\' . $name;
44
    }
45
46
    /**
47
     * @param string $name
48
     *
49
     * @return string[]
50 2
     */
51
    public static function getHelperClassVariations($name)
52
    {
53 2
        return [
54
            '\Nip\View\Helpers\\' . $name . 'Helper',
55
        ];
56
    }
57
}
58