Passed
Push — master ( 7e0f83...5c9096 )
by Gabor
03:19
created

TwigRendererAdapter   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 137
Duplicated Lines 11.68 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 16
loc 137
c 0
b 0
f 0
wmc 18
lcom 2
cbo 6
ccs 65
cts 65
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 37 3
B render() 0 25 3
C checkSelectedThemeFeatures() 16 34 12

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 EnvironmentManager */
33
    private $environmentManager;
34
    /** @var string */
35
    private $defaultViewPath;
36
    /** @var string */
37
    private $templateViewPath;
38
    /** @var string */
39
    private $templateResourcePath;
40
    /** @var string */
41
    private $applicationBaseUri;
42
43
    /**
44
     * RendererAdapterInterface constructor.
45
     *
46
     * @param ConfigInterface    $configuration
47
     * @param EnvironmentManager $environmentManager
48
     */
49 6
    public function __construct(ConfigInterface $configuration, EnvironmentManager $environmentManager)
50
    {
51 6
        $this->environmentManager = $environmentManager;
52 6
        $documentRoot = $environmentManager->getDocumentRoot();
53 6
        $selectedTheme = $environmentManager->getSelectedTheme();
54 6
        $selectedThemeResourcePath = $environmentManager->getResourcePath();
55
56 6
        if (!$configuration->has('themes/'.$selectedTheme)
57 6
            || !$this->checkSelectedThemeFeatures($configuration->getConfig('themes/'.$selectedTheme))
58 6
        ) {
59 4
            $selectedTheme = EnvironmentManager::DEFAULT_THEME;
60 4
            $selectedThemeResourcePath = EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH;
61 4
        }
62
63 6
        $this->configuration = $configuration->getConfig('themes/'.$selectedTheme);
64
65 6
        $this->defaultViewPath = $documentRoot.EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH.'/view';
66 6
        $this->templateViewPath = $documentRoot.$selectedThemeResourcePath.'/view';
67 6
        $this->templateResourcePath = $selectedThemeResourcePath.'/static';
68 6
        $this->applicationBaseUri = $environmentManager->getSelectedApplicationUri();
69
70 6
        $loader = new Twig_Loader_Filesystem($this->templateViewPath);
71 6
        $loader->addPath($this->defaultViewPath, 'WebHemi');
72 6
        $loader->addPath($this->templateViewPath, 'Theme');
73 6
        $this->adapter = new Twig_Environment($loader, array('debug' => true, 'cache' => false));
74 6
        $this->adapter->addExtension(new Twig_Extension_Debug());
75
76 6
        $viewPath = $this->templateViewPath;
77
        // @codeCoverageIgnoreStart
78
        // link a core function into template level
79
        $function = new Twig_SimpleFunction('defined', function ($fileName) use ($viewPath) {
80
            $fileName = str_replace('@Theme', $viewPath, $fileName);
81
            return file_exists($fileName);
82
        });
83
        $this->adapter->addFunction($function);
84
        // @codeCoverageIgnoreEnd
85 6
    }
86
87
    /**
88
     * Renders the template for the output.
89
     *
90
     * @param string $template
91
     * @param array  $parameters
92
     *
93
     * @throws InvalidArgumentException
94
     *
95
     * @return StreamInterface
96
     */
97 4
    public function render($template, $parameters = [])
98
    {
99 4
        if ($this->configuration->has('map/'.$template)) {
100 4
            $template = $this->configuration->getData('map/'.$template);
101 4
        }
102
103 4
        if (!file_exists($this->templateViewPath.'/'.$template)) {
104 3
            throw new InvalidArgumentException(
105 3
                sprintf(
106 3
                    'Unable to render file: "%s". No such file: %s.',
107 3
                    $template,
108 3
                    $this->templateViewPath.'/'.$template
109 3
                )
110 3
            );
111
        }
112
113
        // Tell the template where the resources are.
114 4
        $parameters['template_resource_path'] = $this->templateResourcePath;
115 4
        $parameters['application_base_uri'] = $this->applicationBaseUri;
116
117 4
        $output = $this->adapter->render($template, $parameters);
118
119
        // The ugliest shit ever. But that is how they made it... :/
120 4
        return \GuzzleHttp\Psr7\stream_for($output);
121
    }
122
123
    /**
124
     * Checks if the selected theme can be used with the current application.
125
     *
126
     * @param ConfigInterface $themeConfig
127
     * @return bool
128
     */
129 6
    private function checkSelectedThemeFeatures(ConfigInterface $themeConfig)
130
    {
131 6
        $canUseThisTheme = true;
132
133
        // check the theme settings
134
        // If no admin login support, then use default theme
135 6 View Code Duplication
        if ('admin' == $this->environmentManager->getSelectedApplication()
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...
136 6
            && strpos($this->environmentManager->getRequestUri(), '/auth/login') !== false
137 6
            && (!$themeConfig->has('features/admin_login_support')
138 1
                || !$themeConfig->getData('features/admin_login_support')
139 1
            )
140 6
        ) {
141 1
            $canUseThisTheme = false;
142 1
        }
143
144 6 View Code Duplication
        if ('admin' == $this->environmentManager->getSelectedApplication()
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...
145 6
            && strpos($this->environmentManager->getRequestUri(), '/auth/login') === false
146 6
            && (!$themeConfig->has('features/admin_support')
147 1
                || !$themeConfig->getData('features/admin_support')
148 1
            )
149 6
        ) {
150 1
            $canUseThisTheme = false;
151 1
        }
152
153 6
        if ('admin' != $this->environmentManager->getSelectedApplication()
154 6
            && (!$themeConfig->has('features/website_support')
155 4
                || !$themeConfig->getData('features/website_support')
156 4
            )
157 6
        ) {
158 1
            $canUseThisTheme = false;
159 1
        }
160
161 6
        return $canUseThisTheme;
162
    }
163
}
164