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

Taxonomy::generate()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 53
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 8.0368

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 37
c 1
b 0
f 0
nc 9
nop 0
dl 0
loc 53
ccs 33
cts 36
cp 0.9167
crap 8.0368
rs 8.0835

How to fix   Long Method   

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
 * 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