Passed
Push — master ( 0d72e8...44be60 )
by Gabor
04:58
created

ServiceAdapter::render()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 12
cts 12
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 8
nop 2
crap 4
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 Twig_Environment;
19
use Twig_Extension_Debug;
20
use Twig_Loader_Filesystem;
21
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface;
22
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
23
use WebHemi\Renderer\ServiceInterface;
24
use WebHemi\Renderer\Traits\GetSelectedThemeResourcePathTrait;
25
26
/**
27
 * Class ServiceAdapter.
28
 */
29
class ServiceAdapter implements ServiceInterface
30
{
31
    /** @var Twig_Environment */
32
    private $adapter;
33
    /** @var string */
34
    private $defaultViewPath;
35
    /** @var string */
36
    private $templateViewPath;
37
    /** @var string */
38
    private $templateResourcePath;
39
    /** @var string */
40
    private $applicationBaseUri;
41
    /** @var array<HelperIterface> */
42
    private $helpers;
43
    /** @var array<FilterInterface> */
44
    private $filters;
45
46
    use GetSelectedThemeResourcePathTrait;
47
48
    /** @var ConfigurationInterface */
49
    protected $configuration;
50
    /** @var EnvironmentInterface */
51
    protected $environmentManager;
52
53
    /**
54
     * ServiceAdapter constructor.
55
     *
56
     * @param ConfigurationInterface $configuration
57
     * @param EnvironmentInterface   $environmentManager
58
     */
59 7
    public function __construct(ConfigurationInterface $configuration, EnvironmentInterface $environmentManager)
60
    {
61 7
        $this->configuration = $configuration;
62 7
        $this->environmentManager = $environmentManager;
63
64 7
        $documentRoot = $environmentManager->getDocumentRoot();
65 7
        $selectedTheme = $environmentManager->getSelectedTheme();
66 7
        $selectedThemeResourcePath = $this->getSelectedThemeResourcePath(
67
            $selectedTheme,
68
            $configuration,
69
            $environmentManager
70
        );
71
72
        // Overwrite for later usage.
73 7
        $this->configuration = $configuration->getConfig('themes/'.$selectedTheme);
74
75 7
        $this->defaultViewPath = $documentRoot.EnvironmentInterface::DEFAULT_THEME_RESOURCE_PATH.'/view';
76 7
        $this->templateViewPath = $documentRoot.$selectedThemeResourcePath.'/view';
77 7
        $this->templateResourcePath = $selectedThemeResourcePath.'/static';
78 7
        $this->applicationBaseUri = $environmentManager->getSelectedApplicationUri();
79
80 7
        $loader = new Twig_Loader_Filesystem($this->templateViewPath);
81 7
        $loader->addPath($this->defaultViewPath, 'WebHemi');
82 7
        $loader->addPath($this->templateViewPath, 'Theme');
83
84 7
        $this->adapter = new Twig_Environment($loader, array('debug' => true, 'cache' => false));
85 7
        $this->adapter->addExtension(new Twig_Extension_Debug());
86 7
    }
87
88
    /**
89
     * Renders the template for the output.
90
     *
91
     * @param string $template
92
     * @param array  $parameters
93
     * @throws InvalidArgumentException
94
     * @return StreamInterface
95
     */
96 4
    public function render(string $template, array $parameters = []) : StreamInterface
97
    {
98
        // @codeCoverageIgnoreStart
99
        //
100
        if (!defined('PHPUNIT_WEBHEMI_TESTSUITE')) {
101
            $this->adapter->addExtension(new TwigExtension($this->helpers, $this->filters));
0 ignored issues
show
Unused Code introduced by
The call to TwigExtension::__construct() has too many arguments starting with $this->helpers.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
102
        }
103
        // @codeCoverageIgnoreEnd
104
105 4
        if ($this->configuration->has('map/'.$template)) {
106 4
            $template = $this->configuration->getData('map/'.$template)[0];
107
        }
108
109 4
        if (!file_exists($this->templateViewPath.'/'.$template)) {
110 3
            throw new InvalidArgumentException(
111
                sprintf(
112 3
                    'Unable to render file: "%s". No such file: %s.',
113
                    $template,
114 3
                    $this->templateViewPath.'/'.$template
115
                )
116
            );
117
        }
118
119
        // Tell the template where the resources are.
120 4
        $parameters['template_resource_path'] = $this->templateResourcePath;
121 4
        $parameters['document_root'] = $this->environmentManager->getDocumentRoot();
122 4
        $parameters['application_base_uri'] = $this->applicationBaseUri;
123
124 4
        $output = $this->adapter->render($template, $parameters);
125
126
        // The ugliest shit ever. But that is how they made it... :/
127 4
        return \GuzzleHttp\Psr7\stream_for($output);
128
    }
129
}
130