Passed
Pull Request — master (#1835)
by Arnaud
16:29 queued 11:16
created

Layout::fallback()   C

Complexity

Conditions 15
Paths 24

Size

Total Lines 89
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 53
CRAP Score 15.0108

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 15
eloc 54
c 2
b 0
f 0
nc 24
nop 2
dl 0
loc 89
ccs 53
cts 55
cp 0.9636
crap 15.0108
rs 5.9166

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(["$layout.$format.$ext"], $layouts);
101
                }
102 1
                break;
103
            case PageType::SECTION:
104 1
                $layouts = [
105
                    // "$layout.$format.$ext",
106
                    // "$section/index.$format.$ext",
107
                    // "$section/list.$format.$ext",
108
                    // "section/$section.$format.$ext",
109 1
                    "_default/section.$format.$ext",
110 1
                    "_default/list.$format.$ext",
111 1
                ];
112 1
                if ($page->getPath()) {
113 1
                    $layouts = array_merge(["section/{$page->getSection()}.$format.$ext"], $layouts);
114 1
                    $layouts = array_merge(["{$page->getSection()}/list.$format.$ext"], $layouts);
115 1
                    $layouts = array_merge(["{$page->getSection()}/index.$format.$ext"], $layouts);
116
                }
117 1
                if ($page->hasVariable('layout')) {
118
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts);
119
                }
120 1
                break;
121
            case PageType::VOCABULARY:
122 1
                $layouts = [
123
                    // "taxonomy/$plural.$format.$ext", // e.g.: taxonomy/tags.html.twig
124 1
                    "_default/vocabulary.$format.$ext", // e.g.: _default/vocabulary.html.twig
125 1
                ];
126 1
                if ($page->hasVariable('plural')) {
127 1
                    $layouts = array_merge(["taxonomy/{$page->getVariable('plural')}.$format.$ext"], $layouts);
128
                }
129 1
                break;
130
            case PageType::TERM:
131 1
                $layouts = [
132
                    // "taxonomy/$term.$format.$ext",     // e.g.: taxonomy/velo.html.twig
133
                    // "taxonomy/$singular.$format.$ext", // e.g.: taxonomy/tag.html.twig
134 1
                    "_default/term.$format.$ext",         // e.g.: _default/term.html.twig
135 1
                    "_default/list.$format.$ext",         // e.g.: _default/list.html.twig
136 1
                ];
137 1
                if ($page->hasVariable('term')) {
138 1
                    $layouts = array_merge(["taxonomy/{$page->getVariable('term')}.$format.$ext"], $layouts);
139
                }
140 1
                if ($page->hasVariable('singular')) {
141 1
                    $layouts = array_merge(["taxonomy/{$page->getVariable('singular')}.$format.$ext"], $layouts);
142
                }
143 1
                break;
144
            default:
145 1
                $layouts = [
146
                    // "$section/$layout.$format.$ext",
147
                    // "$layout.$format.$ext",
148
                    // "$section/page.$format.$ext",
149
                    // "page.$format.$ext",
150
                    // "_default/$layout.$format.$ext",
151 1
                    "_default/page.$format.$ext",
152 1
                ];
153 1
                if ($page->hasVariable('layout')) {
154 1
                    $layouts = array_merge(["_default/$layout.$format.$ext"], $layouts);
155
                }
156 1
                $layouts = array_merge(["page.$format.$ext"], $layouts);
157 1
                if ($page->getSection()) {
158 1
                    $layouts = array_merge(["{$page->getSection()}/page.$format.$ext"], $layouts);
159
                }
160 1
                if ($page->hasVariable('layout')) {
161 1
                    $layouts = array_merge(["$layout.$format.$ext"], $layouts);
162 1
                    if ($page->getSection()) {
163 1
                        $layouts = array_merge(["{$page->getSection()}/$layout.$format.$ext"], $layouts);
164
                    }
165
                }
166
        }
167
168 1
        return $layouts;
169
    }
170
}
171