Passed
Push — develop ( 2a0a90...9063e1 )
by Brent
02:35
created

CopyStaticFiles::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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