Passed
Push — feat/markdown-highlighter ( 1efac5...98434c )
by Arnaud
13:07 queued 08:45
created

Create::hasTaxonomies()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 5
c 1
b 1
f 0
nc 3
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 10
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\Step\Taxonomies;
15
16
use Cecil\Collection\Page\Page;
17
use Cecil\Collection\Taxonomy\Collection as VocabulariesCollection;
18
use Cecil\Collection\Taxonomy\Term;
19
use Cecil\Collection\Taxonomy\Vocabulary;
20
use Cecil\Exception\RuntimeException;
21
use Cecil\Step\AbstractStep;
22
23
/**
24
 * Creates taxonomies collection.
25
 */
26
class Create extends AbstractStep
27
{
28
    /** @var VocabulariesCollection */
29
    protected $vocabCollection;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 1
    public function getName(): string
35
    {
36 1
        return 'Creating taxonomies';
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 1
    public function init(array $options): void
43
    {
44 1
        if (is_dir($this->builder->getConfig()->getContentPath()) && $this->hasTaxonomies()) {
45 1
            $this->canProcess = true;
46
        }
47 1
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1
    public function process(): void
53
    {
54 1
        if ($this->config->get('taxonomies')) {
55 1
            $this->createVocabulariesCollection();
56 1
            $this->builder->getLogger()->info('Vocabularies collection created', ['progress' => [1, 2]]);
57 1
            $this->collectTermsFromPages();
58 1
            $this->builder->getLogger()->info('Terms collection created', ['progress' => [2, 2]]);
59
        }
60
61 1
        $this->builder->setTaxonomies($this->vocabCollection);
62 1
    }
63
64
    /**
65
     * Creates a collection from the vocabularies configuration.
66
     */
67 1
    protected function createVocabulariesCollection(): void
68
    {
69
        // creates an empty a vocabularies collection
70 1
        $this->vocabCollection = new VocabulariesCollection('taxonomies');
71
        /*
72
         * Adds each vocabulary to the collection.
73
         * ie:
74
         *   taxonomies:
75
         *     - tags: tag
76
         *     - categories: category
77
         */
78 1
        foreach (array_keys((array) $this->config->get('taxonomies')) as $vocabulary) {
79
            /*
80
             * Disabled vocabulary?
81
             * ie:
82
             *   taxonomies:
83
             *     tags: disabled
84
             */
85 1
            if ($this->config->get("taxonomies.$vocabulary") == 'disabled') {
86 1
                continue;
87
            }
88
89 1
            $this->vocabCollection->add(new Vocabulary($vocabulary));
90
        }
91 1
    }
92
93
    /**
94
     * Collects vocabularies/terms from pages frontmatter.
95
     */
96 1
    protected function collectTermsFromPages(): void
97
    {
98
        /** @var Page $page */
99
        $pages = $this->builder->getPages()->filter(function (Page $page) {
100 1
            return $page->getVariable('published');
101 1
        })->sortByDate();
102 1
        foreach ($pages as $page) {
103
            // ie: tags
104 1
            foreach ($this->vocabCollection as $vocabulary) {
105 1
                $plural = $vocabulary->getId();
106
                /*
107
                 * ie:
108
                 *   tags: Tag 1, Tag 2
109
                 */
110 1
                if ($page->hasVariable($plural)) {
111
                    // converts a string list to an array
112 1
                    if (!is_array($page->getVariable($plural))) {
113 1
                        $page->setVariable($plural, [$page->getVariable($plural)]);
114
                    }
115
                    // adds each term to the vocabulary collection...
116 1
                    foreach ($page->getVariable($plural) as $termName) {
117 1
                        if (null === $termName) {
118
                            throw new RuntimeException(\sprintf(
119
                                'Taxonomy "%s" of "%s" can\'t be empty.',
120
                                $plural,
121
                                $page->getId()
122
                            ));
123
                        }
124 1
                        $termId = Page::slugify($termName);
125 1
                        $term = (new Term($termId))->setName($termName);
126 1
                        $this->vocabCollection
127 1
                            ->get($plural)
128 1
                            ->add($term);
129
                        // ... and adds page to the term collection
130 1
                        $this->vocabCollection
131 1
                            ->get($plural)
132 1
                            ->get($termId)
133 1
                            ->add($page);
134
                    }
135
                }
136
            }
137
        }
138 1
    }
139
140
    /**
141
     * Checks if there is enabled taxonomies in config.
142
     */
143 1
    private function hasTaxonomies(): bool
144
    {
145 1
        $taxonomiesCount = 0;
146 1
        foreach (array_keys((array) $this->config->get('taxonomies')) as $vocabulary) {
147 1
            if ($this->config->get("taxonomies.$vocabulary") != 'disabled') {
148 1
                $taxonomiesCount++;
149
            }
150
        }
151
152 1
        return $taxonomiesCount > 0;
153
    }
154
}
155