Completed
Pull Request — master (#224)
by Cees-Jan
17:12 queued 07:14
created

ExtensionsListener::construct()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 4.0042

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 29
cts 31
cp 0.9355
rs 8.7636
c 0
b 0
f 0
cc 4
crap 4.0042
nc 5
nop 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ExtensionsListener.php$0 ➔ __construct() 0 4 1
A ExtensionsListener.php$0 ➔ load() 0 6 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of TwigView.
6
 *
7
 ** (c) 2014 Cees-Jan Kiewiet
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace WyriHaximus\TwigView\Event;
14
15
use Cake\Core\Configure;
16
use Cake\Event\EventListenerInterface;
17
use Jasny\Twig\ArrayExtension;
18
use Jasny\Twig\DateExtension;
19
use Jasny\Twig\PcreExtension;
20
use Jasny\Twig\TextExtension;
21
use Twig\Extension\DebugExtension;
22
use Twig\Extension\StringLoaderExtension;
23
use Twig\Extra\Markdown\MarkdownExtension;
24
use Twig\Extra\Markdown\MarkdownInterface;
25
use Twig\Extra\Markdown\MarkdownRuntime;
26
use Twig\RuntimeLoader\RuntimeLoaderInterface;
27
use WyriHaximus\TwigView\Lib\Twig\Extension;
28
29
/**
30
 * Class ExtensionsListener.
31
 * @package WyriHaximus\TwigView\Event
32
 */
33
final class ExtensionsListener implements EventListenerInterface
34
{
35
    /**
36
     * Return implemented events.
37
     *
38
     * @return array
39
     */
40
    public function implementedEvents(): array
41
    {
42 14
        return [
43
            ConstructEvent::EVENT => 'construct',
44
        ];
45 14
    }
46
47
    /**
48
     * Event handler.
49
     *
50
     * @param \WyriHaximus\TwigView\Event\ConstructEvent $event Event.
51
     */
52
    public function construct(ConstructEvent $event)
53
    {
54 5
        if ($event->getTwig()->hasExtension(StringLoaderExtension::class)) {
55
            return;
56 5
        }
57
58
        // Twig core extensions
59
        $event->getTwig()->addExtension(new StringLoaderExtension());
60
        $event->getTwig()->addExtension(new DebugExtension());
61 5
62 5
        // CakePHP bridging extensions
63
        $event->getTwig()->addExtension(new Extension\I18n());
64
        $event->getTwig()->addExtension(new Extension\Time());
65 5
        $event->getTwig()->addExtension(new Extension\Basic());
66 5
        $event->getTwig()->addExtension(new Extension\Number());
67 5
        $event->getTwig()->addExtension(new Extension\Utils());
68 5
        $event->getTwig()->addExtension(new Extension\Arrays());
69 5
        $event->getTwig()->addExtension(new Extension\Strings());
70 5
        $event->getTwig()->addExtension(new Extension\Inflector());
71 5
72 5
        if (
73
            !Configure::check('WyriHaximus.TwigView.flags.potentialDangerous') ||
74
            (
75 5
                Configure::check('WyriHaximus.TwigView.flags.potentialDangerous') &&
76
                Configure::read('WyriHaximus.TwigView.flags.potentialDangerous') === true
77
            )
78 5
        ) {
79
            $event->getTwig()->addExtension(new Extension\PotentialDangerous());
80
        }
81 5
82
        // Markdown extension
83
        if (
84
            Configure::check('WyriHaximus.TwigView.markdown.engine') &&
85
            Configure::read('WyriHaximus.TwigView.markdown.engine') instanceof MarkdownInterface
86 5
        ) {
87 5
            $engine = Configure::read('WyriHaximus.TwigView.markdown.engine');
88
            $event->getTwig()->addExtension(new MarkdownExtension());
89 1
90 1
            $event->getTwig()->addRuntimeLoader(new class ($engine) implements RuntimeLoaderInterface {
91 1
                /**
92
                 * @var \Twig\Extra\Markdown\MarkdownInterface
93
                 */
94
                private $engine;
95 5
96 5
                public function __construct(MarkdownInterface $engine)
97 5
                {
98 5
                    $this->engine = $engine;
99
                }
100
101 5
                public function load($class)
102 5
                {
103 5
                    if ($class === MarkdownRuntime::class) {
104 5
                        return new MarkdownRuntime($this->engine);
105
                    }
106 5
                }
107
            });
108
        }
109
110
        // jasny/twig-extensions
111
        $event->getTwig()->addExtension(new DateExtension());
112
        $event->getTwig()->addExtension(new PcreExtension());
113
        $event->getTwig()->addExtension(new TextExtension());
114
        $event->getTwig()->addExtension(new ArrayExtension());
115
        // @codingStandardsIgnoreEnd
116
    }
117
}
118