Passed
Push — fix/serve-timeout ( 697f77...233bc8 )
by Arnaud
04:27
created

Create::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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