Passed
Pull Request — master (#2105)
by Arnaud
10:57 queued 04:40
created

Copy::process()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 12.7789

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 8
eloc 18
c 2
b 1
f 0
nc 12
nop 0
dl 0
loc 37
ccs 11
cts 19
cp 0.5789
crap 12.7789
rs 8.4444
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 1
        if ($this->count === 0) {
87
            $this->builder->getLogger()->info('Nothing to copy');
88
89
            return;
90
        }
91 1
        $this->builder->getLogger()->info('Files copied', ['progress' => [$this->count, $this->count]]);
92
    }
93
94
    /**
95
     * Copying (mirror) files from $from to static/$to directory if $from path exists.
96
     *
97
     * @throws RuntimeException
98
     */
99 1
    protected function copy(string $from, ?string $to = null, ?array $exclude = null): void
100
    {
101 1
        if (Util\File::getFS()->exists($from)) {
102
            try {
103
                // count...
104 1
                $finder = Finder::create()
105 1
                    ->files()
106 1
                    ->in($from)
107 1
                    ->ignoreDotFiles(false);
108 1
                if (\is_array($exclude)) {
109 1
                    $finder->notPath($exclude)->notName($exclude);
110
                }
111 1
                $this->count += $finder->count();
112
                // ...and copy
113 1
                Util\File::getFS()->mirror(
114 1
                    $from,
115 1
                    Util::joinFile($this->config->getOutputPath(), $to ?? ''),
116 1
                    $finder,
117 1
                    ['override' => true]
118 1
                );
119
            } catch (\Exception $e) {
120
                throw new RuntimeException(\sprintf('Error during static files copy: %s', $e->getMessage()));
121
            }
122
        }
123
    }
124
}
125