GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TwigServiceProvider::register()   C
last analyzed

Complexity

Conditions 11
Paths 1

Size

Total Lines 122

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 72
CRAP Score 11

Importance

Changes 0
Metric Value
dl 0
loc 122
ccs 72
cts 72
cp 1
rs 5.8533
c 0
b 0
f 0
cc 11
nc 1
nop 1
crap 11

How to fix   Long Method    Complexity   

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
 * This file is part of the trefoil application.
4
 *
5
 * (c) Miguel Angel Gabriel <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Trefoil\Providers;
12
13
use Easybook\DependencyInjection\Application;
14
use Easybook\Util\TwigCssExtension;
15
use Pimple\Container;
16
use Pimple\ServiceProviderInterface;
17
use Trefoil\Util\Toolkit;
18
19
class TwigServiceProvider implements ServiceProviderInterface
20
{
21 35
    public function register(Container $app)
22
    {
23 1
        $app['twig.options'] = array(
24 1
            'autoescape'       => false,
25
            // 'cache'         => $app['app.dir.cache'].'/Twig',
26 1
            'charset'          => $app['app.charset'],
27 35
            'debug'            => $app['app.debug'],
28 1
            'strict_variables' => $app['app.debug.strict_variables'],
29
        );
30
31 1
        $app['twig.loader'] =
32
            function () use ($app) {
33
34
                /* @var Application $var */
35
36 34
                $theme = ucfirst($app->edition('theme'));
37 34
                $format = Toolkit::camelize($app->edition('format'), true);
38
39 34
                $loader = new \Twig_Loader_Filesystem($app['app.dir.themes']);
40
41
                // easybook base configuration:
42
                // Base theme (common styles per edition type)
43
                // <easybook>/app/Resources/Themes/Base/<edition-type>/Templates/<template-name>.twig
44 34
                $baseThemeDir = sprintf('%s/Base/%s/Templates', $app['app.dir.themes'], $format);
45 34
                $loader->addPath($baseThemeDir);
46 34
                $loader->addPath($baseThemeDir, 'theme');
47 34
                $loader->addPath($baseThemeDir, 'theme_base');
48
49
                // easybook base configuration:
50
                // Book theme (configured per edition in 'config.yml')
51
                // <easybook>/app/Resources/Themes/<theme>/<edition-type>/Templates/<template-name>.twig
52
                $bookThemeDir =
53 34
                    sprintf('%s/%s/%s/Templates', $app['app.dir.themes'], $theme, $format);
54 34
                if (file_exists($bookThemeDir)) {
55 4
                    $loader->prependPath($bookThemeDir);
56 4
                    $loader->prependPath($bookThemeDir, 'theme');
57 4
                }
58
59
                // look if we have a custom theme set
60 34
                $themesDir = Toolkit::getCurrentThemeDir($app);
61 34
                if (file_exists($themesDir)) {
62
                    // Theme common (common styles per edition theme)
63
                    // <themes-dir>/Common/Templates/<template-name>.twig
64 14
                    $baseThemeDir = sprintf('%s/Common/Templates', $themesDir);
65 14
                    $loader->prependPath($baseThemeDir);
66 14
                    $loader->prependPath($baseThemeDir, 'theme');
67 14
                    $loader->prependPath($baseThemeDir, 'theme_common');
68
69
                    // Register template paths
70
                    $ownTemplatePaths = array(
71
                        // <themes-dir>/<edition-type>/Templates/<template-name>.twig
72 14
                        sprintf('%s/%s/Templates', $themesDir, $format),
73
                        // <themes-dir>/<edition-name>/Templates/<template-name>.twig
74 14
                        sprintf('%s/%s/Templates', $themesDir, $app['publishing.edition']),
75 14
                    );
76
                    
77 14
                    foreach ($ownTemplatePaths as $path) {
78 14
                        if (file_exists($path)) {
79 14
                            $loader->prependPath($path);
80 14
                            $loader->prependPath($path, 'theme');
81 14
                        }
82 14
                    }
83 14
                }
84
85
                $userTemplatePaths = array(
86
                    // <book-dir>/Resources/Templates/<template-name>.twig
87 34
                    $app['publishing.dir.templates'],
88
                    // <book-dir>/Resources/Templates/<edition-type>/<template-name>.twig
89 34
                    sprintf('%s/%s', $app['publishing.dir.templates'], $format),
90
                    // <book-dir>/Resources/Templates/<edition-name>/<template-name>.twig
91 34
                    sprintf(
92 34
                        '%s/%s',
93 34
                        $app['publishing.dir.templates'],
94 34
                        $app['publishing.edition']
95 34
                    ),
96 34
                );
97
98 34
                foreach ($userTemplatePaths as $path) {
99 34
                    if (file_exists($path)) {
100 12
                        $loader->prependPath($path);
101 12
                    }
102 34
                }
103
104
                // Register content paths
105 34
                if (file_exists($themesDir)) {
106
                    $ownContentPaths = array(
107
                        // <themes-dir>/Common/Contents/<template-name>.md
108 14
                        sprintf('%s/Common/Contents', $themesDir),
109
                        // <themes-dir>/<edition-type>/Contents/<template-name>.md
110 14
                        sprintf('%s/%s/Contents', $themesDir, $format));
111
112 14
                    foreach ($ownContentPaths as $path) {
113 14
                        if (file_exists($path)) {
114 12
                            $loader->prependPath($path, 'content');
115 12
                        }
116 14
                    }
117 14
                }
118
119 34
                return $loader;
120
            };
121
122 1
        $app['twig'] =
123 34
            function () use ($app) {
124
                /* @var Application $var */
125
126 34
                $twig = new \Twig_Environment($app['twig.loader'], $app['twig.options']);
127 34
                $twig->addExtension(new TwigCssExtension());
128 34
                $twig->addExtension(new \Twig_Extension_Debug());
129
130 34
                $twig->addGlobal('app', $app);
131
132 34
                if (null !== $bookConfig = $app['publishing.book.config']) {
133 34
                    $twig->addGlobal('book', $bookConfig['book']);
134
135 34
                    $publishingEdition = $app['publishing.edition'];
136 34
                    $editions = $app->book('editions');
137 34
                    $twig->addGlobal('edition', $editions[$publishingEdition]);
138 34
                }
139
140 34
                return $twig;
141
            };
142 1
    }
143
}
144