Completed
Push — master ( a1590e...0af16c )
by Vladimir
02:39
created

TwigManager::configureTwig()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 53
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6

Importance

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