Passed
Push — fallback-intl ( 5d6f9b )
by Arnaud
04:57
created

Twig::__construct()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 52
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 33
c 3
b 0
f 0
nc 16
nop 2
dl 0
loc 52
rs 8.7697

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
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
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 Cecil\Renderer;
12
13
use Cecil\Builder;
14
use Cecil\Renderer\Twig\Extension as TwigExtension;
15
16
/**
17
 * Class Twig.
18
 */
19
class Twig implements RendererInterface
20
{
21
    /** @var \Twig\Environment */
22
    protected $twig;
23
24
    /** @var string */
25
    protected $templatesDir;
26
27
    /** @var \Twig\Profiler\Profile */
28
    public $profile;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function __construct(Builder $builder, $templatesPath)
34
    {
35
        // load layouts
36
        $loader = new \Twig\Loader\FilesystemLoader($templatesPath);
37
        // default options
38
        $loaderOptions = [
39
            'debug'            => $builder->isDebug(),
40
            'strict_variables' => true,
41
            'autoescape'       => false,
42
            'auto_reload'      => true,
43
            'cache'            => false,
44
        ];
45
        // use Twig cache?
46
        if ($builder->getConfig()->get('cache.templates.enabled')) {
47
            $templatesCachePath = \Cecil\Util::joinFile(
48
                $builder->getConfig()->getCachePath(),
49
                (string) $builder->getConfig()->get('cache.templates.dir')
50
            );
51
            $loaderOptions = array_replace($loaderOptions, ['cache' => $templatesCachePath]);
52
        }
53
        // create the Twig instance
54
        $this->twig = new \Twig\Environment($loader, $loaderOptions);
55
        // set date format & timezone
56
        $this->twig->getExtension(\Twig\Extension\CoreExtension::class)
57
            ->setDateFormat($builder->getConfig()->get('date.format'));
58
        $this->twig->getExtension(\Twig\Extension\CoreExtension::class)
59
            ->setTimezone($builder->getConfig()->get('date.timezone'));
60
        // adds extensions
61
        $this->twig->addExtension(new TwigExtension($builder));
62
        $this->twig->addExtension(new \Twig\Extension\StringLoaderExtension());
63
        // internationalisation
64
        if (extension_loaded('intl')) {
65
            $this->twig->addExtension(new \Twig_Extensions_Extension_Intl());
66
            $this->twig->registerUndefinedFilterCallback(function ($name) {
67
                switch ($name) {
68
                    case 'localizeddate':
69
                        return new \Twig\TwigFilter($name, 'date');
70
                        break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
71
                }
72
73
                return false;
74
            });
75
        }
76
        if (extension_loaded('gettext')) {
77
            $this->twig->addExtension(new \Twig_Extensions_Extension_I18n());
78
        }
79
        if ($builder->isDebug()) {
80
            // dump()
81
            $this->twig->addExtension(new \Twig\Extension\DebugExtension());
82
            // profiler
83
            $this->profile = new \Twig\Profiler\Profile();
84
            $this->twig->addExtension(new \Twig\Extension\ProfilerExtension($this->profile));
85
        }
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function addGlobal(string $name, $value): void
92
    {
93
        $this->twig->addGlobal($name, $value);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function render(string $template, array $variables): string
100
    {
101
        return $this->twig->render($template, $variables);
102
    }
103
}
104