Passed
Pull Request — master (#2103)
by Arnaud
14:35 queued 08:21
created

Copy::process()   C

Complexity

Conditions 12
Paths 48

Size

Total Lines 51
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 24.6381

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 12
eloc 24
c 2
b 1
f 0
nc 48
nop 0
dl 0
loc 51
ccs 15
cts 27
cp 0.5556
crap 24.6381
rs 6.9666

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\StaticFiles;
15
16
use Cecil\Step\AbstractStep;
17
use Cecil\Util;
18
use Symfony\Component\Finder\Finder;
19
20
/**
21
 * Copying static files to site root.
22
 */
23
class Copy extends AbstractStep
24
{
25
    protected $count = 0;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public function getName(): string
31
    {
32 1
        return 'Copying static';
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function init(array $options): void
39
    {
40 1
        if ($options['dry-run']) {
41
            return;
42
        }
43
44
        // reset output directory
45 1
        Util\File::getFS()->remove($this->config->getOutputPath());
46 1
        Util\File::getFS()->mkdir($this->config->getOutputPath());
47
48 1
        $this->canProcess = true;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function process(): void
55
    {
56 1
        $target = $this->config->get('static.target');
57 1
        $exclude = $this->config->get('static.exclude');
58
59
        // abord exclude if in debug mode + sourcemap
60 1
        if ($this->builder->isDebug() && (bool) $this->config->get('assets.compile.sourcemap')) {
61
            $exclude = [];
62
        }
63
64
        // copying content of '<theme>/static/' dir if exists
65 1
        if ($this->config->hasTheme()) {
66 1
            $themes = array_reverse($this->config->getTheme());
67 1
            foreach ($themes as $theme) {
68 1
                $this->copy($this->config->getThemeDirPath($theme, 'static'), $target, $exclude);
69
            }
70
        }
71
72
        // copying mounts
73 1
        if ($this->config->get('static.mounts')) {
74 1
            foreach ($this->config->get('static.mounts') as $source => $destination) {
75 1
                $this->copy(Util::joinFile($this->config->getStaticPath(), (string) $source), (string) $destination);
76
            }
77
        }
78
79
        // copying content of 'static/' dir if exists
80 1
        $this->copy($this->config->getStaticPath(), $target, $exclude);
81
82
        // copying assets in debug mode (for source maps)
83 1
        if ($this->builder->isDebug() && (bool) $this->config->get('assets.compile.sourcemap')) {
84
            // copying content of '<theme>/assets/' dir if exists
85
            if ($this->config->hasTheme()) {
86
                $themes = array_reverse($this->config->getTheme());
87
                foreach ($themes as $theme) {
88
                    $this->copy(
89
                        $this->config->getThemeDirPath($theme, 'assets')
90
                    );
91
                }
92
            }
93
            // copying content of 'assets/' dir if exists
94
            $this->copy(
95
                $this->config->getAssetsPath()
96
            );
97
        }
98
99 1
        if ($this->count === 0) {
100
            $this->builder->getLogger()->info('Nothing to copy');
101
102
            return;
103
        }
104 1
        $this->builder->getLogger()->info('Files copied', ['progress' => [$this->count, $this->count]]);
105
    }
106
107
    /**
108
     * Copying (mirror) files.
109
     */
110 1
    protected function copy(string $from, ?string $to = null, ?array $exclude = null): bool
111
    {
112 1
        if (Util\File::getFS()->exists($from)) {
113 1
            $finder = Finder::create()
114 1
                ->files()
115 1
                ->in($from)
116 1
                ->ignoreDotFiles(false);
117 1
            if (\is_array($exclude)) {
118 1
                $finder->notPath($exclude);
119 1
                $finder->notName($exclude);
120
            }
121 1
            $this->count += $finder->count();
122 1
            Util\File::getFS()->mirror(
123 1
                $from,
124 1
                Util::joinFile($this->config->getOutputPath(), $to ?? ''),
125 1
                $finder,
126 1
                ['override' => true]
127 1
            );
128
129 1
            return true;
130
        }
131
132
        return false;
133
    }
134
}
135