Passed
Push — master ( bf3918...698edc )
by Gabor
12:58 queued 06:03
created

ServiceAdapter::__construct()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 21
cts 21
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 25
nc 2
nop 3
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\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\I18n\ServiceInterface as I18nService;
25
use WebHemi\Renderer\ServiceInterface;
26
use WebHemi\Renderer\Traits\GetSelectedThemeResourcePathTrait;
27
28
/**
29
 * Class ServiceAdapter.
30
 */
31
class ServiceAdapter implements ServiceInterface
32
{
33
    /** @var Twig_Environment */
34
    private $adapter;
35
    /** @var string */
36
    private $defaultViewPath;
37
    /** @var string */
38
    private $templateViewPath;
39
    /** @var string */
40
    private $templateResourcePath;
41
    /** @var string */
42
    private $applicationBaseUri;
43
44
    use GetSelectedThemeResourcePathTrait;
45
46
    /** @var ConfigurationInterface */
47
    protected $configuration;
48
    /** @var EnvironmentInterface */
49
    protected $environmentManager;
50
    /** @var I18nService */
51
    protected $i18nService;
52
53
    /**
54
     * ServiceAdapter constructor.
55
     *
56
     * @param ConfigurationInterface $configuration
57
     * @param EnvironmentInterface   $environmentManager
58
     * @param I18nService            $i18nService
59
     * @throws Throwable
60
     */
61 7
    public function __construct(
62
        ConfigurationInterface $configuration,
63
        EnvironmentInterface $environmentManager,
64
        I18nService $i18nService
65
    ) {
66 7
        $this->configuration = $configuration;
67 7
        $this->environmentManager = $environmentManager;
68 7
        $this->i18nService = $i18nService;
69
70 7
        $documentRoot = $environmentManager->getDocumentRoot();
71 7
        $selectedTheme = $environmentManager->getSelectedTheme();
72 7
        $selectedThemeResourcePath = $this->getSelectedThemeResourcePath(
73 7
            $selectedTheme,
74 7
            $configuration,
75 7
            $environmentManager
76
        );
77
78
        // Overwrite for later usage.
79 7
        $this->configuration = $configuration->getConfig('themes/'.$selectedTheme);
80
81 7
        $this->defaultViewPath = $documentRoot.EnvironmentInterface::DEFAULT_THEME_RESOURCE_PATH.'/view';
82 7
        $this->templateViewPath = $documentRoot.$selectedThemeResourcePath.'/view';
83 7
        $this->templateResourcePath = $selectedThemeResourcePath.'/static';
84 7
        $this->applicationBaseUri = $environmentManager->getSelectedApplicationUri();
85
86 7
        $loader = new Twig_Loader_Filesystem($this->templateViewPath);
87 7
        $loader->addPath($this->defaultViewPath, 'WebHemi');
88 7
        $loader->addPath($this->templateViewPath, 'Theme');
89
90 7
        $this->adapter = new Twig_Environment($loader, array('debug' => true, 'cache' => false));
91 7
        $this->adapter->addExtension(new Twig_Extension_Debug());
92
93
        // @codeCoverageIgnoreStart
94
        if (!defined('PHPUNIT_WEBHEMI_TESTSUITE')) {
95
            $this->adapter->addExtension(new TwigExtension());
96
        }
97
        // @codeCoverageIgnoreEnd
98 7
    }
99
100
    /**
101
     * Renders the template for the output.
102
     *
103
     * @param string $template
104
     * @param array  $parameters
105
     * @throws Throwable
106
     * @return StreamInterface
107
     */
108 4
    public function render(string $template, array $parameters = []) : StreamInterface
109
    {
110 4
        if ($this->configuration->has('map/'.$template)) {
111 4
            $template = $this->configuration->getData('map/'.$template)[0];
112
        }
113
114 4
        if (!file_exists($this->templateViewPath.'/'.$template)) {
115 3
            throw new InvalidArgumentException(
116 3
                sprintf(
117 3
                    'Unable to render file: "%s". No such file: %s.',
118 3
                    $template,
119 3
                    $this->templateViewPath.'/'.$template
120
                )
121
            );
122
        }
123
124
        // Merge mandatory Application data.
125 4
        $applicationParams['application'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$applicationParams was never initialized. Although not strictly required by PHP, it is generally a good practice to add $applicationParams = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
126 4
            'selectedModule' => $this->environmentManager->getSelectedModule(),
127 4
            'address' => $this->environmentManager->getAddress(),
128 4
            'domainName' => $this->environmentManager->getApplicationDomain(),
129 4
            'domainAddress' => 'http'.($this->environmentManager->isSecuredApplication() ? 's' : '').'://'
130 4
                .$this->environmentManager->getApplicationDomain(),
131 4
            'resourcePath' => $this->templateResourcePath,
132 4
            'baseUri' => $this->applicationBaseUri,
133 4
            'currentUri' => $this->environmentManager->getRequestUri(),
134 4
            'documentRoot' => $this->environmentManager->getDocumentRoot(),
135 4
            'language' => $this->i18nService->getLanguage(),
136 4
            'locale' => $this->i18nService->getLocale(),
137
        ];
138
139 4
        $parameters = merge_array_overwrite($parameters, $applicationParams);
140
141 4
        $output = $this->adapter->render($template, $parameters);
142
143
        // The ugliest shit ever. But that is how they made it... :/
144 4
        return \GuzzleHttp\Psr7\stream_for($output);
145
    }
146
}
147