Passed
Pull Request — master (#1711)
by Arnaud
08:22 queued 03:22
created

Taxonomy   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 91.11%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 67
ccs 41
cts 45
cp 0.9111
rs 10
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B generate() 0 62 11
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('language', $language)
82 1
                            ->setVariable('plural', $plural)
83 1
                            ->setVariable('singular', $singular);
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