Completed
Push — master ( 2319eb...3c4d84 )
by Vladimir
02:22
created

TwigStakxBridgeFactory   C

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 20

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 0
cbo 20
dl 0
loc 80
rs 6.4705
c 1
b 0
f 0
ccs 0
cts 36
cp 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B createTwigEnvironment() 0 77 6
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\Manager\CollectionManager;
9
use allejo\stakx\Manager\DataManager;
10
use allejo\stakx\Manager\MenuManager;
11
use allejo\stakx\Manager\PageManager;
12
use allejo\stakx\Service;
13
use allejo\stakx\System\Filesystem;
14
use allejo\stakx\Twig\FilesystemExtension;
15
use allejo\stakx\Twig\StakxTwigFileLoader;
16
use allejo\stakx\Twig\TextExtension;
17
use allejo\stakx\Twig\TwigExtension;
18
use Aptoma\Twig\Extension\MarkdownExtension;
19
use Psr\Log\LoggerInterface;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
use Twig_Environment;
23
use Twig_Extension_Debug;
24
25
class TwigStakxBridgeFactory
26
{
27
    public static function createTwigEnvironment(
28
        Configuration $configuration,
29
        CollectionManager $collectionManager,
30
        DataManager $dataManager,
31
        PageManager $pageManager,
32
        MenuManager $menuManager,
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
        $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
        $loader = new StakxTwigFileLoader(array(
38
            getcwd(),
39
        ));
40
41
        $theme = $configuration->getTheme();
42
        $mdEngine = new TwigMarkdownEngine();
43
44
        // Only load a theme if one is specified and actually exists
45
        if ($theme !== null)
46
        {
47
            try
48
            {
49
                $loader->addPath($fs->absolutePath('_themes', $theme), 'theme');
50
            }
51
            catch (\Twig_Error_Loader $e)
52
            {
53
                $logger->error('The following theme could not be loaded: {theme}', array(
54
                    'theme' => $theme,
55
                ));
56
                $logger->error($e->getMessage());
57
            }
58
        }
59
60
        $twig = new Twig_Environment($loader, array(
61
            'autoescape'  => $configuration->getTwigAutoescape(),
62
            'auto_reload' => true,
63
            'cache'       => $fs->absolutePath('.stakx-cache/twig'),
64
        ));
65
66
        // We'll use this to access the current file in our Twig filters
67
        $twig->addGlobal('__currentTemplate', '');
68
69
        // Global variables maintained by stakx
70
        $twig->addGlobal('site', $configuration->getConfiguration());
71
        $twig->addGlobal('data', $dataManager->getJailedDataItems());
72
        $twig->addGlobal('collections', $collectionManager->getJailedCollections());
73
        $twig->addGlobal('menu', $menuManager->getSiteMenu());
74
        $twig->addGlobal('pages', $pageManager->getJailedStaticPageViews());
75
76
        $twig->addExtension(new TwigExtension());
77
        $twig->addExtension(new TextExtension());
78
        $twig->addExtension(new MarkdownExtension($mdEngine));
79
80
        if (!Service::getParameter(BuildableCommand::SAFE_MODE))
81
        {
82
            $twig->addExtension(new FilesystemExtension());
83
        }
84
85
        $profiler = null;
86
87
        if (Service::getParameter(BuildableCommand::BUILD_PROFILE))
88
        {
89
            $profiler = new \Twig_Profiler_Profile();
90
            $twig->addExtension(new \Twig_Extension_Profiler($profiler));
91
        }
92
93
        if ($configuration->isDebug())
94
        {
95
            $twig->addExtension(new Twig_Extension_Debug());
96
            $twig->enableDebug();
97
        }
98
99
        $bridge = new TwigStakxBridge($twig);
100
        $bridge->setProfiler($profiler);
101
102
        return $bridge;
103
    }
104
}
105