Convert::process()   B
last analyzed

Complexity

Conditions 10
Paths 17

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 27
c 1
b 0
f 0
nc 17
nop 0
dl 0
loc 40
rs 7.6666

How to fix   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
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cecil\Step\Pages;
15
16
use Cecil\Builder;
17
use Cecil\Collection\Page\Page;
18
use Cecil\Converter\Converter;
19
use Cecil\Converter\ConverterInterface;
20
use Cecil\Exception\RuntimeException;
21
use Cecil\Step\AbstractStep;
22
use Cecil\Util;
23
24
/**
25
 * Convert step.
26
 *
27
 * This step is responsible for converting pages from their source format
28
 * (i.e. Markdown) to HTML, applying front matter processing,
29
 * and ensuring that the pages are ready for rendering. It handles both
30
 * published and draft pages, depending on the build options.
31
 */
32
class Convert extends AbstractStep
33
{
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getName(): string
38
    {
39
        if ($this->builder->getBuildOptions()['drafts']) {
40
            return 'Converting pages (drafts included)';
41
        }
42
43
        return 'Converting pages';
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function init(array $options): void
50
    {
51
        parent::init($options);
52
53
        if (\is_null($this->builder->getPages())) {
54
            $this->canProcess = false;
55
        }
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function process(): void
62
    {
63
        if (!is_iterable($this->builder->getPages()) || \count($this->builder->getPages()) == 0) {
64
            return;
65
        }
66
67
        $total = \count($this->builder->getPages());
68
        $count = 0;
69
        /** @var Page $page */
70
        foreach ($this->builder->getPages() as $page) {
71
            if (!$page->isVirtual()) {
72
                $count++;
73
74
                try {
75
                    $convertedPage = $this->convertPage($this->builder, $page);
76
                    // set default language (ex: "en") if necessary
77
                    if ($convertedPage->getVariable('language') === null) {
78
                        $convertedPage->setVariable('language', $this->config->getLanguageDefault());
79
                    }
80
                } catch (RuntimeException $e) {
81
                    $this->builder->getLogger()->error(\sprintf('Unable to convert "%s:%s": %s', $e->getFile(), $e->getLine(), $e->getMessage()));
82
                    $this->builder->getPages()->remove($page->getId());
83
                    continue;
84
                } catch (\Exception $e) {
85
                    $this->builder->getLogger()->error(\sprintf('Unable to convert "%s": %s', Util::joinPath(Util\File::getFS()->makePathRelative($page->getFilePath(), $this->config->getPagesPath())), $e->getMessage()));
86
                    $this->builder->getPages()->remove($page->getId());
87
                    continue;
88
                }
89
                $message = \sprintf('Page "%s" converted', $page->getId());
90
                $statusMessage = ' (not published)';
91
                // forces drafts convert?
92
                if ($this->builder->getBuildOptions()['drafts']) {
93
                    $page->setVariable('published', true);
94
                }
95
                // replaces page in collection
96
                if ($page->getVariable('published')) {
97
                    $this->builder->getPages()->replace($page->getId(), $convertedPage);
98
                    $statusMessage = '';
99
                }
100
                $this->builder->getLogger()->info($message . $statusMessage, ['progress' => [$count, $total]]);
101
            }
102
        }
103
    }
104
105
    /**
106
     * Converts page content:
107
     *  - front matter to PHP array
108
     *  - body to HTML.
109
     *
110
     * @throws RuntimeException
111
     */
112
    public function convertPage(Builder $builder, Page $page, ?string $format = null, ?ConverterInterface $converter = null): Page
113
    {
114
        $format = $format ?? (string) $builder->getConfig()->get('pages.frontmatter');
115
        $converter = $converter ?? new Converter($builder);
116
117
        // converts front matter
118
        if ($page->getFrontmatter()) {
119
            try {
120
                $variables = $converter->convertFrontmatter($page->getFrontmatter(), $format);
121
            } catch (RuntimeException $e) {
122
                throw new RuntimeException($e->getMessage(), file: $page->getFilePath(), line: $e->getLine());
123
            }
124
            $page->setFmVariables($variables);
125
            $page->setVariables($variables);
126
        }
127
128
        // converts body (only if page is published or drafts option is enabled)
129
        if ($page->getVariable('published') || $this->options['drafts']) {
130
            try {
131
                $html = $converter->convertBody($page->getBody());
132
            } catch (RuntimeException $e) {
133
                throw new \Exception($e->getMessage());
134
            }
135
            $page->setBodyHtml($html);
136
        }
137
138
        return $page;
139
    }
140
}
141