Passed
Pull Request — master (#1676)
by Arnaud
13:18 queued 04:29
created

Layout::fallback()   C

Complexity

Conditions 17
Paths 48

Size

Total Lines 99
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 58
CRAP Score 17.0106

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 17
eloc 58
c 2
b 0
f 0
nc 48
nop 3
dl 0
loc 99
ccs 58
cts 60
cp 0.9667
crap 17.0106
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
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 1
    public static function finder(CollectionPage $page, string $format, \Cecil\Config $config): array
34
    {
35 1
        $layout = 'unknown';
36
37
        // which layouts, in what format, could be used for the page?
38 1
        $layouts = self::lookup($page, $format, $config);
39
40
        // take the first available layout
41 1
        foreach ($layouts as $layout) {
42 1
            $layout = Util::joinFile($layout);
43
            // is it in `layouts/` dir?
44 1
            if (Util\File::getFS()->exists(Util::joinFile($config->getLayoutsPath(), $layout))) {
45 1
                return [
46 1
                    'scope' => 'site',
47 1
                    'file'  => $layout,
48 1
                ];
49
            }
50
            // is it in `<theme>/layouts/` dir?
51 1
            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
                    }
60
                }
61
            }
62
            // is it in resources/layouts/ dir?
63 1
            if (Util\File::getFS()->exists(Util::joinPath($config->getLayoutsInternalPath(), $layout))) {
64 1
                return [
65 1
                    'scope' => 'cecil',
66 1
                    'file'  => $layout,
67 1
                ];
68
            }
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 1
    protected static function lookup(CollectionPage $page, string $format, \Cecil\Config $config): array
80
    {
81 1
        $ext = self::EXT;
82
83
        // remove potential redundant extension
84 1
        $layout = str_replace(".$ext", '', (string) $page->getVariable('layout'));
85
86 1
        switch ($page->getType()) {
87 1
            case PageType::HOMEPAGE->value:
88 1
                $layouts = [
89
                    // "$layout.$format.$ext",
90 1
                    "index.$format.$ext",
91 1
                    "home.$format.$ext",
92 1
                    "list.$format.$ext",
93 1
                ];
94 1
                if ($page->hasVariable('layout')) {
95
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts, ["_default/$layout.$format.$ext"]);
96
                }
97 1
                $layouts = array_merge($layouts, [
98
                    // "_default/$layout.$format.$ext",
99 1
                    "_default/index.$format.$ext",
100 1
                    "_default/home.$format.$ext",
101 1
                    "_default/list.$format.$ext",
102 1
                    "_default/page.$format.$ext",
103 1
                ]);
104 1
                break;
105 1
            case PageType::SECTION->value:
106 1
                $layouts = [
107
                    // "$layout.$format.$ext",
108
                    // "$section/index.$format.$ext",
109
                    // "$section/list.$format.$ext",
110
                    // "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
                }
119 1
                if ($page->hasVariable('layout')) {
120
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts);
121
                }
122 1
                break;
123 1
            case PageType::VOCABULARY->value:
124 1
                $layouts = [
125
                    // "taxonomy/$plural.$format.$ext", // e.g.: taxonomy/tags.html.twig
126 1
                    "_default/vocabulary.$format.$ext", // e.g.: _default/vocabulary.html.twig
127 1
                ];
128 1
                if ($page->hasVariable('plural')) {
129 1
                    $layouts = array_merge(["taxonomy/{$page->getVariable('plural')}.$format.$ext"], $layouts);
130
                }
131 1
                break;
132 1
            case PageType::TERM->value:
133 1
                $layouts = [
134
                    // "taxonomy/$term.$format.$ext",     // e.g.: taxonomy/velo.html.twig
135
                    // "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 1
                    $layouts = array_merge(["taxonomy/{$page->getVariable('term')}.$format.$ext"], $layouts);
141
                }
142 1
                if ($page->hasVariable('singular')) {
143 1
                    $layouts = array_merge(["taxonomy/{$page->getVariable('singular')}.$format.$ext"], $layouts);
144
                }
145 1
                break;
146
            default:
147 1
                $layouts = [
148
                    // "$section/$layout.$format.$ext",
149
                    // "$layout.$format.$ext",
150
                    // "$section/page.$format.$ext",
151
                    // "page.$format.$ext",
152
                    // "_default/$layout.$format.$ext",
153 1
                    "_default/page.$format.$ext",
154 1
                ];
155 1
                if ($page->hasVariable('layout')) {
156 1
                    $layouts = array_merge(["_default/$layout.$format.$ext"], $layouts);
157
                }
158 1
                $layouts = array_merge(["page.$format.$ext"], $layouts);
159 1
                if ($page->getSection()) {
160 1
                    $layouts = array_merge(["{$page->getSection()}/page.$format.$ext"], $layouts);
161
                }
162 1
                if ($page->hasVariable('layout')) {
163 1
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts);
164 1
                    if ($page->getSection()) {
165 1
                        $layouts = array_merge(["{$page->getSection()}/$layout.$format.$ext"], $layouts);
166
                    }
167
                }
168
        }
169
170
        // 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
            }
175
        }
176
177 1
        return $layouts;
178
    }
179
}
180