Completed
Push — include-lib ( fd24a6...4173d3 )
by Arnaud
13:24
created

Layout::fallback()   C

Complexity

Conditions 11
Paths 13

Size

Total Lines 87

Duplication

Lines 25
Ratio 28.74 %

Importance

Changes 0
Metric Value
dl 25
loc 87
rs 6.1369
c 0
b 0
f 0
cc 11
nc 13
nop 1

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
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Renderer;
10
11
use Cecil\Collection\Page\Page;
12
use Cecil\Config;
13
use Cecil\Exception\Exception;
14
use Cecil\Page\NodeType;
15
use Cecil\Util;
16
17
/**
18
 * Class Layout.
19
 */
20
class Layout
21
{
22
    /**
23
     * Layout file finder.
24
     *
25
     * @param Page   $page
26
     * @param Config $config
27
     *
28
     * @throws Exception
29
     *
30
     * @return string
31
     */
32
    public function finder(Page $page, Config $config)
33
    {
34
        $layout = 'unknown';
35
36
        // what layouts could be use for the page?
37
        $layouts = self::fallback($page);
38
39
        // take the first available layout
40
        foreach ($layouts as $layout) {
41
            // is it in layouts/ dir?
42
            if (Util::getFS()->exists($config->getLayoutsPath().'/'.$layout)) {
43
                return $layout;
44
            }
45
            // is it in <theme>/layouts/ dir?
46
            if ($config->hasTheme()) {
47
                $themes = $config->getTheme();
48
                foreach ($themes as $theme) {
49
                    if (Util::getFS()->exists($config->getThemeDirPath($theme, 'layouts').'/'.$layout)) {
50
                        return $layout;
51
                    }
52
                }
53
            }
54
            // is it in res/layouts/ dir?
55
            if (Util::getFS()->exists($config->getInternalLayoutsPath().'/'.$layout)) {
56
                return $layout;
57
            }
58
        }
59
60
        throw new Exception(sprintf("Layout '%s' not found for page '%s'!", $layout, $page->getId()));
61
    }
62
63
    /**
64
     * Layout fall-back.
65
     *
66
     * @param $page
67
     *
68
     * @return string[]
69
     *
70
     * @see finder()
71
     */
72
    protected static function fallback(Page $page)
73
    {
74
        // remove redundant '.twig' extension
75
        $layout = str_replace('.twig', '', $page->getLayout());
76
77
        switch ($page->getNodeType()) {
78
            case NodeType::HOMEPAGE:
79
                $layouts = [
80
                    'index.html.twig',
81
                    '_default/list.html.twig',
82
                    '_default/page.html.twig',
83
                ];
84
                break;
85
            case NodeType::SECTION:
86
                $layouts = [
87
                    // 'section/$section.html.twig',
88
                    '_default/section.html.twig',
89
                    '_default/list.html.twig',
90
                ];
91
                if ($page->getPathname()) {
92
                    $section = explode('/', $page->getPathname())[0];
93
                    $layouts = array_merge(
94
                        [sprintf('section/%s.html.twig', $section)],
95
                        $layouts
96
                    );
97
                }
98
                break;
99 View Code Duplication
            case NodeType::TAXONOMY:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
                $layouts = [
101
                    // 'taxonomy/$singular.html.twig',
102
                    '_default/taxonomy.html.twig',
103
                    '_default/list.html.twig',
104
                ];
105
                if ($page->getVariable('singular')) {
106
                    $layouts = array_merge(
107
                        [sprintf('taxonomy/%s.html.twig', $page->getVariable('singular'))],
108
                        $layouts
109
                    );
110
                }
111
                break;
112 View Code Duplication
            case NodeType::TERMS:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
                $layouts = [
114
                    // 'taxonomy/$singular.terms.html.twig',
115
                    '_default/terms.html.twig',
116
                ];
117
                if ($page->getVariable('singular')) {
118
                    $layouts = array_merge(
119
                        [sprintf('taxonomy/%s.terms.html.twig', $page->getVariable('singular'))],
120
                        $layouts
121
                    );
122
                }
123
                break;
124
            default:
125
                $layouts = [
126
                    // '$section/$layout.twig',
127
                    // '$layout.twig',
128
                    // '$section/page.html.twig',
129
                    // 'page.html.twig',
130
                    '_default/page.html.twig',
131
                ];
132
                $layouts = array_merge(
133
                    ['page.html.twig'],
134
                    $layouts
135
                );
136
137
                if ($page->getSection()) {
138
                    $layouts = array_merge(
139
                        [sprintf('%s/page.html.twig', $page->getSection())],
140
                        $layouts
141
                    );
142
                }
143
                if ($page->getLayout()) {
144
                    $layouts = array_merge(
145
                        [sprintf('%s.twig', $layout)],
146
                        $layouts
147
                    );
148
                    if ($page->getSection()) {
149
                        $layouts = array_merge(
150
                            [sprintf('%s/%s.twig', $page->getSection(), $layout)],
151
                            $layouts
152
                        );
153
                    }
154
                }
155
        }
156
157
        return $layouts;
158
    }
159
}
160