Passed
Pull Request — master (#1013)
by lee
07:38
created

Taxonomy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 58
ccs 33
cts 36
cp 0.9167
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B generate() 0 53 8
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Generator;
12
13
use Cecil\Collection\Page\Collection as PagesCollection;
14
use Cecil\Collection\Page\Page;
15
use Cecil\Collection\Page\Type;
16
use Cecil\Collection\Taxonomy\Vocabulary as Vocabulary;
17
use Cecil\Exception\Exception;
18
19
/**
20
 * Class Generator\Taxonomy.
21
 */
22
class Taxonomy extends AbstractGenerator implements GeneratorInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 1
    public function generate(): void
28
    {
29 1
        if ($this->config->get('taxonomies') && $this->builder->getTaxonomies() !== null) {
30
            /** @var Vocabulary $vocabulary */
31 1
            foreach ($this->builder->getTaxonomies() as $vocabulary) {
32 1
                $plural = $vocabulary->getId();
33 1
                $singular = $this->config->get("taxonomies.$plural");
34 1
                if (count($vocabulary) > 0) {
35 1
                    $date = date('Y-m-d');
36
                    /*
37
                     * Creates $plural/$term pages (list of pages)
38
                     * ie: /tags/tag-1/
39
                     */
40
                    /** @var PagesCollection $pages */
41 1
                    foreach ($vocabulary as $term) {
42 1
                        $pageId = $path = Page::slugify(sprintf('%s/%s', $plural, $term->getId()));
43 1
                        $pages = $term->sortByDate();
44 1
                        $date = $pages->first()->getVariable('date');
45 1
                        $page = (new Page($pageId))
46 1
                            ->setVariable('title', $term->getName());
47 1
                        if ($this->builder->getPages()->has($pageId)) {
48
                            $page = clone $this->builder->getPages()->get($pageId);
49
                        }
50
                        /** @var Page $page */
51
                        $page
52 1
                            ->setType(Type::TERM)
53 1
                            ->setPath($path)
54 1
                            ->setVariable('date', $date)
55 1
                            ->setVariable('term', $term->getId())
56 1
                            ->setVariable('plural', $plural)
57 1
                            ->setVariable('singular', $singular)
58 1
                            ->setVariable('pages', $pages);
59 1
                        $this->generatedPages->add($page);
60
                    }
61
                    /*
62
                     * Creates $plural pages (list of terms)
63
                     * ex: /tags/
64
                     */
65 1
                    $pageId = $path = Page::slugify($plural);
66 1
                    $page = (new Page($pageId))
67 1
                        ->setType(Type::VOCABULARY)
68 1
                        ->setPath($path)
69 1
                        ->setVariable('title', ucfirst($plural))
70 1
                        ->setVariable('date', $date)
71 1
                        ->setVariable('plural', $plural)
72 1
                        ->setVariable('singular', $singular)
73 1
                        ->setVariable('terms', $vocabulary);
74
                    // adds page only if a template exist
75
                    try {
76 1
                        $this->generatedPages->add($page);
77
                    } catch (Exception $e) {
78
                        printf("%s\n", $e->getMessage());
79 1
                        unset($page); // do not adds page
80
                    }
81
                }
82
            }
83
        }
84 1
    }
85
}
86