Passed
Pull Request — master (#2172)
by Franck
05:30
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
    protected $path = '';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    public function getName(): string
34
    {
35 1
        return 'Copying static';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public function init(array $options): void
42
    {
43 1
        if ($options['dry-run']) {
44
            return;
45
        }
46
47 1
        if (!empty($options['renderPath'])) {
48
            $this->path = $options['renderPath'];
49
        }
50
51
        // reset output directory
52 1
        if (empty($this->path)) {
53 1
            Util\File::getFS()->remove($this->config->getOutputPath());
54 1
            Util\File::getFS()->mkdir($this->config->getOutputPath());
55
        }
56
57 1
        $this->canProcess = true;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function process(): void
64
    {
65 1
        $target = (string) $this->config->get('static.target');
66 1
        $exclude = (array) $this->config->get('static.exclude');
67
68
        // copying assets in debug mode (for source maps)
69 1
        if ($this->builder->isDebug() && $this->config->isEnabled('assets.compile.sourcemap')) {
70
            // copying content of '<theme>/assets/' dir if exists
71
            if ($this->config->hasTheme()) {
72
                $themes = array_reverse($this->config->getTheme());
73
                foreach ($themes as $theme) {
74
                    $this->copy($this->config->getThemeDirPath($theme, 'assets'));
75
                }
76
            }
77
            // copying content of 'assets/' dir if exists
78
            $this->copy($this->config->getAssetsPath());
79
            // cancel exclusion for static files (see below)
80
            $exclude = [];
81
        }
82
83
        // copying content of '<theme>/static/' dir if exists
84 1
        if ($this->config->hasTheme()) {
85 1
            $themes = array_reverse($this->config->getTheme());
86 1
            foreach ($themes as $theme) {
87 1
                $this->copy($this->config->getThemeDirPath($theme, 'static'), $target, $exclude);
88
            }
89
        }
90
91
        // copying content of 'static/' dir if exists
92 1
        $this->copy($this->config->getStaticPath(), $target, $exclude);
93
94
        // copying mounts
95 1
        if ($this->config->get('static.mounts')) {
96 1
            foreach ((array) $this->config->get('static.mounts') as $source => $destination) {
97 1
                $this->copy(Util::joinFile($this->config->getStaticPath(), (string) $source), (string) $destination);
98
            }
99
        }
100
101 1
        if ($this->count === 0) {
102
            $this->builder->getLogger()->info('Nothing to copy');
103
104
            return;
105
        }
106 1
        $this->builder->getLogger()->info('Files copied', ['progress' => [$this->count, $this->count]]);
107
    }
108
109
    /**
110
     * Copying a file or files in a directory from $from (if exists) to $to (relative to output path).
111
     * Exclude files or directories with $exclude array.
112
     */
113 1
    protected function copy(string $from, ?string $to = null, ?array $exclude = null): void
114
    {
115
        try {
116 1
            if (Util\File::getFS()->exists($from)) {
117
                // copy a file
118 1
                if (is_file($from)) {
119 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

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