Passed
Pull Request — master (#1835)
by Arnaud
09:37 queued 03:58
created

Layout::fallback()   C

Complexity

Conditions 15
Paths 24

Size

Total Lines 124
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 83
CRAP Score 15.1528

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 78
c 1
b 0
f 0
nc 24
nop 2
dl 0
loc 124
ccs 83
cts 91
cp 0.9121
crap 15.1528
rs 5.2133

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\Config;
19
use Cecil\Exception\RuntimeException;
20
use Cecil\Util;
21
22
/**
23
 * Class Layout.
24
 */
25
class Layout
26
{
27
    public const EXT = 'twig';
28
29
    /**
30
     * Layout files finder.
31
     *
32
     * @throws RuntimeException
33
     */
34 1
    public static function finder(CollectionPage $page, string $format, Config $config): array
35
    {
36 1
        $layout = 'unknown';
37
38
        // what layouts, in what format, could be use for the page?
39 1
        $layouts = self::fallback($page, $format);
40
41
        // take the first available layout
42 1
        foreach ($layouts as $layout) {
43 1
            $layout = Util::joinFile($layout);
44
            // is it in `layouts/` dir?
45 1
            if (Util\File::getFS()->exists(Util::joinFile($config->getLayoutsPath(), $layout))) {
46 1
                return [
47 1
                    'scope' => 'site',
48 1
                    'file'  => $layout,
49 1
                ];
50
            }
51
            // is it in `<theme>/layouts/` dir?
52 1
            if ($config->hasTheme()) {
53 1
                $themes = $config->getTheme();
54 1
                foreach ($themes as $theme) {
55 1
                    if (Util\File::getFS()->exists(Util::joinFile($config->getThemeDirPath($theme, 'layouts'), $layout))) {
56 1
                        return [
57 1
                            'scope' => $theme,
58 1
                            'file'  => $layout,
59 1
                        ];
60
                    }
61
                }
62
            }
63
            // is it in `resources/layouts/` dir?
64 1
            if (Util\File::getFS()->exists(Util::joinPath($config->getInternalLayoutsPath(), $layout))) {
65 1
                return [
66 1
                    'scope' => 'cecil',
67 1
                    'file'  => $layout,
68 1
                ];
69
            }
70
        }
71
72
        throw new RuntimeException(sprintf('Layout "%s" not found (page: %s).', $layout, $page->getId()));
73
    }
74
75
    /**
76
     * Layout fall-back.
77
     *
78
     * @see finder()
79
     */
80 1
    protected static function fallback(CollectionPage $page, string $format): array
81
    {
82 1
        $ext = self::EXT;
83
84
        // remove potential redundant extension
85 1
        $layout = str_replace(".$ext", '', (string) $page->getVariable('layout'));
86
87 1
        switch ($page->getType()) {
88
            case PageType::HOMEPAGE:
89 1
                $layouts = [
90
                    // "$layout.$format.$ext",
91 1
                    "home.$format.$ext",
92 1
                    "index.$format.$ext",
93 1
                    "list.$format.$ext",
94 1
                    "_default/home.$format.$ext",
95 1
                    "_default/index.$format.$ext",
96 1
                    "_default/list.$format.$ext",
97 1
                    "_default/page.$format.$ext",
98 1
                ];
99 1
                if ($page->hasVariable('layout')) {
100
                    $layouts = array_merge(
101
                        ["$layout.$format.$ext"],
102
                        $layouts
103
                    );
104
                }
105 1
                break;
106
            case PageType::SECTION:
107 1
                $layouts = [
108
                    // "$layout.$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
                    $section = $page->getSection();
116 1
                    $layouts = array_merge(
117 1
                        ["section/$section.$format.$ext"],
118 1
                        $layouts
119 1
                    );
120 1
                    $layouts = array_merge(
121 1
                        ["$section/list.$format.$ext"],
122 1
                        $layouts
123 1
                    );
124
                }
125 1
                if ($page->hasVariable('layout')) {
126
                    $layouts = array_merge(
127
                        ["$layout.$format.$ext"],
128
                        $layouts
129
                    );
130
                }
131 1
                break;
132
            case PageType::VOCABULARY:
133 1
                $layouts = [
134
                    // "taxonomy/$plural.$format.$ext", // e.g.: taxonomy/tags.html.twig
135 1
                    "_default/vocabulary.$format.$ext", // e.g.: _default/vocabulary.html.twig
136 1
                ];
137 1
                if ($page->hasVariable('plural')) {
138 1
                    $layouts = array_merge(
139 1
                        ["taxonomy/{$page->getVariable('plural')}.$format.$ext"],
140 1
                        $layouts
141 1
                    );
142
                }
143 1
                break;
144
            case PageType::TERM:
145 1
                $layouts = [
146
                    // "taxonomy/$term.$format.$ext",     // e.g.: taxonomy/velo.html.twig
147
                    // "taxonomy/$singular.$format.$ext", // e.g.: taxonomy/tag.html.twig
148 1
                    "_default/term.$format.$ext",         // e.g.: _default/term.html.twig
149 1
                    "_default/list.$format.$ext",         // e.g.: _default/list.html.twig
150 1
                ];
151 1
                if ($page->hasVariable('term')) {
152 1
                    $layouts = array_merge(
153 1
                        ["taxonomy/{$page->getVariable('term')}.$format.$ext"],
154 1
                        $layouts
155 1
                    );
156
                }
157 1
                if ($page->hasVariable('singular')) {
158 1
                    $layouts = array_merge(
159 1
                        ["taxonomy/{$page->getVariable('singular')}.$format.$ext"],
160 1
                        $layouts
161 1
                    );
162
                }
163 1
                break;
164
            default:
165 1
                $layouts = [
166
                    // "$section/$layout.$format.$ext",
167
                    // "$layout.$format.$ext",
168
                    // "$section/page.$format.$ext",
169
                    // "page.$format.$ext",
170
                    // "_default/$layout.$format.$ext",
171 1
                    "_default/page.$format.$ext",
172 1
                ];
173 1
                if ($page->hasVariable('layout')) {
174 1
                    $layouts = array_merge(
175 1
                        ["_default/$layout.$format.$ext"],
176 1
                        $layouts
177 1
                    );
178
                }
179 1
                $layouts = array_merge(
180 1
                    ["page.$format.$ext"],
181 1
                    $layouts
182 1
                );
183 1
                if ($page->getSection()) {
184 1
                    $layouts = array_merge(
185 1
                        ["{$page->getSection()}/page.$format.$ext"],
186 1
                        $layouts
187 1
                    );
188
                }
189 1
                if ($page->hasVariable('layout')) {
190 1
                    $layouts = array_merge(
191 1
                        ["$layout.$format.$ext"],
192 1
                        $layouts
193 1
                    );
194 1
                    if ($page->getSection()) {
195 1
                        $layouts = array_merge(
196 1
                            ["{$page->getSection()}/$layout.$format.$ext"],
197 1
                            $layouts
198 1
                        );
199
                    }
200
                }
201
        }
202
203 1
        return $layouts;
204
    }
205
}
206