Completed
Push — internationalization ( bf1b4f...6a2add )
by Arnaud
01:56
created

Section::generate()   C

Complexity

Conditions 11
Paths 81

Size

Total Lines 54

Duplication

Lines 6
Ratio 11.11 %

Importance

Changes 0
Metric Value
dl 6
loc 54
rs 6.8569
c 0
b 0
f 0
cc 11
nc 81
nop 0

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\Generator;
10
11
use Cecil\Collection\Page\Collection as PagesCollection;
12
use Cecil\Collection\Page\Page;
13
use Cecil\Collection\Page\Type;
14
15
/**
16
 * Class Section.
17
 */
18
class Section extends AbstractGenerator implements GeneratorInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function generate(): void
24
    {
25
        $sections = [];
26
27
        // identify sections
28
        /* @var $page Page */
29
        foreach ($this->pagesCollection as $page) {
30
            if ($page->getSection()) {
31
                $sections[$page->getSection()][] = $page;
32
            }
33
        }
34
35
        // adds section to pages collection
36
        if (count($sections) > 0) {
37
            $menuWeight = 100;
38
            foreach ($sections as $section => $pagesAsArray) {
39
                // excludes "sections" based on language code
40
                if (!empty($this->config->getLanguages())) {
41
                    if (in_array($section, array_keys($this->config->getLanguages()))) {
42
                        continue;
43
                    }
44
                }
45
                $pageId = $path = Page::slugify($section);
46
                $page = (new Page($pageId))->setVariable('title', ucfirst($section));
47
                if ($this->pagesCollection->has($pageId)) {
48
                    $page = clone $this->pagesCollection->get($pageId);
49
                }
50
                $pages = new PagesCollection($section, $pagesAsArray);
51
                // sort
52
                $pages = $pages->sortByDate();
53 View Code Duplication
                if ($page->getVariable('sortby')) {
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...
54
                    $sortMethod = sprintf('sortBy%s', ucfirst($page->getVariable('sortby')));
55
                    if (method_exists($pages, $sortMethod)) {
56
                        $pages = $pages->$sortMethod();
57
                    }
58
                }
59
                // add navigation links
60
                $this->addNavigationLinks($pages);
61
                // create page for each section
62
                $page->setPath($path)
63
                    ->setType(Type::SECTION)
64
                    ->setVariable('pages', $pages)
65
                    ->setVariable('date', $pages->first()->getVariable('date'));
66
                // default menu
67
                if (!$page->getVariable('menu')) {
68
                    $page->setVariable('menu', [
69
                        'main' => ['weight' => $menuWeight],
70
                    ]);
71
                }
72
                $this->generatedPages->add($page);
73
                $menuWeight += 10;
74
            }
75
        }
76
    }
77
78
    /**
79
     * Add pagination (next and prev) to section sub pages.
80
     *
81
     * @param PagesCollection $pages
82
     *
83
     * @return void
84
     */
85
    protected function addNavigationLinks(PagesCollection $pages): void
86
    {
87
        $pagesAsArray = $pages->toArray();
88
        if (count($pagesAsArray) > 1) {
89
            foreach ($pagesAsArray as $position => $page) {
90
                switch ($position) {
91
                    // first
92
                    case 0:
93
                        $page->setVariables([
94
                            'next' => [
95
                                'id'    => $pagesAsArray[$position + 1]->getId(),
96
                                'path'  => $pagesAsArray[$position + 1]->getPath(),
97
                                'title' => $pagesAsArray[$position + 1]->getVariable('title'),
98
                            ],
99
                        ]);
100
                        break;
101
                    // last
102
                    case count($pagesAsArray) - 1:
103
                        $page->setVariables([
104
                            'prev' => [
105
                                'id'    => $pagesAsArray[$position - 1]->getId(),
106
                                'path'  => $pagesAsArray[$position - 1]->getPath(),
107
                                'title' => $pagesAsArray[$position - 1]->getVariable('title'),
108
                            ],
109
                        ]);
110
                        break;
111
                    default:
112
                        $page->setVariables([
113
                            'prev' => [
114
                                'id'    => $pagesAsArray[$position - 1]->getId(),
115
                                'path'  => $pagesAsArray[$position - 1]->getPath(),
116
                                'title' => $pagesAsArray[$position - 1]->getVariable('title'),
117
                            ],
118
                            'next' => [
119
                                'id'    => $pagesAsArray[$position + 1]->getId(),
120
                                'path'  => $pagesAsArray[$position + 1]->getPath(),
121
                                'title' => $pagesAsArray[$position + 1]->getVariable('title'),
122
                            ],
123
                        ]);
124
                        break;
125
                }
126
                $this->generatedPages->add($page);
127
            }
128
        }
129
    }
130
}
131