Passed
Push — nested-sections ( 54d13d...96df21 )
by Arnaud
03:28
created

Create::process()   C

Complexity

Conditions 13
Paths 32

Size

Total Lines 72
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 13.0245

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 13
eloc 36
c 1
b 1
f 0
nc 32
nop 0
dl 0
loc 72
ccs 36
cts 38
cp 0.9474
crap 13.0245
rs 6.6166

How to fix   Long Method    Complexity   

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
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
        foreach ($this->builder->getPagesFiles() as $file) {
57 1
            $count++;
58 1
            // create a page from its (Markdown) file
59
            $page = new Page(Page::createIdFromFile($file));
60 1
            $page->setFile($file);
61 1
            // parse frontmatter and body
62
            $page->parse();
63
            // has a parent page?
64
            if (count(explode('/', $page->getFolder())) > 1) {
65
                if ($this->builder->getPages()->has($page->getFolder())) {
66
                    $page->setParent($this->builder->getPages()->get($page->getFolder()));
67
                }
68
            }
69
70
            /*
71
             * Apply an - optional - custom path to pages of a section.
72
             *
73 1
             * ```yaml
74 1
             * paths:
75 1
             *   - section: Blog
76
             *     language: fr # optional
77 1
             *     path: :section/:year/:month/:day/:slug
78 1
             * ```
79
             */
80
            if (\is_array($this->config->get('paths'))) {
81 1
                foreach ($this->config->get('paths') as $entry) {
82 1
                    if (isset($entry['section'])) {
83 1
                        /** @var Page $page */
84 1
                        if ($page->getSection() == Page::slugify($entry['section'])) {
85 1
                            if ((isset($entry['language']) && $entry['language'] != $page->getVariable('language'))) {
86 1
                                break;
87 1
                            }
88 1
                            if (isset($entry['path'])) {
89 1
                                $path = str_replace(
90 1
                                    [
91 1
                                        ':year',
92 1
                                        ':month',
93 1
                                        ':day',
94 1
                                        ':section',
95 1
                                        ':slug',
96 1
                                    ],
97 1
                                    [
98 1
                                        $page->getVariable('date')->format('Y'),
99 1
                                        $page->getVariable('date')->format('m'),
100
                                        $page->getVariable('date')->format('d'),
101
                                        $page->getSection(),
102
                                        $page->getSlug(),
103
                                    ],
104
                                    $entry['path']
105
                                );
106
                                $page->setPath(trim($path, '/'));
107 1
                            }
108 1
                        }
109
                    }
110
                }
111 1
            }
112 1
113
            // add the page to pages collection only if its language is defined in configuration
114
            if (\in_array($page->getVariable('language', $this->config->getLanguageDefault()), array_column($this->config->getLanguages(), 'code'))) {
115
                $this->builder->getPages()->add($page);
116
            }
117
118
            $message = sprintf('Page "%s" (%s) created', $page->getId(), $page->getType());
119
            $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
120
        }
121
    }
122
}
123