Passed
Push — master ( d1c6a8...c93284 )
by Arnaud
06:11
created

Load::process()   C

Complexity

Conditions 14
Paths 108

Size

Total Lines 59
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 24.84

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 40
nc 108
nop 0
dl 0
loc 59
ccs 26
cts 42
cp 0.619
crap 24.84
rs 6.2
c 1
b 0
f 0

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\Exception\RuntimeException;
17
use Cecil\Step\AbstractStep;
18
use Cecil\Util;
19
use Symfony\Component\Finder\Finder;
20
use Symfony\Component\Finder\SplFileInfo;
21
22
/**
23
 * Loads pages.
24
 */
25
class Load extends AbstractStep
26
{
27
    /** @var string */
28
    protected $page;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    public function getName(): string
34
    {
35 1
        return 'Loading pages';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public function init(array $options): void
42
    {
43 1
        if (!is_dir($this->config->getPagesPath())) {
44
            throw new RuntimeException(sprintf('Pages path "%s" not found.', $this->config->getPagesPath()));
45
        }
46
47 1
        $this->page = $options['page'];
48 1
        $this->canProcess = true;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function process(): void
55
    {
56 1
        $namePattern = '/\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/';
57 1
        $pages = Finder::create()
58 1
            ->files()
59 1
            ->in($this->config->getPagesPath())
60 1
            ->sort(function (SplFileInfo $a, SplFileInfo $b): int {
61
                // section's index first
62 1
                if ($a->getRelativePath() == $b->getRelativePath() && $a->getBasename('.' . $a->getExtension()) == 'index') {
63 1
                    return -1;
64
                }
65 1
                if ($b->getRelativePath() == $a->getRelativePath() && $b->getBasename('.' . $b->getExtension()) == 'index') {
66 1
                    return 1;
67
                }
68
                // sort by name
69 1
                return strnatcasecmp($a->getRealPath(), $b->getRealPath());
70 1
            });
71
        // load only one page?
72 1
        if ($this->page) {
73
            // is the page path starts with the `pages.dir` configuration option?
74
            // (i.e.: `pages/...`, `/pages/...`, `./pages/...`)
75
            $pagePathAsArray = explode(DIRECTORY_SEPARATOR, Util::joinFile($this->page));
76
            if ($pagePathAsArray[0] == (string) $this->config->get('pages.dir')) {
77
                unset($pagePathAsArray[0]);
78
                $this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray);
79
            }
80
            if ($pagePathAsArray[0] == '.' && $pagePathAsArray[1] == (string) $this->config->get('pages.dir')) {
81
                unset($pagePathAsArray[0]);
82
                unset($pagePathAsArray[1]);
83
                $this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray);
84
            }
85
            if (!util\File::getFS()->exists(Util::joinFile($this->config->getPagesPath(), $this->page))) {
86
                $this->builder->getLogger()->error(sprintf('File "%s" doesn\'t exist.', $this->page));
87
            }
88
            $pages->path('.')->path(\dirname($this->page));
89
            $pages->name('/index\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/');
90
            $namePattern = basename($this->page);
91
        }
92 1
        $pages->name($namePattern);
93 1
        if (\is_array($exclude = $this->config->get('pages.exclude'))) {
94 1
            $pages->exclude($exclude);
95 1
            $pages->notPath($exclude);
96 1
            $pages->notName($exclude);
97
        }
98 1
        if (file_exists(Util::joinFile($this->config->getPagesPath(), '.gitignore'))) {
99
            $pages->ignoreVCSIgnored(true);
100
        }
101 1
        $this->builder->setPagesFiles($pages);
102
103 1
        $total = $pages->count();
104 1
        $count = 0;
105 1
        if ($total === 0) {
106
            $this->builder->getLogger()->info('Nothing to load');
107
108
            return;
109
        }
110 1
        foreach ($pages as $file) {
111 1
            $count++;
112 1
            $this->builder->getLogger()->info(sprintf('File "%s" loaded', $file->getRelativePathname()), ['progress' => [$count, $total]]);
113
        }
114
    }
115
}
116