Completed
Push — feature-markdown ( c8929c...6d3c30 )
by Cees-Jan
02:53
created

TwigView   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 271
Duplicated Lines 11.07 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 72.82%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 10
dl 30
loc 271
ccs 67
cts 92
cp 0.7282
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 2
A readConfig() 0 13 3
A unshiftExtension() 0 4 1
A getLoader() 0 6 1
A generateHelperList() 0 11 2
B _render() 0 37 6
A _getViewFileName() 15 15 3
A _getLayoutFileName() 15 15 3
A _getElementFileName() 0 13 3
A getTwig() 0 4 1
A resolveConfig() 0 15 1

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
/**
4
 * This file is part of TwigView.
5
 *
6
 ** (c) 2014 Cees-Jan Kiewiet
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace WyriHaximus\TwigView\View;
12
13
use Cake\Core\Configure;
14
use Cake\Event\EventManager;
15
use Cake\Network\Request;
16
use Cake\Network\Response;
17
use Cake\View\View;
18
use Exception;
19
use WyriHaximus\TwigView\Event\ConstructEvent;
20
use WyriHaximus\TwigView\Event\EnvironmentConfigEvent;
21
use WyriHaximus\TwigView\Event\LoaderEvent;
22
use WyriHaximus\TwigView\Lib\Twig\Loader;
23
24
/**
25
 * Class TwigView
26
 * @package WyriHaximus\TwigView\View
27
 */
