Passed
Pull Request — master (#1676)
by Arnaud
09:49 queued 04:14
created

Load::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Exception\RuntimeException;
17
use Cecil\Step\AbstractStep;
18
use Cecil\Util;
19
use Symfony\Component\Finder\Finder;
20
21
/**
22
 * Loads pages.
23
 */
24
class Load extends AbstractStep
25
{
26
    /** @var string */
27
    protected $page;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function getName(): string
33
    {
34 1
        return 'Loading pages';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 1
    public function init(array $options): void
41
    {
42 1
        if (!is_dir($this->config->getPagesPath())) {
43
            throw new RuntimeException(sprintf('Pages path "%s" not found.', $this->config->getPagesPath()));
44
        }
45
46 1
        $this->page = $options['page'];
47 1
        $this->canProcess = true;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    public function process(): void
54
    {
55 1
        $namePattern = '/\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/';
56 1
        $content = Finder::create()
57 1
            ->files()
58 1
            ->in($this->config->getPagesPath())
59 1
            ->sortByName(true);
60
        // load only one page?
61 1
        if ($this->page) {
62
            // is the page path starts with the `pages.dir` configuration option?
63
            // (i.e.: `pages/...`, `/pages/...`, `./pages/...`)
64
            $pagePathAsArray = explode(DIRECTORY_SEPARATOR, Util::joinFile($this->page));
65
            if ($pagePathAsArray[0] == (string) $this->config->get('pages.dir')) {
66
                unset($pagePathAsArray[0]);
67
                $this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray);
68
            }
69
            if ($pagePathAsArray[0] == '.' && $pagePathAsArray[1] == (string) $this->config->get('pages.dir')) {
70
                unset($pagePathAsArray[0]);
71
                unset($pagePathAsArray[1]);
72
                $this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray);
73
            }
74
            if (!util\File::getFS()->exists(Util::joinFile($this->config->getPagesPath(), $this->page))) {
75
                $this->builder->getLogger()->error(sprintf('File "%s" doesn\'t exist.', $this->page));
76
            }
77
            $content->path('.')->path(\dirname($this->page));
78
            $content->name('/index\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/');
79
            $namePattern = basename($this->page);
80
        }
81 1
        $content->name($namePattern);
82 1
        if (\is_array($exclude = $this->config->get('pages.exclude'))) {
83 1
            $content->exclude($exclude);
84 1
            $content->notPath($exclude);
85 1
            $content->notName($exclude);
86
        }
87 1
        if (file_exists(Util::joinFile($this->config->getPagesPath(), '.gitignore'))) {
88
            $content->ignoreVCSIgnored(true);
89
        }
90 1
        $this->builder->setPagesFiles($content);
91
92 1
        $count = $content->count();
93 1
        if ($count === 0) {
94
            $this->builder->getLogger()->info('Nothing to load');
95
96
            return;
97
        }
98 1
        $this->builder->getLogger()->info('Files loaded', ['progress' => [$count, $count]]);
99
    }
100
}
101