Completed
Push — master ( 0ffa26...ca3451 )
by Vladimir
02:21
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 1
Bugs 0 Features 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 1
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 5
    public static function &getInstance ()
21
    {
22 5
        return self::$twig;
23
    }
24
25
    /**
26
     * @param Configuration $configuration
27
     * @param mixed         $options
28
     */
29 1
    public function configureTwig ($configuration, $options = array())
30
    {
31 1
        $loader   = new Twig_Loader_Filesystem(array(
32 1
            getcwd()
33
        ));
34 1
        $theme    = $configuration->getTheme();
35 1
        $mdEngine = new TwigMarkdownEngine();
36
37
        // Only load a theme if one is specified and actually exists
38 1
        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 1
        self::$twig = new Twig_Environment($loader, array(
54 1
            'autoescape'  => $configuration->getTwigAutoescape(),
55
            'auto_reload' => true,
56 1
            'cache'       => $this->fs->absolutePath('.stakx-cache/twig')
57
        ));
58
59 1
        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 1
        self::$twig->addGlobal('__currentTemplate', '');
66
67 1
        self::$twig->addExtension(new TwigExtension());
68 1
        self::$twig->addExtension(new \Twig_Extensions_Extension_Text());
69 1
        self::$twig->addExtension(new MarkdownExtension($mdEngine));
70
71 1
        if (!$options['safe'])
72
        {
73 1
            self::$twig->addExtension(new FilesystemExtension());
74
        }
75
76 1
        if ($configuration->isDebug())
77
        {
78 1
            self::$twig->addExtension(new \Twig_Extension_Debug());
79 1
            self::$twig->enableDebug();
80
        }
81
    }
82
}