28
// @codingStandardsIgnoreStart
29
class TwigView extends View
30
// @codingStandardsIgnoreEnd
31
{
32
    const EXT = '.twig';
33
34
    const ENV_CONFIG = 'WyriHaximus.TwigView.environment';
35
36
    /**
37
     * Extension to use.
38
     *
39
     * @var string
40
     */
41
    // @codingStandardsIgnoreStart
42
    protected $_ext = self::EXT;
43
    // @codingStandardsIgnoreEnd
44
45
    /**
46
     * @var array
47
     */
48
    protected $extensions = [
49
        self::EXT,
50
        '.tpl',
51
        '.ctp',
52
    ];
53
54
    /**
55
     * Twig instance.
56
     *
57
     * @var \Twig_Environment
58
     */
59
    protected $twig;
60
61
    /**
62
     * Helpers.
63
     *
64
     * @var array
65
     */
66
    protected $helperList = [];
67
68
    /**
69
     * Event manager.
70
     *
71
     * @var EventManager
72
     */
73
    protected $eventManager;
74
75
    /**
76
     * Constructor.
77
     *
78
     * @param Request      $request      Request.
79
     * @param Response     $response     Response.
80
     * @param EventManager $eventManager EventManager.
81
     * @param array        $viewOptions  View options.
82
     */
83 10
    public function __construct(
84
        Request $request = null,
85
        Response $response = null,
86
        EventManager $eventManager = null,
87
        array $viewOptions = []
88
    ) {
89 10
        if ($eventManager === null) {
90 9
            $eventManager = EventManager::instance();
91 9
        }
92 10
        $this->eventManager = $eventManager;
93
94 10
        $this->twig = new \Twig_Environment($this->getLoader(), $this->resolveConfig());
95
96 10
        $this->eventManager->dispatch(ConstructEvent::create($this, $this->twig));
97
98 10
        parent::__construct($request, $response, $eventManager, $viewOptions);
99 10
        $this->_ext = self::EXT;
100
101 10
        $this->generateHelperList();
102 10
    }
103
104
    /**
105
     * @return array
106
     */
107 10
    protected function resolveConfig()
108
    {
109
        $config = [
110 10
            'cache' => CACHE . 'twigView' . DS,
111 10
            'charset' => strtolower(Configure::read('App.encoding')),
112 10
            'auto_reload' => Configure::read('debug'),
113 10
            'debug' => Configure::read('debug'),
114 10
        ];
115
116 10
        $config = array_replace($config, $this->readConfig());
117
118 10
        $configEvent = EnvironmentConfigEvent::create($config);
119 10
        $this->eventManager->dispatch($configEvent);
120 10
        return $configEvent->getConfig();
121 1
    }
122
123
    /**
124
     * @return array
125
     */
126 10
    protected function readConfig()
127
    {
128 10
        if (!Configure::check(static::ENV_CONFIG)) {
129 9
            return [];
130
        }
131
132 1
        $config = Configure::read(static::ENV_CONFIG);
133 1
        if (!is_array($config)) {
134
            return [];
135
        }
136
137 1
        return $config;
138
    }
139
140
    /**
141
     * @param string $extension
142
     */
143
    public function unshiftExtension($extension)
144
    {
145
        array_unshift($this->extensions, $extension);
146
    }
147
148
    /**
149
     * Create the template loader.
150
     *
151
     * @return \Twig_LoaderInterface
152
     */
153 10
    protected function getLoader()
154
    {
155 10
        $event = LoaderEvent::create(new Loader());
156 10
        $this->eventManager->dispatch($event);
157 10
        return $event->getResultLoader();
158
    }
159
160
    /**
161
     * Create a useful helper list.
162
     *
163
     * @return void
164
     */
165 10
    protected function generateHelperList()
166
    {
167 10
        $registry = $this->helpers();
168
        
169 10
        $helpersList = array_merge($this->helpers, $registry->loaded());
170 10
        $helpers = $registry->normalizeArray($helpersList);
171 10
        foreach ($helpers as $properties) {
172 3
            list(, $class) = pluginSplit($properties['class']);
173 3
            $this->helperList[$class] = $this->{$class};
174 10
        }
175 10
    }
176
177
    /**
178
     * Render the template.
179
     *
180
     * @param string $viewFile Template file.
181
     * @param array  $data     Data that can be used by the template.
182
     *
183
     * @throws \Exception
184
     * @return string
185
     */
186
    // @codingStandardsIgnoreStart
187 4
    protected function _render($viewFile, $data = array())
188
    {
189
        // @codingStandardsIgnoreEnd
190 4
        if (empty($data)) {
191 4
            $data = $this->viewVars;
192 4
        }
193
194 4
        if (substr($viewFile, -3) === 'ctp') {
195 1
            $out = parent::_render($viewFile, $data);
196
            // @codingStandardsIgnoreStart
197 1
        } else {
198
            // @codingStandardsIgnoreEnd
199 3
            $data = array_merge(
200 3
                $data,
201 3
                $this->helperList,
202
                [
203 3
                    '_view' => $this,
204
                ]
205 3
            );
206
207
            // @codingStandardsIgnoreStart
208
            try {
209 3
                $out = $this->getTwig()->loadTemplate($viewFile)->render($data);
210 3
            } catch (Exception $e) {
211 2
                $previous = $e->getPrevious();
212
213 2
                if ($previous !== null && $previous instanceof Exception) {
214 1
                    throw $previous;
215
                } else {
216 1
                    throw $e;
217
                }
218
            }
219
            // @codingStandardsIgnoreEnd
220
        }
221
222 2
        return $out;
223
    }
224
225
    /**
226
     * @param string|null $name
227
     * @return string
228
     * @throws \Exception
229
     */
230
    // @codingStandardsIgnoreStart
231 2 View Code Duplication
    protected function _getViewFileName($name = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
232
    {
233
        // @codingStandardsIgnoreEnd
234 2
        $rethrow = new \Exception('You\'re not supposed to get here');
235 2
        foreach ($this->extensions as $extension) {
236 2
            $this->_ext = $extension;
237
            try {
238 2
                return parent::_getViewFileName($name);
239
            } catch (\Exception $exception) {
240
                $rethrow = $exception;
241
            }
242
        }
243
244
        throw $rethrow;
245
    }
246
247
    /**
248
     * @param string|null $name
249
     * @return string
250
     * @throws \Exception
251
     */
252
    // @codingStandardsIgnoreStart
253 View Code Duplication
    protected function _getLayoutFileName($name = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
254
    {
255
        // @codingStandardsIgnoreEnd
256
        $rethrow = new \Exception('You\'re not supposed to get here');
257
        foreach ($this->extensions as $extension) {
258
            $this->_ext = $extension;
259
            try {
260
                return parent::_getLayoutFileName($name);
261
            } catch (\Exception $exception) {
262
                $rethrow = $exception;
263
            }
264
        }
265
266
        throw $rethrow;
267
    }
268
269
    /**
270
     * @param string $name
271
     * @param bool $pluginCheck
272
     * @return string
273
     * @throws \Exception
274
     */
275
    // @codingStandardsIgnoreStart
276
    protected function _getElementFileName($name, $pluginCheck = true)
277
    {
278
        // @codingStandardsIgnoreEnd
279
        foreach ($this->extensions as $extension) {
280
            $this->_ext = $extension;
281
            $result = parent::_getElementFileName($name, $pluginCheck);
282
            if ($result !== false) {
283
                return $result;
284
            }
285
        }
286
287
        return false;
288
    }
289
290
    /**
291
     * Get twig environment instance.
292
     *
293
     * @return \Twig_Environment
294
     */
295 3
    public function getTwig()
296
    {
297 3
        return $this->twig;
298
    }
299
}
300