Passed
Push — master ( 86c3d2...1ebd3c )
by Gabor
07:26
created

TwigRendererAdapter::checkSelectedThemeFeatures()   B

Complexity

Conditions 9
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 7.756
c 0
b 0
f 0
cc 9
eloc 8
nc 2
nop 1
crap 9
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\Adapter\Renderer\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\Adapter\Renderer\RendererAdapterInterface;
22
use WebHemi\Application\EnvironmentManager;
23
use WebHemi\Config\ConfigInterface;
24
use WebHemi\Renderer\ThemeCheckTrait;
25
26
/**
27
 * Class TwigRendererAdapter.
28
 */
29
class TwigRendererAdapter implements RendererAdapterInterface
30
{
31
    /** @var Twig_Environment */
32
    private $adapter;
33
    /** @var ConfigInterface */
34
    private $configuration;
35
    /** @var EnvironmentManager */
36
    private $environmentManager;
37
    /** @var string */
38
    private $defaultViewPath;
39
    /** @var string */
40
    private $templateViewPath;
41
    /** @var string */
42
    private $templateResourcePath;
43
    /** @var string */
44
    private $applicationBaseUri;
45
46
    use ThemeCheckTrait;
47
48
    /**
49
     * RendererAdapterInterface constructor.
50
     *
51
     * @param ConfigInterface    $configuration
52
     * @param EnvironmentManager $environmentManager
53
     */
54 6
    public function __construct(ConfigInterface $configuration, EnvironmentManager $environmentManager)
55
    {
56 6
        $this->environmentManager = $environmentManager;
57 6
        $documentRoot = $environmentManager->getDocumentRoot();
58 6
        $selectedTheme = $environmentManager->getSelectedTheme();
59 6
        $selectedThemeResourcePath = $environmentManager->getResourcePath();
60
61 6 View Code Duplication
        if (!$configuration->has('themes/'.$selectedTheme)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62 6
            || !$this->checkSelectedThemeFeatures(
63 6
                $configuration->getConfig('themes/'.$selectedTheme),
64
                $environmentManager
65
            )
66
        ) {
67 4
            $selectedTheme = EnvironmentManager::DEFAULT_THEME;
68 4
            $selectedThemeResourcePath = EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH;
69
        }
70
71 6
        $this->configuration = $configuration->getConfig('themes/'.$selectedTheme);
72
73 6
        $this->defaultViewPath = $documentRoot.EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH.'/view';
74 6
        $this->templateViewPath = $documentRoot.$selectedThemeResourcePath.'/view';
75 6
        $this->templateResourcePath = $selectedThemeResourcePath.'/static';
76 6
        $this->applicationBaseUri = $environmentManager->getSelectedApplicationUri();
77
78 6
        $loader = new Twig_Loader_Filesystem($this->templateViewPath);
79 6
        $loader->addPath($this->defaultViewPath, 'WebHemi');
80 6
        $loader->addPath($this->templateViewPath, 'Theme');
81
82 6
        $this->adapter = new Twig_Environment($loader, array('debug' => true, 'cache' => false));
83 6
        $this->adapter->addExtension(new Twig_Extension_Debug());
84
85
        // @codeCoverageIgnoreStart
86
        if (!defined('PHPUNIT_WEBHEMI_TESTSUITE')) {
87
            $this->adapter->addExtension(new TwigExtension());
88
        }
89
        // @codeCoverageIgnoreEnd
90 6
    }
91
92
    /**
93
     * Renders the template for the output.
94
     *
95
     * @param string $template
96
     * @param array  $parameters
97
     * @throws InvalidArgumentException
98
     * @return StreamInterface
99
     */
100 4
    public function render(string $template, array $parameters = []) : StreamInterface
101
    {
102 4
        if ($this->configuration->has('map/'.$template)) {
103 4
            $template = $this->configuration->getData('map/'.$template)[0];
104
        }
105
106 4
        if (!file_exists($this->templateViewPath.'/'.$template)) {
107 3
            throw new InvalidArgumentException(
108
                sprintf(
109 3
                    'Unable to render file: "%s". No such file: %s.',
110
                    $template,
111 3
                    $this->templateViewPath.'/'.$template
112
                )
113
            );
114
        }
115
116
        // Tell the template where the resources are.
117 4
        $parameters['template_resource_path'] = $this->templateResourcePath;
118 4
        $parameters['document_root'] = $this->environmentManager->getDocumentRoot();
119 4
        $parameters['application_base_uri'] = $this->applicationBaseUri;
120
121 4
        $output = $this->adapter->render($template, $parameters);
122
123
        // The ugliest shit ever. But that is how they made it... :/
124 4
        return \GuzzleHttp\Psr7\stream_for($output);
125
    }
126
}
127