Passed
Pull Request — master (#1711)
by Arnaud
10:18 queued 04:14
created

Taxonomy::generate()   B

Complexity

Conditions 11
Paths 24

Size

Total Lines 62
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 11.085

Importance

Changes 0
Metric Value
cc 11
eloc 45
nc 24
nop 0
dl 0
loc 62
ccs 41
cts 45
cp 0.9111
crap 11.085
rs 7.3166
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
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 1
    public function generate(): void
29
    {
30 1
        foreach ($this->config->getLanguages() as $lang) {
31 1
            $language = $lang['code'];
32 1
            if ($this->config->get('taxonomies', $language, false) && $this->builder->getTaxonomies($language) !== null) {
33
                /** @var Vocabulary $vocabulary */
34 1
                foreach ($this->builder->getTaxonomies($language) as $vocabulary) {
35 1
                    $plural = $vocabulary->getId();
36 1
                    $singular = $this->config->get("taxonomies.$plural", $language, false);
37 1
                    if (\count($vocabulary) > 0) {
38 1
                        $date = date('Y-m-d');
39
                        /*
40
                        * Creates $plural/$term pages (list of pages)
41
                        * e.g.: /tags/tag-1/
42
                        */
43 1
                        foreach ($vocabulary as $term) {
44 1
                            $pageId = $path = Page::slugify($term->getId());
45 1
                            if ($language != $this->config->getLanguageDefault()) {
46 1
                                $pageId = "$language/$pageId";
47
                            }
48 1
                            $pages = $term->sortByDate();
49 1
                            $date = $pages->first()->getVariable('date');
50 1
                            $page = (new Page($pageId))
51 1
                                ->setPath($path)
52 1
                                ->setVariable('title', $term->getName())
53 1
                                ->setVariable('date', $date)
54 1
                                ->setVariable('language', $language);
55 1
                            if ($this->builder->getPages()->has($pageId)) {
56
                                $page = clone $this->builder->getPages()->get($pageId);
57
                            }
58
                            /** @var Page $page */
59 1
                            $page
60 1
                                ->setType(Type::TERM)
61 1
                                ->setPages($pages)
62 1
                                ->setVariable('term', $term->getId())
63 1
                                ->setVariable('plural', $plural)
64 1
                                ->setVariable('singular', $singular);
65 1
                            $this->generatedPages->add($page);
66
                        }
67
                        /*
68
                        * Creates $plural pages (list of terms)
69
                        * e.g.: /tags/
70
                        */
71 1
                        $pageId = $path = Page::slugify($plural);
72 1
                        if ($language != $this->config->getLanguageDefault()) {
73 1
                            $pageId = "$language/$pageId";
74
                        }
75 1
                        $page = (new Page($pageId))
76 1
                            ->setType(Type::VOCABULARY)
77 1
                            ->setPath($path)
78 1
                            ->setTerms($vocabulary)
79 1
                            ->setVariable('title', ucfirst($plural))
80 1
                            ->setVariable('date', $date)
81 1
                            ->setVariable('plural', $plural)
82 1
                            ->setVariable('singular', $singular)
83 1
                            ->setVariable('language', $language);
84
                        // adds page only if a template exist
85
                        try {
86 1
                            $this->generatedPages->add($page);
87
                        } catch (\Exception $e) {
88
                            printf("%s\n", $e->getMessage());
89
                            unset($page); // do not adds page
90
                        }
91
                    }
92
                }
93
            }
94
        }
95
    }
96
}
97