Passed
Push — master ( 7ed858...861abc )
by Gabor
02:38
created

ServiceAdapter::render()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 24
nc 6
nop 2
dl 0
loc 39
ccs 23
cts 23
cp 1
crap 4
rs 9.536
c 0
b 0
f 0
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2018 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\ServiceAdapter\Twig;
15
16
use InvalidArgumentException;
17
use Psr\Http\Message\StreamInterface;
18
use Throwable;
19
use Twig_Environment;
20
use Twig_Extension_Debug;
21
use Twig_Loader_Filesystem;
22
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface;
23
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
24
use WebHemi\GeneralLib;
25
use WebHemi\I18n\ServiceInterface as I18nService;
26
use WebHemi\Renderer\ServiceInterface;
27
use WebHemi\Renderer\Traits\GetSelectedThemeResourcePathTrait;
28
29
/**
30
 * Class ServiceAdapter.
31
 */
32
class ServiceAdapter implements ServiceInterface
33
{
34
    /**
35
     * @var Twig_Environment
36
     */
37
    private $adapter;
38
    /**
39
     * @var string
40
     */
41
    private $defaultViewPath;
42
    /**
43
     * @var string
44
     */
45
    private $templateViewPath;
46
    /**
47
     * @var string
48
     */
49
    private $templateResourcePath;
50
    /**
51
     * @var string
52
     */
53
    private $applicationBaseUri;
54
55
    use GetSelectedThemeResourcePathTrait;
56
57
    /**
58
     * @var ConfigurationInterface
59
     */
60
    protected $configuration;
61
    /**
62
     * @var EnvironmentInterface
63
     */
64
    protected $environmentManager;
65
    /**
66
     * @var I18nService
67
     */
68
    protected $i18nService;
69
70
    /**
71
     * ServiceAdapter constructor.
72
     *
73
     * @param  ConfigurationInterface $configuration
74
     * @param  EnvironmentInterface   $environmentManager
75
     * @param  I18nService            $i18nService
76
     * @throws Throwable
77
     */
78 7
    public function __construct(
79
        ConfigurationInterface $configuration,
80
        EnvironmentInterface $environmentManager,
81
        I18nService $i18nService
82
    ) {
83 7
        $this->configuration = $configuration;
84 7
        $this->environmentManager = $environmentManager;
85 7
        $this->i18nService = $i18nService;
86
87 7
        $documentRoot = $environmentManager->getDocumentRoot();
88 7
        $selectedTheme = $environmentManager->getSelectedTheme();
89 7
        $selectedThemeResourcePath = $this->getSelectedThemeResourcePath(
90 7
            $selectedTheme,
91 7
            $configuration,
92 7
            $environmentManager
93
        );
94
95
        // Overwrite for later usage.
96 7
        $this->configuration = $configuration->getConfig('themes/'.$selectedTheme);
97
98 7
        $this->defaultViewPath = $documentRoot.EnvironmentInterface::DEFAULT_THEME_RESOURCE_PATH.'/view';
99 7
        $this->templateViewPath = $documentRoot.$selectedThemeResourcePath.'/view';
100 7
        $this->templateResourcePath = $selectedThemeResourcePath.'/static';
101 7
        $this->applicationBaseUri = $environmentManager->getSelectedApplicationUri();
102
103 7
        $loader = new Twig_Loader_Filesystem($this->templateViewPath);
104 7
        $loader->addPath($this->defaultViewPath, 'WebHemi');
105 7
        $loader->addPath($this->templateViewPath, 'Theme');
106
107 7
        $this->adapter = new Twig_Environment($loader, array('debug' => true, 'cache' => false));
108 7
        $this->adapter->addExtension(new Twig_Extension_Debug());
109
110
        // @codeCoverageIgnoreStart
111
        if (!defined('PHPUNIT_WEBHEMI_TESTSUITE')) {
112
            $this->adapter->addExtension(new TwigExtension());
113
        }
114
        // @codeCoverageIgnoreEnd
115 7
    }
116
117
    /**
118
     * Renders the template for the output.
119
     *
120
     * @param  string $template
121
     * @param  array  $parameters
122
     * @throws Throwable
123
     * @return StreamInterface
124
     */
125 3
    public function render(string $template, array $parameters = []) : StreamInterface
126
    {
127 3
        if ($this->configuration->has('map/'.$template)) {
128 3
            $template = $this->configuration->getData('map/'.$template)[0];
129
        }
130
131 3
        if (!file_exists($this->templateViewPath.'/'.$template)) {
132 3
            throw new InvalidArgumentException(
133 3
                sprintf(
134 3
                    'Unable to render file: "%s". No such file: %s.',
135 3
                    $template,
136 3
                    $this->templateViewPath.'/'.$template
137
                )
138
            );
139
        }
140
141
        // Merge mandatory Application data.
142
        $applicationParams = [
143
            'WebHemiApplication' => [
144 3
                'selectedModule' => $this->environmentManager->getSelectedModule(),
145 3
                'address' => $this->environmentManager->getAddress(),
146 3
                'topDomain' => $this->environmentManager->getTopDomain(),
147 3
                'domainName' => $this->environmentManager->getApplicationDomain(),
148 3
                'domainAddress' => 'http'.($this->environmentManager->isSecuredApplication() ? 's' : '').'://'
149 3
                    .$this->environmentManager->getApplicationDomain(),
150 3
                'resourcePath' => $this->templateResourcePath,
151 3
                'baseUri' => $this->applicationBaseUri,
152 3
                'currentUri' => $this->environmentManager->getRequestUri(),
153 3
                'language' => $this->i18nService->getLanguage(),
154 3
                'locale' => $this->i18nService->getLocale(),
155
            ],
156
        ];
157
158 3
        $parameters = GeneralLib::mergeArrayOverwrite($parameters, $applicationParams);
159
160 3
        $output = $this->adapter->render($template, $parameters);
161
162
        // The ugliest shit ever. But that is how they made it... :/
163 3
        return \GuzzleHttp\Psr7\stream_for($output);
164
    }
165
}
166