Taxonomy::generate()   C
last analyzed

Complexity

Conditions 13
Paths 84

Size

Total Lines 71
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 50
nc 84
nop 0
dl 0
loc 71
rs 6.6166
c 0
b 0
f 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
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
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
 * Taxonomy generator class.
22
 *
23
 * This class generates pages for taxonomies (vocabularies and terms) based on the configuration
24
 * and the taxonomies defined in the builder. It creates pages for each term in a vocabulary,
25
 * as well as a page for the vocabulary itself. The generated pages are added to the collection
26
 * of generated pages, and they include variables such as title, date, language, plural, and singular
27
 * forms of the vocabulary.
28
 */
29
class Taxonomy extends AbstractGenerator implements GeneratorInterface
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function generate(): void
35
    {
36
        foreach ($this->config->getLanguages() as $lang) {
37
            $language = $lang['code'];
38
            if ($this->config->get('taxonomies', $language, false) && $this->builder->getTaxonomies($language) !== null) {
39
                /** @var Vocabulary $vocabulary */
40
                foreach ($this->builder->getTaxonomies($language) as $vocabulary) {
41
                    $plural = $vocabulary->getId();
42
                    $singular = $this->config->get("taxonomies.$plural", $language, false);
43
                    if (\count($vocabulary) > 0) {
44
                        $date = date('Y-m-d');
45
                        /*
46
                        * Creates $plural/$term pages (list of pages)
47
                        * e.g.: /tags/tag-1/
48
                        */
49
                        foreach ($vocabulary as $term) {
50
                            $pageId = $path = Page::slugify($term->getId());
51
                            if ($language != $this->config->getLanguageDefault()) {
52
                                $pageId = "$language/$pageId";
53
                            }
54
                            $pages = $term->sortByDate();
55
                            $date = $pages->first()->getVariable('date');
56
                            // creates page for each term
57
                            $page = (new Page($pageId))
58
                                ->setPath($path)
59
                                ->setVariable('title', $term->getName())
60
                                ->setVariable('date', $date)
61
                                ->setVariable('language', $language)
62
                                ->setVariable('langref', $path);
63
                            if ($this->builder->getPages()->has($pageId)) {
64
                                $page = clone $this->builder->getPages()->get($pageId);
65
                            }
66
                            $page->setType(Type::TERM->value)
67
                                ->setPages($pages)
68
                                ->setVariable('term', $term->getId())
69
                                ->setVariable('plural', $plural)
70
                                ->setVariable('singular', $singular);
71
                            $this->generatedPages->add($page);
72
                        }
73
                        /*
74
                        * Creates $plural pages (list of terms)
75
                        * e.g.: /tags/
76
                        */
77
                        $pageId = $path = Page::slugify($plural);
78
                        if ($language != $this->config->getLanguageDefault()) {
79
                            $pageId = "$language/$pageId";
80
                        }
81
                        $page = (new Page($pageId))->setVariable('title', ucfirst($plural))
82
                            ->setPath($path);
83
                        if ($this->builder->getPages()->has($pageId)) {
84
                            $page = clone $this->builder->getPages()->get($pageId);
85
                            $page->unSection();
86
                        }
87
                        // creates page for each plural
88
                        $page->setType(Type::VOCABULARY->value)
89
                            ->setPath($path)
90
                            ->setTerms($vocabulary)
91
                            ->setVariable('date', $date)
92
                            ->setVariable('language', $language)
93
                            ->setVariable('plural', $plural)
94
                            ->setVariable('singular', $singular);
95
                        // human readable title
96
                        if ($page->getVariable('title') == 'index') {
97
                            $page->setVariable('title', $plural);
98
                        }
99
                        // adds page only if a template exist
100
                        try {
101
                            $this->generatedPages->add($page);
102
                        } catch (\Exception $e) {
103
                            printf("%s\n", $e->getMessage());
104
                            unset($page); // do not adds page
105
                        }
106
                    }
107
                }
108
            }
109
        }
110
    }
111
}
112