Passed
Push — master ( b01812...3926c4 )
by Gabor
03:44
created

TwigRendererAdapter::render()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 16
cts 16
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 2
crap 3
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 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
namespace WebHemi\Adapter\Renderer\Twig;
13
14
use InvalidArgumentException;
15
use Psr\Http\Message\StreamInterface;
16
use Twig_Environment;
17
use Twig_Extension_Debug;
18
use Twig_Loader_Filesystem;
19
use Twig_SimpleFunction;
20
use WebHemi\Adapter\Renderer\RendererAdapterInterface;
21
use WebHemi\Application\EnvironmentManager;
22
use WebHemi\Config\ConfigInterface;
23
24
/**
25
 * Class TwigRendererAdapter.
26
 */
27
class TwigRendererAdapter implements RendererAdapterInterface
28
{
29
    private $adapter;
30
    /** @var ConfigInterface */
31
    private $configuration;
32
    /** @var string */
33
    private $defaultViewPath;
34
    /** @var string */
35
    private $templateViewPath;
36
    /** @var string */
37
    private $templateResourcePath;
38
    /** @var string */
39
    private $applicationBaseUri;
40
41
    /**
42
     * RendererAdapterInterface constructor.
43
     *
44
     * @param ConfigInterface    $configuration
45
     * @param EnvironmentManager $environmentManager
46
     */
47 4
    public function __construct(ConfigInterface $configuration, EnvironmentManager $environmentManager)
48
    {
49 4
        $documentRoot = $environmentManager->getDocumentRoot();
50 4
        $selectedTheme = $environmentManager->getSelectedTheme();
51 4
        $selectedThemeResourcePath = $environmentManager->getResourcePath();
52
53 4
        if (!$configuration->has('themes/'.$selectedTheme)) {
54 1
            $selectedTheme = EnvironmentManager::DEFAULT_THEME;
55 1
            $selectedThemeResourcePath = EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH;
56 1
        }
57
58 4
        $this->configuration = $configuration->getConfig('themes/'.$selectedTheme);
59
60 4
        $this->defaultViewPath = $documentRoot.EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH.'/view';
61 4
        $this->templateViewPath = $documentRoot.$selectedThemeResourcePath.'/view';
62 4
        $this->templateResourcePath = $selectedThemeResourcePath.'/static';
63 4
        $this->applicationBaseUri = $environmentManager->getSelectedApplicationUri();
64
65 4
        $loader = new Twig_Loader_Filesystem($this->templateViewPath);
66 4
        $loader->addPath($this->defaultViewPath, 'WebHemi');
67 4
        $loader->addPath($this->templateViewPath, 'Theme');
68 4
        $this->adapter = new Twig_Environment($loader, array('debug' => true, 'cache' => false));
69 4
        $this->adapter->addExtension(new Twig_Extension_Debug());
70
71 4
        $viewPath = $this->templateViewPath;
72
        // @codeCoverageIgnoreStart
73
        // link a core function into template level
74
        $function = new Twig_SimpleFunction('defined', function ($fileName) use ($viewPath) {
75
            $fileName = str_replace('@Theme', $viewPath, $fileName);
76
            return file_exists($fileName);
77
        });
78
        $this->adapter->addFunction($function);
79
        // @codeCoverageIgnoreEnd
80 4
    }
81
82
    /**
83
     * Renders the template for the output.
84
     *
85
     * @param string $template
86
     * @param array  $parameters
87
     *
88
     * @throws InvalidArgumentException
89
     *
90
     * @return StreamInterface
91
     */
92 2
    public function render($template, $parameters = [])
93
    {
94 2
        if ($this->configuration->has('map/'.$template)) {
95 2
            $template = $this->configuration->getData('map/'.$template);
96 2
        }
97
98 2
        if (!file_exists($this->templateViewPath.'/'.$template)) {
99 1
            throw new InvalidArgumentException(
100 1
                sprintf(
101 1
                    'Unable to render file: "%s". No such file: %s.',
102 1
                    $template,
103 1
                    $this->templateViewPath.'/'.$template
104 1
                )
105 1
            );
106
        }
107
108
        // Tell the template where the resources are.
109 2
        $parameters['template_resource_path'] = $this->templateResourcePath;
110 2
        $parameters['application_base_uri'] = $this->applicationBaseUri;
111
112 2
        $output = $this->adapter->render($template, $parameters);
113
114
        // The ugliest shit ever. But that is how they made it... :/
115 2
        return \GuzzleHttp\Psr7\stream_for($output);
116
    }
117
}
118