Passed
Push — master ( 86c3d2...1ebd3c )
by Gabor
07:26
created

DefinedHelper::getDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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\Adapter\Renderer\RendererHelperInterface;
17
use WebHemi\Application\EnvironmentManager;
18
use WebHemi\Config\ConfigInterface;
19
use WebHemi\Renderer\ThemeCheckTrait;
20
21
/**
22
 * Class DefinedHelper
23
 */
24
class DefinedHelper implements RendererHelperInterface
25
{
26
    /** @var string */
27
    private $templateViewPath;
28
    /** @var string */
29
    private $defaultTemplateViewPath;
30
31
    use ThemeCheckTrait;
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 'defined(string templateFileName) : bool';
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
     * DefinedHelper constructor.
69
     *
70
     * @param ConfigInterface $configuration
71
     * @param EnvironmentManager $environmentManager
72
     */
73
    public function __construct(ConfigInterface $configuration, EnvironmentManager $environmentManager)
74
    {
75
        $documentRoot = $environmentManager->getDocumentRoot();
76
        $selectedTheme = $environmentManager->getSelectedTheme();
77
        $selectedThemeResourcePath = $environmentManager->getResourcePath();
78
79 View Code Duplication
        if (!$configuration->has('themes/'.$selectedTheme)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
            || !$this->checkSelectedThemeFeatures(
81
                $configuration->getConfig('themes/'.$selectedTheme),
82
                $environmentManager
83
            )
84
        ) {
85
            $selectedThemeResourcePath = EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH;
86
        }
87
88
        $this->defaultTemplateViewPath = $documentRoot.EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH.'/view';
89
        $this->templateViewPath = $documentRoot.$selectedThemeResourcePath.'/view';
90
    }
91
92
    /**
93
     * A renderer helper should be called with its name.
94
     *
95
     * @return bool
96
     */
97
    public function __invoke() : bool
98
    {
99
        $fileName = func_get_args()[0];
100
        $pattern = [
101
            '@WebHemi',
102
            '@Theme',
103
        ];
104
        $replacement = [
105
            $this->defaultTemplateViewPath,
106
            $this->templateViewPath,
107
        ];
108
109
        $fileName = str_replace($pattern, $replacement, $fileName);
110
        return file_exists($fileName);
111
    }
112
}
113