Completed
Push — master ( a6c8f9...b97be3 )
by Vladimir
03:52 queued 19s
created

TwigStakxBridgeFactory::createTwigEnvironment()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 63
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 6.9945

Importance

Changes 0
Metric Value
dl 0
loc 63
ccs 30
cts 43
cp 0.6977
rs 8.6498
c 0
b 0
f 0
cc 6
eloc 33
nc 24
nop 2
crap 6.9945

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\Templating;
4
5
use allejo\stakx\Command\BuildableCommand;
6
use allejo\stakx\Configuration;
7
use allejo\stakx\Engines\Markdown\TwigMarkdownEngine;
8
use allejo\stakx\Service;
9
use allejo\stakx\System\Filesystem;
10
use allejo\stakx\Twig\FilesystemExtension;
11
use allejo\stakx\Twig\StakxTwigFileLoader;
12
use allejo\stakx\Twig\TextExtension;
13
use allejo\stakx\Twig\TwigExtension;
14
use Aptoma\Twig\Extension\MarkdownExtension;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Twig_Environment;
18
use Twig_Extension_Debug;
19
20
class TwigStakxBridgeFactory
21
{
22 14
    public static function createTwigEnvironment(Configuration $configuration, LoggerInterface $logger)
23
    {
24 14
        $fs = new Filesystem();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $fs. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
25 14
        $loader = new StakxTwigFileLoader(array(
26 14
            getcwd(),
27 14
        ));
28
29 14
        $theme = $configuration->getTheme();
30 14
        $mdEngine = new TwigMarkdownEngine();
31
32
        // Only load a theme if one is specified and actually exists
33 14
        if ($theme !== null)
34 14
        {
35
            try
36
            {
37
                $loader->addPath($fs->absolutePath('_themes', $theme), 'theme');
38
            }
39
            catch (\Twig_Error_Loader $e)
40
            {
41
                $logger->error('The following theme could not be loaded: {theme}', array(
42
                    'theme' => $theme,
43
                ));
44
                $logger->error($e->getMessage());
45
            }
46
        }
47
48 14
        $twig = new Twig_Environment($loader, array(
49 14
            'autoescape'  => $configuration->getTwigAutoescape(),
50 14
            'auto_reload' => true,
51 14
            'cache'       => $fs->absolutePath('.stakx-cache/twig'),
52 14
        ));
53
54
        // We'll use this to access the current file in our Twig filters
55 14
        $twig->addGlobal('__currentTemplate', '');
56
57 14
        $twig->addExtension(new TwigExtension());
58 14
        $twig->addExtension(new TextExtension());
59 14
        $twig->addExtension(new MarkdownExtension($mdEngine));
60
61 14
        if (!Service::getParameter(BuildableCommand::SAFE_MODE))
62 14
        {
63 14
            $twig->addExtension(new FilesystemExtension());
64 14
        }
65
66 14
        $profiler = null;
67
68 14
        if (Service::getParameter(BuildableCommand::BUILD_PROFILE))
69 14
        {
70
            $profiler = new \Twig_Profiler_Profile();
71
            $twig->addExtension(new \Twig_Extension_Profiler($profiler));
72
        }
73
74 14
        if ($configuration->isDebug())
75 14
        {
76
            $twig->addExtension(new Twig_Extension_Debug());
77
            $twig->enableDebug();
78
        }
79
80 14
        $bridge = new TwigStakxBridge($twig);
81 14
        $bridge->setProfiler($profiler);
82
83 14
        return $bridge;
84
    }
85
}
86