Passed
Pull Request — master (#1704)
by Arnaud
22:27 queued 14:59
created

Create   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 95.92%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 42
c 2
b 0
f 0
dl 0
loc 107
ccs 47
cts 49
cp 0.9592
rs 10
wmc 18

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A init() 0 6 2
C process() 0 82 15
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\Pages;
15
16
use Cecil\Collection\Page\Collection as PagesCollection;
17
use Cecil\Collection\Page\Page;
18
use Cecil\Step\AbstractStep;
19
20
/**
21
 * Creates Pages collection from content iterator.
22
 */
23
class Create extends AbstractStep
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 1
    public function getName(): string
29
    {
30 1
        return 'Creating pages';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public function init(array $options): void
37
    {
38 1
        $this->builder->setPages(new PagesCollection('all-pages'));
39
40 1
        if (is_dir($this->config->getPagesPath())) {
41 1
            $this->canProcess = true;
42
        }
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function process(): void
49
    {
50 1
        if (\count($this->builder->getPagesFiles()) == 0) {
51
            return;
52
        }
53
54 1
        $total = \count($this->builder->getPagesFiles());
55 1
        $count = 0;
56 1
        foreach ($this->builder->getPagesFiles() as $file) {
57 1
            $count++;
58
            // create a page from its (Markdown) file
59 1
            $page = new Page(Page::createIdFromFile($file));
60 1
            $page->setFile($file);
61
            // parse frontmatter and body
62 1
            $page->parse();
63
64
            /*
65
             * Set parent page
66
             */
67
            // if root page or section: home
68
            /*if (empty($page->getFolder()) || $page->getType() == \Cecil\Collection\Page\Type::SECTION->value) {
69
                $page->setParent($this->builder->getPages()->get('index'));
70
            }*/
71
            // if section page: parent is section
72 1
            if ($page->getSection() !== null && $this->builder->getPages()->has($page->getSection())) {
73 1
                $page->setParent($this->builder->getPages()->get($page->getSection()));
74
            }
75
            // if sub page: parent is "folder"
76 1
            if (!empty($page->getFolder()) && $this->builder->getPages()->has($page->getFolder())) {
77 1
                $page->setParent($this->builder->getPages()->get($page->getFolder()));
78
            }
79
80
            /*
81
             * Apply an - optional - custom path to pages of a section.
82
             *
83
             * ```yaml
84
             * paths:
85
             *   - section: Blog
86
             *     language: fr # optional
87
             *     path: :section/:year/:month/:day/:slug
88
             * ```
89
             */
90 1
            if (\is_array($this->config->get('paths'))) {
91 1
                foreach ($this->config->get('paths') as $entry) {
92 1
                    if (isset($entry['section'])) {
93
                        /** @var Page $page */
94 1
                        if ($page->getSection() == Page::slugify($entry['section'])) {
95 1
                            if (isset($entry['language']) && $entry['language'] != $page->getVariable('language')) {
96
                                break;
97
                            }
98 1
                            if (isset($entry['path'])) {
99 1
                                $path = str_replace(
100 1
                                    [
101 1
                                        ':year',
102 1
                                        ':month',
103 1
                                        ':day',
104 1
                                        ':section',
105 1
                                        ':slug',
106 1
                                    ],
107 1
                                    [
108 1
                                        $page->getVariable('date')->format('Y'),
109 1
                                        $page->getVariable('date')->format('m'),
110 1
                                        $page->getVariable('date')->format('d'),
111 1
                                        $page->getSection(),
112 1
                                        $page->getSlug(),
113 1
                                    ],
114 1
                                    $entry['path']
115 1
                                );
116 1
                                $page->setPath(trim($path, '/'));
117
                            }
118
                        }
119
                    }
120
                }
121
            }
122
123
            // add the page to pages collection only if its language is defined in configuration
124 1
            if (\in_array($page->getVariable('language', $this->config->getLanguageDefault()), array_column($this->config->getLanguages(), 'code'))) {
125 1
                $this->builder->getPages()->add($page);
126
            }
127
128 1
            $message = sprintf('Page "%s" (%s) created', $page->getId(), $page->getType());
129 1
            $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
130
        }
131
    }
132
}
133