Test Failed
Push — master ( 6cdaaa...eddc8c )
by Gabor
03:25
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 5
    public function __construct(
62
        ConfigurationInterface $configuration,
63
        EnvironmentInterface $environmentManager,
64
        I18nService $i18nService
65
    ) {
66 5
        $this->configuration = $configuration;
67 5
        $this->environmentManager = $environmentManager;
68 5
        $this->i18nService = $i18nService;
69
70 5
        $documentRoot = $environmentManager->getDocumentRoot();
71 5
        $selectedTheme = $environmentManager->getSelectedTheme();
72 5
        $selectedThemeResourcePath = $this->getSelectedThemeResourcePath(
73 5
            $selectedTheme,
74 5
            $configuration,
75 5
            $environmentManager
76
        );
77
78
        // Overwrite for later usage.
79 5
        $this->configuration = $configuration->getConfig('themes/'.$selectedTheme);
80
81 5
        $this->defaultViewPath = $documentRoot.EnvironmentInterface::DEFAULT_THEME_RESOURCE_PATH.'/view';
82 5
        $this->templateViewPath = $documentRoot.$selectedThemeResourcePath.'/view';
83 5
        $this->templateResourcePath = $selectedThemeResourcePath.'/static';
84 5
        $this->applicationBaseUri = $environmentManager->getSelectedApplicationUri();
85
86 5
        $loader = new Twig_Loader_Filesystem($this->templateViewPath);
87 5
        $loader->addPath($this->defaultViewPath, 'WebHemi');
88 5
        $loader->addPath($this->templateViewPath, 'Theme');
89
90 5
        $this->adapter = new Twig_Environment($loader, array('debug' => true, 'cache' => false));
91 5
        $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 5
    }
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 2
    public function render(string $template, array $parameters = []) : StreamInterface
109
    {
110 2
        if ($this->configuration->has('map/'.$template)) {
111 2
            $template = $this->configuration->getData('map/'.$template)[0];
112
        }
113
114 2
        if (!file_exists($this->templateViewPath.'/'.$template)) {
115
            throw new InvalidArgumentException(
116
                sprintf(
117
                    'Unable to render file: "%s". No such file: %s.',
118
                    $template,
119
                    $this->templateViewPath.'/'.$template
120
                )
121
            );
122
        }
123
124
        // Tell the template where the resources are.
125
        $parameters['application'] = [
126 2
            'address' => $this->environmentManager->getAddress(),
127
            'domainName' => $this->environmentManager->getApplicationDomain(),
128
            'domainAddress' => 'http'.($this->environmentManager->isSecuredApplication() ? 's' : '').'://'
129
                .$this->environmentManager->getApplicationDomain(),
130
            'resourcePath' => $this->templateResourcePath,
131
            'baseUri' => $this->applicationBaseUri,
132
            'currentUri' => $this->environmentManager->getRequestUri(),
133
            'documentRoot' => $this->environmentManager->getDocumentRoot(),
134
            'language' => $this->i18nService->getLanguage(),
135
            'locale' => $this->i18nService->getLocale(),
136
            'author' => '',
137
            'authorLink' => '',
138
            'description' => '',
139
            'subject' => '',
140
            'copyright' => ''
141
        ];
142
143
        $output = $this->adapter->render($template, $parameters);
144
145
        // The ugliest shit ever. But that is how they made it... :/
146
        return \GuzzleHttp\Psr7\stream_for($output);
147
    }
148
}
149