Completed
Push — master ( e4bfce...b9fe45 )
by Vladimir
02:19
created

TwigManager::configureTwig()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 53
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 6.2644

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 27
c 1
b 0
f 0
nc 24
nop 2
dl 0
loc 53
ccs 29
cts 36
cp 0.8056
crap 6.2644
rs 8.7155

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 1
        ));
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 1
        {
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 1
            'auto_reload' => true,
56 1
            'cache'       => $this->fs->absolutePath('.stakx-cache/twig')
57 1
        ));
58
59 1
        foreach ($options['globals'] as $global)
60
        {
61
            self::$twig->addGlobal($global['name'], $global['value']);
62 1
        }
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 1
        {
73 1
            self::$twig->addExtension(new FilesystemExtension());
74 1
        }
75
76 1
        if ($configuration->isDebug())
77 1
        {
78 1
            self::$twig->addExtension(new \Twig_Extension_Debug());
79 1
            self::$twig->enableDebug();
80 1
        }
81
    }
82
}