Passed
Push — master ( fda67a...b301ed )
by Arnaud
05:18
created

Taxonomy   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 50
c 2
b 1
f 0
dl 0
loc 75
ccs 42
cts 49
cp 0.8571
rs 10
wmc 13

1 Method

Rating   Name   Duplication   Size   Complexity  
C generate() 0 70 13
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
                            // creates page for each term
51 1
                            $page = (new Page($pageId))
52 1
                                ->setPath($path)
53 1
                                ->setVariable('title', $term->getName())
54 1
                                ->setVariable('date', $date)
55 1
                                ->setVariable('language', $language);
56 1
                            if ($this->builder->getPages()->has($pageId)) {
57
                                $page = clone $this->builder->getPages()->get($pageId);
58
                            }
59 1
                            $page->setType(Type::TERM)
60 1
                                ->setPages($pages)
61 1
                                ->setVariable('term', $term->getId())
62 1
                                ->setVariable('plural', $plural)
63 1
                                ->setVariable('singular', $singular);
64 1
                            $this->generatedPages->add($page);
65
                        }
66
                        /*
67
                        * Creates $plural pages (list of terms)
68
                        * e.g.: /tags/
69
                        */
70 1
                        $pageId = $path = Page::slugify($plural);
71 1
                        if ($language != $this->config->getLanguageDefault()) {
72 1
                            $pageId = "$language/$pageId";
73
                        }
74 1
                        $page = (new Page($pageId))->setVariable('title', ucfirst($plural))
75 1
                            ->setPath($path);
76 1
                        if ($this->builder->getPages()->has($pageId)) {
77
                            $page = clone $this->builder->getPages()->get($pageId);
78
                            $page->unSection();
79
                        }
80
                        // creates page for each plural
81 1
                        $page->setType(Type::VOCABULARY)
82 1
                            ->setPath($path)
83 1
                            ->setTerms($vocabulary)
84 1
                            ->setVariable('date', $date)
85 1
                            ->setVariable('language', $language)
86 1
                            ->setVariable('plural', $plural)
87 1
                            ->setVariable('singular', $singular);
88
                        // human readable title
89 1
                        if ($page->getVariable('title') == 'index') {
90
                            $page->setVariable('title', $plural);
91
                        }
92
                        // adds page only if a template exist
93
                        try {
94 1
                            $this->generatedPages->add($page);
95
                        } catch (\Exception $e) {
96
                            printf("%s\n", $e->getMessage());
97
                            unset($page); // do not adds page
98
                        }
99
                    }
100
                }
101
            }
102
        }
103
    }
104
}
105