Passed
Push — fix/no-content ( dd2e1d )
by Arnaud
05:13
created

TaxonomiesCreate::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Step;
10
11
use Cecil\Collection\Page\Page;
12
use Cecil\Collection\Taxonomy\Collection as VocabulariesCollection;
13
use Cecil\Collection\Taxonomy\Term as Term;
14
use Cecil\Collection\Taxonomy\Vocabulary as Vocabulary;
15
use Cecil\Exception\Exception;
16
17
/**
18
 * Create taxonomies collection.
19
 */
20
class TaxonomiesCreate extends AbstractStep
21
{
22
    /**
23
     * @var VocabulariesCollection
24
     */
25
    protected $vocabCollection;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function init($options)
31
    {
32
        /** @var \Cecil\Builder $builder */
33
        if (is_dir($this->builder->getConfig()->getContentPath())) {
34
            $this->process = true;
35
        }
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function process()
42
    {
43
        if ($this->config->get('taxonomies')) {
44
            $this->createVocabulariesCollection();
45
            $this->collectTermsFromPages();
46
        }
47
48
        $this->builder->setTaxonomies($this->vocabCollection);
49
    }
50
51
    /**
52
     * Create a collection from the vocabularies configuration.
53
     */
54
    protected function createVocabulariesCollection()
55
    {
56
        // create an empty a vocabularies collection
57
        $this->vocabCollection = new VocabulariesCollection('taxonomies');
58
        /*
59
         * Adds each vocabulary to the collection.
60
         * ie:
61
         *   taxonomies:
62
         *     - tags: tag
63
         *     - categories: category
64
         */
65
        foreach (array_keys((array) $this->config->get('taxonomies')) as $vocabulary) {
66
            /*
67
             * Disabled vocabulary?
68
             * ie:
69
             *   taxonomies:
70
             *     tags: disabled
71
             */
72
            if ($this->config->get("taxonomies.$vocabulary") == 'disabled') {
73
                continue;
74
            }
75
76
            $this->vocabCollection->add(new Vocabulary($vocabulary));
77
        }
78
    }
79
80
    /**
81
     * Collects vocabularies/terms from pages frontmatter.
82
     */
83
    protected function collectTermsFromPages()
84
    {
85
        /* @var $page Page */
86
        $pages = $this->builder->getPages()->sortByDate();
87
        foreach ($pages as $page) {
88
            // ie: tags
89
            foreach ($this->vocabCollection as $vocabulary) {
90
                $plural = $vocabulary->getId();
91
                /*
92
                 * ie:
93
                 *   tags: Tag 1, Tag 2
94
                 */
95
                if ($page->hasVariable($plural)) {
96
                    // converts a string list to an array
97
                    if (!is_array($page->getVariable($plural))) {
98
                        $page->setVariable($plural, [$page->getVariable($plural)]);
99
                    }
100
                    // adds each term to the vocabulary collection...
101
                    foreach ($page->getVariable($plural) as $termName) {
102
                        if (null === $termName) {
103
                            throw new Exception(\sprintf(
104
                                'Taxonomy "%s" of "%s" can\'t be empty.',
105
                                $plural,
106
                                $page->getId()
107
                            ));
108
                        }
109
                        $termId = Page::slugify($termName);
110
                        $term = (new Term($termId))->setName($termName);
111
                        $this->vocabCollection
112
                            ->get($plural)
113
                            ->add($term);
1 ignored issue
show
Bug introduced by
The method add() does not exist on Cecil\Collection\ItemInterface. It seems like you code against a sub-type of Cecil\Collection\ItemInterface such as Cecil\Collection\Menu\Menu or Cecil\Collection\Taxonomy\Term or Cecil\Collection\Taxonomy\Vocabulary. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
                            ->/** @scrutinizer ignore-call */ add($term);
Loading history...
114
                        // ... and adds page to the term collection
115
                        $this->vocabCollection
116
                            ->get($plural)
117
                            ->get($termId)
1 ignored issue
show
Bug introduced by
The method get() does not exist on Cecil\Collection\ItemInterface. Did you maybe mean getId()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
                            ->/** @scrutinizer ignore-call */ get($termId)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
                            ->add($page);
119
                    }
120
                }
121
            }
122
        }
123
    }
124
}
125