Passed
Push — master ( f47d5f...f7335a )
by Gabor
05:38
created

DefinedHelper::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Renderer\Helper;
15
16
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface;
17
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
18
use WebHemi\Renderer\HelperInterface;
19
use WebHemi\Renderer\Traits\GetSelectedThemeResourcePathTrait;
20
21
/**
22
 * Class DefinedHelper.
23
 */
24
class DefinedHelper implements HelperInterface
25
{
26
    /** @var string */
27
    private $templateViewPath;
28
    /** @var string */
29
    private $defaultTemplateViewPath;
30
31
    use GetSelectedThemeResourcePathTrait;
32
33
    /**
34
     * Should return the name of the helper.
35
     *
36
     * @return string
37
     * @codeCoverageIgnore - plain text
38
     */
39
    public static function getName() : string
40
    {
41
        return 'defined';
42
    }
43
44
    /**
45
     * Should return the name of the helper.
46
     *
47
     * @return string
48
     * @codeCoverageIgnore - plain text
49
     */
50
    public static function getDefinition() : string
51
    {
52
        return '{% if defined("templateFileName") %}';
53
    }
54
55
    /**
56
     * Should return a description text.
57
     *
58
     * @return string
59
     * @codeCoverageIgnore - plain text
60
     */
61
    public static function getDescription() : string
62
    {
63
        return 'Checks if the given filepath exists in the template\'s path. Use @WebHemi for the default theme and '
64
            . '@Theme for the actual (custom) theme.';
65
    }
66
67
    /**
68
     * Gets helper options for the render.
69
     *
70
     * @return array
71
     * @codeCoverageIgnore - empty array
72
     */
73
    public static function getOptions() : array
74
    {
75
        return [];
76
    }
77
78
    /**
79
     * DefinedHelper constructor.
80
     *
81
     * @param ConfigurationInterface $configuration
82
     * @param EnvironmentInterface   $environmentManager
83
     */
84 3
    public function __construct(ConfigurationInterface $configuration, EnvironmentInterface $environmentManager)
85
    {
86 3
        $documentRoot = $environmentManager->getDocumentRoot();
87 3
        $selectedTheme = $environmentManager->getSelectedTheme();
88 3
        $selectedThemeResourcePath = $this->getSelectedThemeResourcePath(
89
            $selectedTheme,
90
            $configuration,
91
            $environmentManager
92
        );
93
94 3
        $this->defaultTemplateViewPath = $documentRoot.EnvironmentInterface::DEFAULT_THEME_RESOURCE_PATH.'/view';
95 3
        $this->templateViewPath = $documentRoot.$selectedThemeResourcePath.'/view';
96 3
    }
97
98
    /**
99
     * A renderer helper should be called with its name.
100
     *
101
     * @return bool
102
     */
103 1
    public function __invoke() : bool
104
    {
105 1
        $fileName = func_get_args()[0];
106
        $pattern = [
107 1
            '@WebHemi',
108
            '@Theme',
109
        ];
110
        $replacement = [
111 1
            $this->defaultTemplateViewPath,
112 1
            $this->templateViewPath,
113
        ];
114
115 1
        $fileName = str_replace($pattern, $replacement, $fileName);
116 1
        return file_exists($fileName);
117
    }
118
}
119