Passed
Push — develop ( 667b67...a1f6a6 )
by Brent
03:21
created

CopyStaticFiles::copyStaticFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Stitcher\Task;
4
5
use Stitcher\File;
6
use Stitcher\Task;
7
use Symfony\Component\Filesystem\Filesystem;
8
9
class CopyStaticFiles implements Task
10
{
11
    private $fs;
12
    private $staticFiles;
13
    private $cacheStaticFiles;
14
    private $publicDirectory;
15
16 4
    public function __construct(string $publicDirectory, array $staticFiles, bool $cacheStaticFiles)
17
    {
18 4
        $this->fs = new Filesystem();
19 4
        $this->publicDirectory = $publicDirectory;
20 4
        $this->staticFiles = $staticFiles;
21 4
        $this->cacheStaticFiles = $cacheStaticFiles;
22 4
    }
23
24 2
    public function execute(): void
25
    {
26 2
        foreach ($this->staticFiles as $staticFile) {
27 2
            $staticFile = trim($staticFile, '/');
28
29 2
            $publicPath = File::path("{$this->publicDirectory}/{$staticFile}");
30
31 2
            if ($this->cacheStaticFiles && $this->fs->exists($publicPath)) {
32
                continue;
33
            }
34
35 2
            $sourcePath = File::path($staticFile);
36
37 2
            $this->copyStaticFile($sourcePath, $publicPath);
38
        }
39 2
    }
40
41 2
    private function copyStaticFile(string $sourcePath, string $publicPath): void
42
    {
43 2
        if (is_dir($sourcePath)) {
44 2
            $this->fs->mirror($sourcePath, $publicPath);
45
46 2
            return;
47
        }
48
49 2
        $this->fs->copy($sourcePath, $publicPath);
50 2
    }
51
}
52