Completed
Pull Request — master (#41)
by Vladimir
02:31
created

TwigManager::configureTwig()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 53
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 6.9459

Importance

Changes 0
Metric Value
cc 6
eloc 27
nc 24
nop 2
dl 0
loc 53
ccs 26
cts 37
cp 0.7027
crap 6.9459
rs 8.7155
c 0
b 0
f 0

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
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Manager;
9
10
use allejo\stakx\Engines\Markdown\TwigMarkdownEngine;
11
use allejo\stakx\Object\Configuration;
12
use allejo\stakx\Twig\FilesystemExtension;
13
use allejo\stakx\Twig\TextExtension;
14
use allejo\stakx\Twig\TwigExtension;
15
use Aptoma\Twig\Extension\MarkdownExtension;
16
use Twig_Environment;
17
use Twig_Loader_Filesystem;
18
19
class TwigManager extends BaseManager
20
{
21
    /**
22
     * @var Twig_Environment
23
     */
24
    private static $twig;
25
26 16
    public static function &getInstance()
27
    {
28 16
        return self::$twig;
29
    }
30
31
    /**
32
     * @param Configuration $configuration
33
     * @param mixed         $options
34
     */
35 9
    public function configureTwig($configuration, $options = array())
36
    {
37 9
        $loader = new Twig_Loader_Filesystem(array(
38 9
            getcwd(),
39 9
        ));
40 9
        $theme = $configuration->getTheme();
41 9
        $mdEngine = new TwigMarkdownEngine();
42
43
        // Only load a theme if one is specified and actually exists
44 9
        if (!is_null($theme))
45 9
        {
46
            try
47
            {
48
                $loader->addPath($this->fs->absolutePath('_themes', $configuration->getTheme()), 'theme');
49
            }
50
            catch (\Twig_Error_Loader $e)
51
            {
52
                $this->output->error('The following theme could not be loaded: {theme}', array(
53
                    'theme' => $theme,
54
                ));
55
                $this->output->error($e->getMessage());
56
            }
57
        }
58
59 9
        self::$twig = new Twig_Environment($loader, array(
60 9
            'autoescape'  => $configuration->getTwigAutoescape(),
61 9
            'auto_reload' => true,
62 9
            'cache'       => $this->fs->absolutePath('.stakx-cache/twig'),
63 9
        ));
64
65 9
        foreach ($options['globals'] as $global)
66
        {
67
            self::$twig->addGlobal($global['name'], $global['value']);
68 9
        }
69
70
        // We'll use this to access the current file in our Twig filters
71 9
        self::$twig->addGlobal('__currentTemplate', '');
72
73 9
        self::$twig->addExtension(new TwigExtension());
74 9
        self::$twig->addExtension(new TextExtension());
75 9
        self::$twig->addExtension(new MarkdownExtension($mdEngine));
76
77 9
        if (!$options['safe'])
78 9
        {
79 9
            self::$twig->addExtension(new FilesystemExtension());
80 9
        }
81
82 9
        if ($configuration->isDebug())
83 9
        {
84
            self::$twig->addExtension(new \Twig_Extension_Debug());
85
            self::$twig->enableDebug();
86
        }
87 9
    }
88
}
89