Passed
Pull Request — master (#2117)
by Arnaud
19:02 queued 12:57
created

Convert::process()   B

Complexity

Conditions 9
Paths 17

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 9.9957

Importance

Changes 0
Metric Value
cc 9
eloc 27
nc 17
nop 0
dl 0
loc 39
ccs 20
cts 26
cp 0.7692
crap 9.9957
rs 8.0555
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\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
 * Converts content of all pages.
26
 */
27
class Convert extends AbstractStep
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function getName(): string
33
    {
34 1
        if ($this->builder->getBuildOptions()['drafts']) {
35 1
            return 'Converting pages (drafts included)';
36
        }
37
38
        return 'Converting pages';
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    public function init(array $options): void
45
    {
46 1
        parent::init($options);
47
48 1
        if (\is_null($this->builder->getPages())) {
49
            $this->canProcess = false;
50
        }
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 1
    public function process(): void
57
    {
58 1
        if (\count($this->builder->getPages()) == 0) {
59
            return;
60
        }
61
62 1
        $total = \count($this->builder->getPages());
63 1
        $count = 0;
64
        /** @var Page $page */
65 1
        foreach ($this->builder->getPages() as $page) {
66 1
            if (!$page->isVirtual()) {
67 1
                $count++;
68
69
                try {
70 1
                    $convertedPage = $this->convertPage($this->builder, $page);
71
                    // set default language (ex: "en") if necessary
72 1
                    if ($convertedPage->getVariable('language') === null) {
73 1
                        $convertedPage->setVariable('language', $this->config->getLanguageDefault());
74
                    }
75 1
                } catch (RuntimeException $e) {
76 1
                    $this->builder->getLogger()->error(\sprintf('Unable to convert "%s:%s": %s', $e->getPageFile(), $e->getPageLine(), $e->getMessage()));
77 1
                    $this->builder->getPages()->remove($page->getId());
78 1
                    continue;
79
                } catch (\Exception $e) {
80
                    $this->builder->getLogger()->error(\sprintf('Unable to convert "%s": %s', Util::joinPath(Util\File::getFS()->makePathRelative($page->getFilePath(), $this->config->getPagesPath())), $e->getMessage()));
81
                    $this->builder->getPages()->remove($page->getId());
82
                    continue;
83
                }
84 1
                $message = \sprintf('Page "%s" converted', $page->getId());
85
                // forces drafts convert?
86 1
                if ($this->builder->getBuildOptions()['drafts']) {
87 1
                    $page->setVariable('published', true);
88
                }
89 1
                if ($page->getVariable('published')) {
90 1
                    $this->builder->getPages()->replace($page->getId(), $convertedPage);
91
                } else {
92
                    $message .= ' (not published)';
93
                }
94 1
                $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
95
            }
96
        }
97
    }
98
99
    /**
100
     * Converts page content:
101
     *  - front matter to PHP array
102
     *  - body to HTML.
103
     *
104
     * @throws RuntimeException
105
     */
106 1
    public function convertPage(Builder $builder, Page $page, ?string $format = null, ?ConverterInterface $converter = null): Page
107
    {
108 1
        $format = $format ?? (string) $builder->getConfig()->get('pages.frontmatter.format');
109 1
        $converter = $converter ?? new Converter($builder);
110
111
        // converts front matter
112 1
        if ($page->getFrontmatter()) {
113
            try {
114 1
                $variables = $converter->convertFrontmatter($page->getFrontmatter(), $format);
115 1
            } catch (RuntimeException $e) {
116 1
                throw new RuntimeException($e->getMessage(), $page->getFilePath(), $e->getPageLine());
117
            }
118 1
            $page->setFmVariables($variables);
119 1
            $page->setVariables($variables);
120
        }
121
122
        // converts body (only if page is published or drafts option is enabled)
123 1
        if ($page->getVariable('published') || $this->options['drafts']) {
124
            try {
125 1
                $html = $converter->convertBody($page->getBody());
126
            } catch (RuntimeException $e) {
127
                throw new \Exception($e->getMessage());
128
            }
129 1
            $page->setBodyHtml($html);
130
        }
131
132 1
        return $page;
133
    }
134
}
135