Passed
Push — master ( e65f7a...b84038 )
by
unknown
06:29
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
46 1
        Util\File::getFS()->remove($this->config->getOutputPath());
47 1
        Util\File::getFS()->mkdir($this->config->getOutputPath());
48
49 1
        $this->canProcess = true;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function process(): void
56
    {
57 1
        $target = $this->config->get('static.target');
58 1
        $exclude = $this->config->get('static.exclude');
59
60
        // copying assets in debug mode (for source maps)
61 1
        if ($this->builder->isDebug() && (bool) $this->config->get('assets.compile.sourcemap')) {
62
            // copying content of '<theme>/assets/' dir if exists
63
            if ($this->config->hasTheme()) {
64
                $themes = array_reverse($this->config->getTheme());
65
                foreach ($themes as $theme) {
66
                    $this->copy($this->config->getThemeDirPath($theme, 'assets'));
67
                }
68
            }
69
            // copying content of 'assets/' dir if exists
70
            $this->copy($this->config->getAssetsPath());
71
            // cancel exclusion for static files (see below)
72
            $exclude = [];
73
        }
74
75
        // copying content of '<theme>/static/' dir if exists
76 1
        if ($this->config->hasTheme()) {
77 1
            $themes = array_reverse($this->config->getTheme());
78 1
            foreach ($themes as $theme) {
79 1
                $this->copy($this->config->getThemeDirPath($theme, 'static'), $target, $exclude);
80
            }
81
        }
82
83
        // copying content of 'static/' dir if exists
84 1
        $this->copy($this->config->getStaticPath(), $target, $exclude);
85
86
        // copying mounts
87 1
        if ($this->config->get('static.mounts')) {
88 1
            foreach ($this->config->get('static.mounts') as $source => $destination) {
89 1
                $this->copy(Util::joinFile($this->config->getStaticPath(), (string) $source), (string) $destination);
90
            }
91
        }
92
93 1
        if ($this->count === 0) {
94
            $this->builder->getLogger()->info('Nothing to copy');
95
96
            return;
97
        }
98 1
        $this->builder->getLogger()->info('Files copied', ['progress' => [$this->count, $this->count]]);
99
    }
100
101
    /**
102
     * Copying a file or files in a directory from $from (if exists) to $to (relative to output path).
103
     * Exclude files or directories with $exclude array.
104
     */
105 1
    protected function copy(string $from, ?string $to = null, ?array $exclude = null): void
106
    {
107
        try {
108 1
            if (Util\File::getFS()->exists($from)) {
109
                // copy a file
110 1
                if (is_file($from)) {
111 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

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