Passed
Branch master (e0fe6c)
by Arnaud
05:31
created

Copy   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 78.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
c 1
b 0
f 0
dl 0
loc 104
ccs 40
cts 51
cp 0.7843
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 13 2
A getName() 0 3 1
A copy() 0 22 3
B process() 0 44 8
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Step\StaticFiles;
12
13
use Cecil\Step\AbstractStep;
14
use Cecil\Util;
15
use Symfony\Component\Finder\Finder;
16
17
/**
18
 * Copying static files to site root.
19
 */
20
class Copy extends AbstractStep
21
{
22
    protected $count = 0;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 1
    public function getName(): string
28
    {
29 1
        return 'Copying static';
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 1
    public function init($options)
36
    {
37 1
        if ($options['dry-run']) {
38
            $this->canProcess = false;
39
40
            return;
41
        }
42
43
        // reset output directory
44 1
        Util\File::getFS()->remove($this->config->getOutputPath());
45 1
        Util\File::getFS()->mkdir($this->config->getOutputPath());
46
47 1
        $this->canProcess = true;
48 1
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    public function process()
54
    {
55
        // copying content of '<theme>/static/' dir if exists
56 1
        if ($this->config->hasTheme()) {
57 1
            $themes = array_reverse($this->config->getTheme());
58 1
            foreach ($themes as $theme) {
59 1
                $this->copy(
60 1
                    $this->config->getThemeDirPath($theme, 'static'),
61 1
                    $this->config->get('static.target'),
62 1
                    $this->config->get('static.exclude')
63
                );
64
            }
65
        }
66
67
        // copying content of 'static/' dir if exists
68 1
        $this->copy(
69 1
            $this->config->getStaticPath(),
70 1
            $this->config->get('static.target'),
71 1
            $this->config->get('static.exclude')
72
        );
73
74
        // copying assets in debug mode (for source maps)
75 1
        if ($this->builder->isDebug() && (bool) $this->config->get('assets.compile.sourcemap')) {
76
            // copying content of '<theme>/assets/' dir if exists
77
            if ($this->config->hasTheme()) {
78
                $themes = array_reverse($this->config->getTheme());
79
                foreach ($themes as $theme) {
80
                    $this->copy(
81
                        $this->config->getThemeDirPath($theme, 'assets')
82
                    );
83
                }
84
            }
85
            // copying content of 'assets/' dir if exists
86
            $this->copy(
87
                $this->config->getAssetsPath()
88
            );
89
        }
90
91 1
        if ($this->count === 0) {
92
            $this->builder->getLogger()->info('Nothing to copy');
93
94
            return 0;
95
        }
96 1
        $this->builder->getLogger()->info('Files copied', ['progress' => [$this->count, $this->count]]);
97 1
    }
98
99
    /**
100
     * Copying (mirror) files.
101
     */
102 1
    protected function copy(string $from, string $to = null, array $exclude = null): bool
103
    {
104 1
        if (Util\File::getFS()->exists($from)) {
105 1
            $finder = Finder::create()
106 1
                ->files()
107 1
                ->in($from);
108 1
            if (is_array($exclude)) {
109 1
                $finder->notPath($exclude);
110 1
                $finder->notName($exclude);
111
            }
112 1
            $this->count += $finder->count();
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
            );
119
120 1
            return true;
121
        }
122
123 1
        return false;
124
    }
125
}
126