Passed
Pull Request — master (#2172)
by Franck
05:03
created

Copy::process()   B

Complexity

Conditions 10
Paths 24

Size

Total Lines 44
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 14.8069

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 10
eloc 21
c 2
b 1
f 0
nc 24
nop 0
dl 0
loc 44
ccs 14
cts 22
cp 0.6364
crap 14.8069
rs 7.6666

How to fix   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\StaticFiles;
15
16
use Cecil\Exception\RuntimeException;
17
use Cecil\Step\AbstractStep;
18
use Cecil\Util;
19
use Symfony\Component\Finder\Finder;
20
21
/**
22
 * Copying static files to site root.
23
 */
24
class Copy extends AbstractStep
25
{
26
    protected $count = 0;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public function getName(): string
32
    {
33 1
        return 'Copying static';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function init(array $options): void
40
    {
41 1
        if ($options['dry-run']) {
42
            return;
43
        }
44
45
        // reset output directory only if it's not partial rendering
46 1
        if (empty($options['render-only-path'])) {
47 1
            Util\File::getFS()->remove($this->config->getOutputPath());
48 1
            Util\File::getFS()->mkdir($this->config->getOutputPath());
49
        }
50
51 1
        $this->canProcess = true;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function process(): void
58
    {
59 1
        $target = (string) $this->config->get('static.target');
60 1
        $exclude = (array) $this->config->get('static.exclude');
61
62
        // copying assets in debug mode (for source maps)
63 1
        if ($this->builder->isDebug() && $this->config->isEnabled('assets.compile.sourcemap')) {
64
            // copying content of '<theme>/assets/' dir if exists
65
            if ($this->config->hasTheme()) {
66
                $themes = array_reverse($this->config->getTheme());
67
                foreach ($themes as $theme) {
68
                    $this->copy($this->config->getThemeDirPath($theme, 'assets'));
69
                }
70
            }
71
            // copying content of 'assets/' dir if exists
72
            $this->copy($this->config->getAssetsPath());
73
            // cancel exclusion for static files (see below)
74
            $exclude = [];
75
        }
76
77
        // copying content of '<theme>/static/' dir if exists
78 1
        if ($this->config->hasTheme()) {
79 1
            $themes = array_reverse($this->config->getTheme());
80 1
            foreach ($themes as $theme) {
81 1
                $this->copy($this->config->getThemeDirPath($theme, 'static'), $target, $exclude);
82
            }
83
        }
84
85
        // copying content of 'static/' dir if exists
86 1
        $this->copy($this->config->getStaticPath(), $target, $exclude);
87
88
        // copying mounts
89 1
        if ($this->config->get('static.mounts')) {
90 1
            foreach ((array) $this->config->get('static.mounts') as $source => $destination) {
91 1
                $this->copy(Util::joinFile($this->config->getStaticPath(), (string) $source), (string) $destination);
92
            }
93
        }
94
95 1
        if ($this->count === 0) {
96
            $this->builder->getLogger()->info('Nothing to copy');
97
98
            return;
99
        }
100 1
        $this->builder->getLogger()->info('Files copied', ['progress' => [$this->count, $this->count]]);
101
    }
102
103
    /**
104
     * Copying a file or files in a directory from $from (if exists) to $to (relative to output path).
105
     * Exclude files or directories with $exclude array.
106
     */
107 1
    protected function copy(string $from, ?string $to = null, ?array $exclude = null): void
108
    {
109
        try {
110 1
            if (Util\File::getFS()->exists($from)) {
111
                // copy a file
112 1
                if (is_file($from)) {
113 1
                    Util\File::getFS()->copy($from, Util::joinFile($this->config->getOutputPath(), $to), true);
0 ignored issues
show
Bug introduced by
It seems like $to can also be of type null; however, parameter $path of Cecil\Util::joinFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
                    Util\File::getFS()->copy($from, Util::joinFile($this->config->getOutputPath(), /** @scrutinizer ignore-type */ $to), true);
Loading history...
114
115 1
                    return;
116
                }
117
                // copy a directory
118 1
                $finder = Finder::create()
119 1
                    ->files()
120 1
                    ->in($from)
121 1
                    ->ignoreDotFiles(false);
122
                // exclude files or directories
123 1
                if (\is_array($exclude)) {
124 1
                    $finder->notPath($exclude);
125 1
                    $finder->notName($exclude);
126
                }
127 1
                $this->count += $finder->count();
128 1
                Util\File::getFS()->mirror(
129 1
                    $from,
130 1
                    Util::joinFile($this->config->getOutputPath(), $to ?? ''),
131 1
                    $finder,
132 1
                    ['override' => true]
133 1
                );
134
            }
135
        } catch (\Exception $e) {
136
            throw new RuntimeException(\sprintf('Error during static files copy: %s', $e->getMessage()));
137
        }
138
    }
139
}
140