Passed
Pull Request — master (#1704)
by Arnaud
22:27 queued 14:59
created

Load::process()   D

Complexity

Conditions 18
Paths 108

Size

Total Lines 66
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 31.6311

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 18
eloc 44
c 2
b 0
f 0
nc 108
nop 0
dl 0
loc 66
ccs 30
cts 46
cp 0.6522
crap 31.6311
rs 4.8

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
                // root pages first
62 1
                if (empty($a->getRelativePath()) && !empty($b->getRelativePath())) {
63 1
                    return -1;
64
                }
65 1
                if (empty($b->getRelativePath()) && !empty($a->getRelativePath())) {
66 1
                    return 1;
67
                }
68
                // section's index first
69 1
                if ($a->getRelativePath() == $b->getRelativePath() && $a->getBasename('.' . $a->getExtension()) == 'index') {
70 1
                    return -1;
71
                }
72 1
                if ($b->getRelativePath() == $a->getRelativePath() && $b->getBasename('.' . $b->getExtension()) == 'index') {
73 1
                    return 1;
74
                }
75
                // sort by name
76 1
                return strnatcasecmp($a->getRelativePath(), $b->getRelativePath());
77 1
            });
78
        // load only one page?
79 1
        if ($this->page) {
80
            // is the page path starts with the `pages.dir` configuration option?
81
            // (i.e.: `pages/...`, `/pages/...`, `./pages/...`)
82
            $pagePathAsArray = explode(DIRECTORY_SEPARATOR, Util::joinFile($this->page));
83
            if ($pagePathAsArray[0] == (string) $this->config->get('pages.dir')) {
84
                unset($pagePathAsArray[0]);
85
                $this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray);
86
            }
87
            if ($pagePathAsArray[0] == '.' && $pagePathAsArray[1] == (string) $this->config->get('pages.dir')) {
88
                unset($pagePathAsArray[0]);
89
                unset($pagePathAsArray[1]);
90
                $this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray);
91
            }
92
            if (!util\File::getFS()->exists(Util::joinFile($this->config->getPagesPath(), $this->page))) {
93
                $this->builder->getLogger()->error(sprintf('File "%s" doesn\'t exist.', $this->page));
94
            }
95
            $pages->path('.')->path(\dirname($this->page));
96
            $pages->name('/index\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/');
97
            $namePattern = basename($this->page);
98
        }
99 1
        $pages->name($namePattern);
100 1
        if (\is_array($exclude = $this->config->get('pages.exclude'))) {
101 1
            $pages->exclude($exclude);
102 1
            $pages->notPath($exclude);
103 1
            $pages->notName($exclude);
104
        }
105 1
        if (file_exists(Util::joinFile($this->config->getPagesPath(), '.gitignore'))) {
106
            $pages->ignoreVCSIgnored(true);
107
        }
108 1
        $this->builder->setPagesFiles($pages);
109
110 1
        $total = $pages->count();
111 1
        $count = 0;
112 1
        if ($total === 0) {
113
            $this->builder->getLogger()->info('Nothing to load');
114
115
            return;
116
        }
117 1
        foreach ($pages as $file) {
118 1
            $count++;
119 1
            $this->builder->getLogger()->info(sprintf('File "%s" loaded', $file->getRelativePathname()), ['progress' => [$count, $total]]);
120
        }
121
    }
122
}
123