Completed
Push — master ( 0164d4...9f5a90 )
by Cees-Jan
01:55
created

ExtensionsListener::construct()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 6.0014

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 28
cts 29
cp 0.9655
rs 8.4905
c 0
b 0
f 0
cc 6
nc 4
nop 1
crap 6.0014
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of TwigView.
4
 *
5
 ** (c) 2014 Cees-Jan Kiewiet
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace WyriHaximus\TwigView\Event;
12
13
use Aptoma\Twig\Extension\MarkdownEngineInterface;
14
use Aptoma\Twig\Extension\MarkdownExtension;
15
use Aptoma\Twig\TokenParser\MarkdownTokenParser;
16
use Asm89\Twig\CacheExtension\CacheStrategy\LifetimeCacheStrategy;
17
use Asm89\Twig\CacheExtension\Extension as CacheExtension;
18
use Cake\Core\Configure;
19
use Cake\Event\EventListenerInterface;
20
use Jasny\Twig\ArrayExtension;
21
use Jasny\Twig\DateExtension;
22
use Jasny\Twig\PcreExtension;
23
use Jasny\Twig\TextExtension;
24
use Twig\Extension\DebugExtension;
25
use Twig\Extension\StringLoaderExtension;
26
use WyriHaximus\TwigView\Lib\Cache;
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 1
    public function implementedEvents(): array
41
    {
42
        return [
43 1
            ConstructEvent::EVENT => 'construct',
44
        ];
45
    }
46
47
    /**
48
     * Event handler.
49
     *
50
     * @param ConstructEvent $event Event.
51
     */
52 5
    public function construct(ConstructEvent $event)
53
    {
54
        // Twig core extensions
55 5
        $event->getTwig()->addExtension(new StringLoaderExtension());
56 5
        $event->getTwig()->addExtension(new DebugExtension());
57
58
        // CakePHP bridging extensions
59 5
        $event->getTwig()->addExtension(new Extension\I18n());
60 5
        $event->getTwig()->addExtension(new Extension\Time());
61 5
        $event->getTwig()->addExtension(new Extension\Basic());
62 5
        $event->getTwig()->addExtension(new Extension\Number());
63 5
        $event->getTwig()->addExtension(new Extension\Utils());
64 5
        $event->getTwig()->addExtension(new Extension\Arrays());
65 5
        $event->getTwig()->addExtension(new Extension\Strings());
66 5
        $event->getTwig()->addExtension(new Extension\Inflector());
67
68
        if (
69 5
            !Configure::check('WyriHaximus.TwigView.flags.potentialDangerous') ||
70
            (
71
                Configure::check('WyriHaximus.TwigView.flags.potentialDangerous') &&
72 5
                Configure::read('WyriHaximus.TwigView.flags.potentialDangerous') === true
73
            )
74
        ) {
75 5
            $event->getTwig()->addExtension(new Extension\PotentialDangerous());
76
        }
77
78
        // Markdown extension
79
        if (
80 5
            Configure::check('WyriHaximus.TwigView.markdown.engine') &&
81 5
            Configure::read('WyriHaximus.TwigView.markdown.engine') instanceof MarkdownEngineInterface
82
        ) {
83 1
            $engine = Configure::read('WyriHaximus.TwigView.markdown.engine');
84 1
            $event->getTwig()->addExtension(new MarkdownExtension($engine));
85 1
            $event->getTwig()->addTokenParser(new MarkdownTokenParser($engine));
0 ignored issues
show
Unused Code introduced by
The call to MarkdownTokenParser::__construct() has too many arguments starting with $engine.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
86
        }
87
88
        // Third party cache extension
89 5
        $cacheProvider = new Cache();
90 5
        $cacheStrategy = new LifetimeCacheStrategy($cacheProvider);
91 5
        $cacheExtension = new CacheExtension($cacheStrategy);
92 5
        $event->getTwig()->addExtension($cacheExtension);
93
94
        // jasny/twig-extensions
95 5
        $event->getTwig()->addExtension(new DateExtension());
96 5
        $event->getTwig()->addExtension(new PcreExtension());
97 5
        $event->getTwig()->addExtension(new TextExtension());
98 5
        $event->getTwig()->addExtension(new ArrayExtension());
99
        // @codingStandardsIgnoreEnd
100 5
    }
101
}
102