Passed
Pull Request — master (#1704)
by Arnaud
08:53 queued 03:36
created

Load::process()   C

Complexity

Conditions 13
Paths 72

Size

Total Lines 57
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 23.816

Importance

Changes 4
Bugs 2 Features 0
Metric Value
cc 13
eloc 37
c 4
b 2
f 0
nc 72
nop 0
dl 0
loc 57
ccs 24
cts 40
cp 0.6
crap 23.816
rs 6.6166

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
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
        // legacy support
43 1
        if (is_dir(Util::joinFile($this->config->getSourceDir(), 'content'))) {
44
            $this->builder->getLogger()->alert('"content" directory is deprecated, please rename it to "pages"');
45
        }
46
47 1
        if (!is_dir($this->config->getPagesPath())) {
48
            throw new RuntimeException(sprintf('Pages path "%s" not found.', $this->config->getPagesPath()));
49
        }
50
51 1
        $this->page = $options['page'];
52 1
        $this->canProcess = true;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function process(): void
59
    {
60 1
        $namePattern = '/\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/';
61 1
        $content = Finder::create()
62 1
            ->files()
63 1
            ->in($this->config->getPagesPath())
64 1
            //->sortByName(true);
65 1
            ->sort(function (\Symfony\Component\Finder\SplFileInfo $a, \Symfony\Component\Finder\SplFileInfo $b) {
66
                // section's index first
67 1
                if ($a->getRelativePath() == $b->getRelativePath() && $a->getBasename('.' . $a->getExtension()) == 'index') {
68 1
                    return -1;
69
                }
70 1
                if ($b->getRelativePath() == $a->getRelativePath() && $b->getBasename('.' . $b->getExtension()) == 'index') {
71 1
                    return 1;
72
                }
73
74 1
                return strnatcasecmp($a->getRealPath(), $b->getRealPath());
75 1
            });
76
        // load only one page?
77 1
        if ($this->page) {
78
            // is the page path starts with the `pages.dir` configuration option?
79
            // (i.e.: `pages/...`, `/pages/...`, `./pages/...`)
80
            $pagePathAsArray = explode(DIRECTORY_SEPARATOR, $this->page);
81
            if ($pagePathAsArray[0] == (string) $this->config->get('pages.dir')) {
82
                unset($pagePathAsArray[0]);
83
                $this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray);
84
            }
85
            if ($pagePathAsArray[0] == '.' && $pagePathAsArray[1] == (string) $this->config->get('pages.dir')) {
86
                unset($pagePathAsArray[0]);
87
                unset($pagePathAsArray[1]);
88
                $this->page = implode(DIRECTORY_SEPARATOR, $pagePathAsArray);
89
            }
90
            if (!util\File::getFS()->exists(Util::joinFile($this->config->getPagesPath(), $this->page))) {
91
                $this->builder->getLogger()->error(sprintf('File "%s" doesn\'t exist.', $this->page));
92
            }
93
            $content->path('.')->path(\dirname($this->page));
94
            $content->name('/index\.(' . implode('|', (array) $this->config->get('pages.ext')) . ')$/');
95
            $namePattern = basename($this->page);
96
        }
97 1
        $content->name($namePattern);
98 1
        if (\is_array($this->config->get('pages.exclude'))) {
99 1
            $content->exclude($this->config->get('pages.exclude'));
100 1
            $content->notPath($this->config->get('pages.exclude'));
101 1
            $content->notName($this->config->get('pages.exclude'));
102
        }
103 1
        if (file_exists(Util::joinFile($this->config->getPagesPath(), '.gitignore'))) {
104
            $content->ignoreVCSIgnored(true);
105
        }
106 1
        $this->builder->setPagesFiles($content);
107
108 1
        $count = $content->count();
109 1
        if ($count === 0) {
110
            $this->builder->getLogger()->info('Nothing to load');
111
112
            return;
113
        }
114 1
        $this->builder->getLogger()->info('Files loaded', ['progress' => [$count, $count]]);
115
    }
116
}
117