Passed
Push — nested-sections ( 2d40b2...7dbf34 )
by Arnaud
12:03 queued 05:37
created

Layout::fallback()   C

Complexity

Conditions 14
Paths 18

Size

Total Lines 115
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 76
CRAP Score 14.1691

Importance

Changes 0
Metric Value
cc 14
eloc 72
c 0
b 0
f 0
nc 18
nop 2
dl 0
loc 115
ccs 76
cts 84
cp 0.9048
crap 14.1691
rs 5.6242

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Renderer;
15
16
use Cecil\Collection\Page\Page as CollectionPage;
17
use Cecil\Collection\Page\Type as PageType;
18
use Cecil\Exception\RuntimeException;
19
use Cecil\Util;
20
21
/**
22
 * Class Layout.
23
 */
24
class Layout
25
{
26
    public const EXT = 'twig';
27
28
    /**
29
     * Layout files finder.
30
     *
31
     * @throws RuntimeException
32
     */
33
    public static function finder(CollectionPage $page, string $format, \Cecil\Config $config): array
34 1
    {
35
        $layout = 'unknown';
36 1
37
        // which layouts, in what format, could be used for the page?
38
        $layouts = self::lookup($page, $format, $config);
39 1
40
        // take the first available layout
41
        foreach ($layouts as $layout) {
42 1
            $layout = Util::joinFile($layout);
43 1
            // is it in `layouts/` dir?
44
            if (Util\File::getFS()->exists(Util::joinFile($config->getLayoutsPath(), $layout))) {
45 1
                return [
46 1
                    'scope' => 'site',
47 1
                    'file'  => $layout,
48 1
                ];
49 1
            }
50
            // is it in `<theme>/layouts/` dir?
51
            if ($config->hasTheme()) {
52 1
                $themes = $config->getTheme();
53 1
                foreach ($themes as $theme) {
54 1
                    if (Util\File::getFS()->exists(Util::joinFile($config->getThemeDirPath($theme, 'layouts'), $layout))) {
55 1
                        return [
56 1
                            'scope' => $theme,
57 1
                            'file'  => $layout,
58 1
                        ];
59 1
                    }
60
                }
61
            }
62
            // is it in resources/layouts/ dir?
63
            if (Util\File::getFS()->exists(Util::joinPath($config->getLayoutsInternalPath(), $layout))) {
64 1
                return [
65 1
                    'scope' => 'cecil',
66 1
                    'file'  => $layout,
67 1
                ];
68 1
            }
69
        }
70
71
        throw new RuntimeException(sprintf('Layout "%s" not found (page: %s).', $layout, $page->getId()));
72
    }
73
74
    /**
75
     * Templates lookup rules.
76
     *
77
     * @see self::finder()
78
     */
79
    protected static function lookup(CollectionPage $page, string $format, \Cecil\Config $config): array
80 1
    {
81
        $ext = self::EXT;
82 1
83
        // remove potential redundant extension
84
        $layout = str_replace(".$ext", '', (string) $page->getVariable('layout'));
85 1
86
        switch ($page->getType()) {
87 1
            case PageType::HOMEPAGE->value:
88
                $layouts = [
89 1
                    // "$layout.$format.$ext",
90
                    "index.$format.$ext",
91 1
                    "home.$format.$ext",
92 1
                    "list.$format.$ext",
93 1
                ];
94 1
                if ($page->hasVariable('layout')) {
95 1
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts, ["_default/$layout.$format.$ext"]);
96 1
                }
97 1
                $layouts = array_merge($layouts, [
98
                    // "_default/$layout.$format.$ext",
99
                    "_default/index.$format.$ext",
100
                    "_default/home.$format.$ext",
101
                    "_default/list.$format.$ext",
102
                    "_default/page.$format.$ext",
103 1
                ]);
104
                break;
105 1
            case PageType::SECTION->value:
106
                $layouts = [
107
                    // "$layout.$format.$ext",
108
                    // "$section/index.$format.$ext",
109 1
                    // "$section/list.$format.$ext",
110 1
                    // "section/$section.$format.$ext",
111 1
                    "_default/section.$format.$ext",
112 1
                    "_default/list.$format.$ext",
113 1
                ];
114 1
                if ($page->getPath()) {
115 1
                    $layouts = array_merge(["section/{$page->getSection()}.$format.$ext"], $layouts);
116 1
                    $layouts = array_merge(["{$page->getSection()}/list.$format.$ext"], $layouts);
117 1
                    $layouts = array_merge(["{$page->getSection()}/index.$format.$ext"], $layouts);
118 1
                }
119 1
                if ($page->hasVariable('layout')) {
120 1
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts);
121 1
                }
122
                break;
123 1
            case PageType::VOCABULARY->value:
124
                $layouts = [
125
                    // "taxonomy/$plural.$format.$ext", // e.g.: taxonomy/tags.html.twig
126
                    "_default/vocabulary.$format.$ext", // e.g.: _default/vocabulary.html.twig
127
                ];
128
                if ($page->hasVariable('plural')) {
129 1
                    $layouts = array_merge(["taxonomy/{$page->getVariable('plural')}.$format.$ext"], $layouts);
130
                }
131 1
                break;
132
            case PageType::TERM->value:
133 1
                $layouts = [
134 1
                    // "taxonomy/$term.$format.$ext",     // e.g.: taxonomy/velo.html.twig
135 1
                    // "taxonomy/$singular.$format.$ext", // e.g.: taxonomy/tag.html.twig
136 1
                    "_default/term.$format.$ext",         // e.g.: _default/term.html.twig
137 1
                    "_default/list.$format.$ext",         // e.g.: _default/list.html.twig
138 1
                ];
139 1
                if ($page->hasVariable('term')) {
140
                    $layouts = array_merge(["taxonomy/{$page->getVariable('term')}.$format.$ext"], $layouts);
141 1
                }
142
                if ($page->hasVariable('singular')) {
143 1
                    $layouts = array_merge(["taxonomy/{$page->getVariable('singular')}.$format.$ext"], $layouts);
144
                }
145
                break;
146 1
            default:
147 1
                $layouts = [
148 1
                    // "$section/$layout.$format.$ext",
149 1
                    // "$layout.$format.$ext",
150 1
                    // "$section/page.$format.$ext",
151 1
                    // "_default/$layout.$format.$ext",
152 1
                    // "page.$format.$ext",
153 1
                    "_default/page.$format.$ext",
154
                ];
155 1
                $layouts = array_merge(["page.$format.$ext"], $layouts);
156 1
                if ($page->hasVariable('layout')) {
157 1
                    $layouts = array_merge(["_default/$layout.$format.$ext"], $layouts);
158 1
                }
159 1
                if ($page->getSection()) {
160
                    $layouts = array_merge(["{$page->getSection()}/page.$format.$ext"], $layouts);
161 1
                }
162
                if ($page->hasVariable('layout')) {
163 1
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts);
164
                    if ($page->getSection()) {
165
                        $layouts = array_merge(["{$page->getSection()}/$layout.$format.$ext"], $layouts);
166
                    }
167
                }
168 1
        }
169 1
170 1
        // add localized layouts
171 1
        if ($page->getVariable('language') !== $config->getLanguageDefault()) {
172 1
            foreach ($layouts as $key => $value) {
173 1
                $layouts = array_merge(\array_slice($layouts, 0, $key), [str_replace(".$ext", ".{$page->getVariable('language')}.$ext", $value)], \array_slice($layouts, $key));
174 1
            }
175 1
        }
176 1
177 1
        return $layouts;
178 1
    }
179
}
180