Passed
Push — master ( 0b1158...df6e14 )
by
unknown
05:01
created

Layout::lookup()   C

Complexity

Conditions 17
Paths 48

Size

Total Lines 104
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 66
CRAP Score 17.0073

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 17
eloc 62
c 2
b 1
f 1
nc 48
nop 3
dl 0
loc 104
ccs 66
cts 68
cp 0.9706
crap 17.0073
rs 5.2166

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