Passed
Push — plural ( aebfd5 )
by Arnaud
04:13
created

Taxonomy::generate()   C

Complexity

Conditions 13
Paths 84

Size

Total Lines 69
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 48
c 1
b 0
f 0
nc 84
nop 0
dl 0
loc 69
rs 6.6166

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\Generator;
15
16
use Cecil\Collection\Page\Page;
17
use Cecil\Collection\Page\Type;
18
use Cecil\Collection\Taxonomy\Vocabulary;
19
20
/**
21
 * Class Generator\Taxonomy.
22
 */
23
class Taxonomy extends AbstractGenerator implements GeneratorInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function generate(): void
29
    {
30
        foreach ($this->config->getLanguages() as $lang) {
31
            $language = $lang['code'];
32
            if ($this->config->get('taxonomies', $language, false) && $this->builder->getTaxonomies($language) !== null) {
33
                /** @var Vocabulary $vocabulary */
34
                foreach ($this->builder->getTaxonomies($language) as $vocabulary) {
35
                    $plural = $vocabulary->getId();
36
                    $singular = $this->config->get("taxonomies.$plural", $language, false);
37
                    if (\count($vocabulary) > 0) {
38
                        $date = date('Y-m-d');
39
                        /*
40
                        * Creates $plural/$term pages (list of pages)
41
                        * e.g.: /tags/tag-1/
42
                        */
43
                        foreach ($vocabulary as $term) {
44
                            $pageId = $path = Page::slugify($term->getId());
45
                            if ($language != $this->config->getLanguageDefault()) {
46
                                $pageId = "$language/$pageId";
47
                            }
48
                            $pages = $term->sortByDate();
49
                            $date = $pages->first()->getVariable('date');
50
                            // creates page for each term
51
                            $page = (new Page($pageId))
52
                                ->setPath($path)
53
                                ->setVariable('title', $term->getName())
54
                                ->setVariable('date', $date)
55
                                ->setVariable('language', $language);
56
                            if ($this->builder->getPages()->has($pageId)) {
57
                                $page = clone $this->builder->getPages()->get($pageId);
58
                            }
59
                            $page->setType(Type::TERM)
60
                                ->setPages($pages)
61
                                ->setVariable('term', $term->getId())
62
                                ->setVariable('plural', $plural)
63
                                ->setVariable('singular', $singular);
64
                            $this->generatedPages->add($page);
65
                        }
66
                        /*
67
                        * Creates $plural pages (list of terms)
68
                        * e.g.: /tags/
69
                        */
70
                        $pageId = $path = Page::slugify($plural);
71
                        if ($language != $this->config->getLanguageDefault()) {
72
                            $pageId = "$language/$pageId";
73
                        }
74
                        $page = (new Page($pageId))->setVariable('title', ucfirst($plural))
75
                            ->setPath($path);
76
                        if ($this->builder->getPages()->has($pageId)) {
77
                            $page = clone $this->builder->getPages()->get($pageId);
78
                        }
79
                        // creates page for each plural
80
                        $page->setType(Type::VOCABULARY)
81
                            ->setPath($path)
82
                            ->setTerms($vocabulary)
83
                            ->setVariable('date', $date)
84
                            ->setVariable('language', $language)
85
                            ->setVariable('plural', $plural)
86
                            ->setVariable('singular', $singular);
87
                        // human readable title
88
                        if ($page->getVariable('title') == 'index') {
89
                            $page->setVariable('title', $plural);
90
                        }
91
                        // adds page only if a template exist
92
                        try {
93
                            $this->generatedPages->add($page);
94
                        } catch (\Exception $e) {
95
                            printf("%s\n", $e->getMessage());
96
                            unset($page); // do not adds page
97
                        }
98
                    }
99
                }
100
            }
101
        }
102
    }
103
}
104