Passed
Push — master ( 0f66d0...a5c111 )
by Gabor
16:59 queued 08:35
created

DefinedHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 84
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getDefinition() 0 4 1
A getDescription() 0 5 1
A __construct() 0 13 1
A __invoke() 0 15 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\Adapter\Renderer\RendererHelperInterface;
17
use WebHemi\Application\EnvironmentManager;
18
use WebHemi\Config\ConfigInterface;
19
use WebHemi\Renderer\GetSelectedThemeResourcePathTrait;
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 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 '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 3
    public function __construct(ConfigInterface $configuration, EnvironmentManager $environmentManager)
74
    {
75 3
        $documentRoot = $environmentManager->getDocumentRoot();
76 3
        $selectedTheme = $environmentManager->getSelectedTheme();
77 3
        $selectedThemeResourcePath = $this->getSelectedThemeResourcePath(
78
            $selectedTheme,
79
            $configuration,
80
            $environmentManager
81
        );
82
83 3
        $this->defaultTemplateViewPath = $documentRoot.EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH.'/view';
84 3
        $this->templateViewPath = $documentRoot.$selectedThemeResourcePath.'/view';
85 3
    }
86
87
    /**
88
     * A renderer helper should be called with its name.
89
     *
90
     * @return bool
91
     */
92 1
    public function __invoke() : bool
93
    {
94 1
        $fileName = func_get_args()[0];
95
        $pattern = [
96 1
            '@WebHemi',
97
            '@Theme',
98
        ];
99
        $replacement = [
100 1
            $this->defaultTemplateViewPath,
101 1
            $this->templateViewPath,
102
        ];
103
104 1
        $fileName = str_replace($pattern, $replacement, $fileName);
105 1
        return file_exists($fileName);
106
    }
107
}
108