Passed
Pull Request — master (#2118)
by
unknown
13:00 queued 06:11
created

Layout::lookup()   C

Complexity

Conditions 17
Paths 48

Size

Total Lines 101
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 63
CRAP Score 17.0084

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 59
c 1
b 0
f 0
nc 48
nop 3
dl 0
loc 101
ccs 63
cts 65
cp 0.9692
crap 17.0084
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
        // page section or layout mapping
86 1
        $section = $config->getLayoutSection($page->getSection());
87
88 1
        switch ($page->getType()) {
89 1
            case PageType::HOMEPAGE->value:
90 1
                $layouts = [
91
                    // "$layout.$format.$ext",
92 1
                    "index.$format.$ext",
93 1
                    "home.$format.$ext",
94 1
                    "list.$format.$ext",
95 1
                ];
96 1
                if ($page->hasVariable('layout')) {
97
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts, ["_default/$layout.$format.$ext"]);
98
                }
99 1
                $layouts = array_merge($layouts, [
100
                    // "_default/$layout.$format.$ext",
101 1
                    "_default/index.$format.$ext",
102 1
                    "_default/home.$format.$ext",
103 1
                    "_default/list.$format.$ext",
104 1
                    "_default/page.$format.$ext",
105 1
                ]);
106 1
                break;
107 1
            case PageType::SECTION->value:
108 1
                $layouts = [
109
                    // "$layout.$format.$ext",
110
                    // "$section/index.$format.$ext",
111
                    // "$section/list.$format.$ext",
112
                    // "section/$section.$format.$ext",
113 1
                    "_default/section.$format.$ext",
114 1
                    "_default/list.$format.$ext",
115 1
                ];
116 1
                if ($page->getPath()) {
117 1
                    $layouts = array_merge(["section/{$section}.$format.$ext"], $layouts);
118 1
                    $layouts = array_merge(["{$section}/list.$format.$ext"], $layouts);
119 1
                    $layouts = array_merge(["{$section}/index.$format.$ext"], $layouts);
120
                }
121 1
                if ($page->hasVariable('layout')) {
122
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts);
123
                }
124 1
                break;
125 1
            case PageType::VOCABULARY->value:
126 1
                $layouts = [
127
                    // "taxonomy/$plural.$format.$ext", // e.g.: taxonomy/tags.html.twig
128 1
                    "_default/vocabulary.$format.$ext", // e.g.: _default/vocabulary.html.twig
129 1
                ];
130 1
                if ($page->hasVariable('plural')) {
131 1
                    $layouts = array_merge(["taxonomy/{$page->getVariable('plural')}.$format.$ext"], $layouts);
132
                }
133 1
                break;
134 1
            case PageType::TERM->value:
135 1
                $layouts = [
136
                    // "taxonomy/$term.$format.$ext",     // e.g.: taxonomy/velo.html.twig
137
                    // "taxonomy/$singular.$format.$ext", // e.g.: taxonomy/tag.html.twig
138 1
                    "_default/term.$format.$ext",         // e.g.: _default/term.html.twig
139 1
                    "_default/list.$format.$ext",         // e.g.: _default/list.html.twig
140 1
                ];
141 1
                if ($page->hasVariable('term')) {
142 1
                    $layouts = array_merge(["taxonomy/{$page->getVariable('term')}.$format.$ext"], $layouts);
143
                }
144 1
                if ($page->hasVariable('singular')) {
145 1
                    $layouts = array_merge(["taxonomy/{$page->getVariable('singular')}.$format.$ext"], $layouts);
146
                }
147 1
                break;
148
            default:
149 1
                $layouts = [
150
                    // "$section/$layout.$format.$ext",
151
                    // "$layout.$format.$ext",
152
                    // "$section/page.$format.$ext",
153
                    // "_default/$layout.$format.$ext",
154
                    // "page.$format.$ext",
155 1
                    "_default/page.$format.$ext",
156 1
                ];
157 1
                $layouts = array_merge(["page.$format.$ext"], $layouts);
158 1
                if ($page->hasVariable('layout')) {
159 1
                    $layouts = array_merge(["_default/$layout.$format.$ext"], $layouts);
160
                }
161 1
                if ($section) {
162 1
                    $layouts = array_merge(["{$section}/page.$format.$ext"], $layouts);
163
                }
164 1
                if ($page->hasVariable('layout')) {
165 1
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts);
166 1
                    if ($section) {
167 1
                        $layouts = array_merge(["{$section}/$layout.$format.$ext"], $layouts);
168
                    }
169
                }
170
        }
171
172
        // add localized layouts
173 1
        if ($page->getVariable('language') !== $config->getLanguageDefault()) {
174 1
            foreach ($layouts as $key => $value) {
175 1
                $layouts = array_merge(\array_slice($layouts, 0, $key), [str_replace(".$ext", ".{$page->getVariable('language')}.$ext", $value)], \array_slice($layouts, $key));
176
            }
177
        }
178
179 1
        return $layouts;
180
    }
181
}
182