Completed
Pull Request — master (#61)
by Vladimir
03:02
created

TwigStakxBridgeFactory::createTwigEnvironment()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 70
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5.6116

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 8.5454
c 0
b 0
f 0
ccs 22
cts 31
cp 0.7097
cc 5
eloc 41
nc 12
nop 8
crap 5.6116

How to fix   Long Method    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * @copyright 2018 Vladimir Jimenez
4
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
5
 */
6
7
namespace allejo\stakx\Templating\Twig;
8
9
use allejo\stakx\Command\BuildableCommand;
10
use allejo\stakx\Configuration;
11
use allejo\stakx\Engines\Markdown\TwigMarkdownEngine;
12
use allejo\stakx\Manager\CollectionManager;
13
use allejo\stakx\Manager\DataManager;
14
use allejo\stakx\Manager\MenuManager;
15
use allejo\stakx\Manager\PageManager;
16
use allejo\stakx\Service;
17
use allejo\stakx\System\Filesystem;
18
use Aptoma\Twig\Extension\MarkdownExtension;
19
use Psr\Log\LoggerInterface;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
use Twig_Environment;
22
use Twig_Extension_Debug;
23
24
class TwigStakxBridgeFactory
25
{
26 12
    public static function createTwigEnvironment(
27
        Configuration $configuration,
28
        CollectionManager $collectionManager,
29
        DataManager $dataManager,
30
        PageManager $pageManager,
31
        MenuManager $menuManager,
32
        TwigExtension $twigExtension,
33
        EventDispatcherInterface $eventDispatcher,
0 ignored issues
show
Unused Code introduced by
The parameter $eventDispatcher is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
        LoggerInterface $logger
35
    ) {
36 12
        $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...
37 12
        $loader = new TwigFileLoader(array(
38 12
            Service::getWorkingDirectory(),
39
        ));
40
41 12
        $theme = $configuration->getTheme();
42
43
        // Only load a theme if one is specified and actually exists
44 12
        if ($theme !== null)
45
        {
46
            try
47
            {
48
                $loader->addPath($fs->absolutePath('_themes', $theme), 'theme');
49
            }
50
            catch (\Twig_Error_Loader $e)
51
            {
52
                $logger->error('The following theme could not be loaded: {theme}', array(
53
                    'theme' => $theme,
54
                ));
55
                $logger->error($e->getMessage());
56
            }
57
        }
58
59 12
        $twig = new Twig_Environment($loader, array(
60 12
            'autoescape'  => $configuration->getTwigAutoescape(),
61
            'auto_reload' => true,
62 12
            'cache'       => $fs->absolutePath('.stakx-cache/twig'),
63
        ));
64
65
        // We'll use this to access the current file in our Twig filters
66 12
        $twig->addGlobal('__currentTemplate', '');
67
68
        // Global variables maintained by stakx
69 12
        $twig->addGlobal('site', $configuration->getConfiguration());
70 12
        $twig->addGlobal('data', $dataManager->getJailedDataItems());
71 12
        $twig->addGlobal('collections', $collectionManager->getJailedCollections());
72 12
        $twig->addGlobal('menu', $menuManager->getSiteMenu());
73 12
        $twig->addGlobal('pages', $pageManager->getJailedStaticPageViews());
74
75 12
        $twig->addExtension($twigExtension);
76
77 12
        $profiler = null;
78
79 12
        if (Service::getParameter(BuildableCommand::BUILD_PROFILE))
80
        {
81
            $profiler = new \Twig_Profiler_Profile();
82
            $twig->addExtension(new \Twig_Extension_Profiler($profiler));
83
        }
84
85 12
        if ($configuration->isDebug())
86
        {
87
            $twig->addExtension(new Twig_Extension_Debug());
88
            $twig->enableDebug();
89
        }
90
91 12
        $bridge = new TwigStakxBridge($twig);
92 12
        $bridge->setProfiler($profiler);
93
94 12
        return $bridge;
95
    }
96
}
97