Passed
Push — master ( 5c9096...cd0441 )
by Gabor
03:00
created

TwigRendererAdapter::isFeatureSupported()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 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
namespace WebHemi\Adapter\Renderer\Twig;
13
14
use InvalidArgumentException;
15
use Psr\Http\Message\StreamInterface;
16
use Twig_Environment;
17
use Twig_Extension_Debug;
18
use Twig_Loader_Filesystem;
19
use Twig_SimpleFunction;
20
use WebHemi\Adapter\Renderer\RendererAdapterInterface;
21
use WebHemi\Application\EnvironmentManager;
22
use WebHemi\Config\ConfigInterface;
23
24
/**
25
 * Class TwigRendererAdapter.
26
 */
27
class TwigRendererAdapter implements RendererAdapterInterface
28
{
29
    private $adapter;
30
    /** @var ConfigInterface */
31
    private $configuration;
32
    /** @var EnvironmentManager */
33
    private $environmentManager;
34
    /** @var string */
35
    private $defaultViewPath;
36
    /** @var string */
37
    private $templateViewPath;
38
    /** @var string */
39
    private $templateResourcePath;
40
    /** @var string */
41
    private $applicationBaseUri;
42
43
    /**
44
     * RendererAdapterInterface constructor.
45
     *
46
     * @param ConfigInterface    $configuration
47
     * @param EnvironmentManager $environmentManager
48
     */
49 6
    public function __construct(ConfigInterface $configuration, EnvironmentManager $environmentManager)
50
    {
51 6
        $this->environmentManager = $environmentManager;
52 6
        $documentRoot = $environmentManager->getDocumentRoot();
53 6
        $selectedTheme = $environmentManager->getSelectedTheme();
54 6
        $selectedThemeResourcePath = $environmentManager->getResourcePath();
55
56 6
        if (!$configuration->has('themes/'.$selectedTheme)
57 6
            || !$this->checkSelectedThemeFeatures($configuration->getConfig('themes/'.$selectedTheme))
58 6
        ) {
59 4
            $selectedTheme = EnvironmentManager::DEFAULT_THEME;
60 4
            $selectedThemeResourcePath = EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH;
61 4
        }
62
63 6
        $this->configuration = $configuration->getConfig('themes/'.$selectedTheme);
64
65 6
        $this->defaultViewPath = $documentRoot.EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH.'/view';
66 6
        $this->templateViewPath = $documentRoot.$selectedThemeResourcePath.'/view';
67 6
        $this->templateResourcePath = $selectedThemeResourcePath.'/static';
68 6
        $this->applicationBaseUri = $environmentManager->getSelectedApplicationUri();
69
70 6
        $loader = new Twig_Loader_Filesystem($this->templateViewPath);
71 6
        $loader->addPath($this->defaultViewPath, 'WebHemi');
72 6
        $loader->addPath($this->templateViewPath, 'Theme');
73 6
        $this->adapter = new Twig_Environment($loader, array('debug' => true, 'cache' => false));
74 6
        $this->adapter->addExtension(new Twig_Extension_Debug());
75
76 6
        $viewPath = $this->templateViewPath;
77
        // @codeCoverageIgnoreStart
78
        // link a core function into template level
79
        $function = new Twig_SimpleFunction('defined', function ($fileName) use ($viewPath) {
80
            $fileName = str_replace('@Theme', $viewPath, $fileName);
81
            return file_exists($fileName);
82
        });
83
        $this->adapter->addFunction($function);
84
        // @codeCoverageIgnoreEnd
85 6
    }
86
87
    /**
88
     * Renders the template for the output.
89
     *
90
     * @param string $template
91
     * @param array  $parameters
92
     *
93
     * @throws InvalidArgumentException
94
     *
95
     * @return StreamInterface
96
     */
97 4
    public function render($template, $parameters = [])
98
    {
99 4
        if ($this->configuration->has('map/'.$template)) {
100 4
            $template = $this->configuration->getData('map/'.$template);
101 4
        }
102
103 4
        if (!file_exists($this->templateViewPath.'/'.$template)) {
104 3
            throw new InvalidArgumentException(
105 3
                sprintf(
106 3
                    'Unable to render file: "%s". No such file: %s.',
107 3
                    $template,
108 3
                    $this->templateViewPath.'/'.$template
109 3
                )
110 3
            );
111
        }
112
113
        // Tell the template where the resources are.
114 4
        $parameters['template_resource_path'] = $this->templateResourcePath;
115 4
        $parameters['application_base_uri'] = $this->applicationBaseUri;
116
117 4
        $output = $this->adapter->render($template, $parameters);
118
119
        // The ugliest shit ever. But that is how they made it... :/
120 4
        return \GuzzleHttp\Psr7\stream_for($output);
121
    }
122
123
    /**
124
     * Checks if the selected theme can be used with the current application.
125
     *
126
     * @param ConfigInterface $themeConfig
127
     * @return bool
128
     */
129 6
    private function checkSelectedThemeFeatures(ConfigInterface $themeConfig)
130
    {
131 6
        $canUseThisTheme = true;
132
133
        // check the theme settings
134
        // If no theme support for the application, then use the default theme
135 6
        if (($this->isAdminApplication(false) && !$this->isFeatureSupported($themeConfig, 'admin'))
136 6
            || ($this->isAdminApplication(true) && !$this->isFeatureSupported($themeConfig, 'admin_login'))
137 6
            || (!$this->isAdminApplication(false) && !$this->isFeatureSupported($themeConfig, 'website'))
138 6
            || (!$this->isAdminApplication(true) && !$this->isFeatureSupported($themeConfig, 'website'))
139 6
        ) {
140 3
            $canUseThisTheme = false;
141 3
        }
142
143 6
        return $canUseThisTheme;
144
    }
145
146
    /**
147
     * Checks whether the current application is the Admin(login) or not.
148
     *
149
     * @param bool $checkIfLogin
150
     * @return bool
151
     */
152 6
    private function isAdminApplication($checkIfLogin = false)
153
    {
154 6
        $isAdmin = 'admin' == $this->environmentManager->getSelectedApplication();
155 6
        $isLogin = strpos($this->environmentManager->getRequestUri(), '/auth/login') !== false;
156
157 6
        return $checkIfLogin ? $isAdmin && $isLogin : $isAdmin && !$isLogin;
158
    }
159
160
    /**
161
     * Checks the config for feature settings.
162
     *
163
     * @param ConfigInterface $themeConfig
164
     * @param string          $feature
165
     * @return bool
166
     */
167 6
    private function isFeatureSupported(ConfigInterface $themeConfig, $feature)
168
    {
169 6
        return $themeConfig->has('features/'.$feature.'_support')
170 6
            && $themeConfig->getData('features/'.$feature.'_support');
171
    }
172
}
173