CopyStaticFiles::execute()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 0
dl 0
loc 20
ccs 0
cts 11
cp 0
crap 30
rs 9.2888
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
    public function __construct(
26
        string $publicDirectory,
27
        array $staticFiles,
28
        bool $cacheStaticFiles
29
    ) {
30
        $this->fs = new Filesystem();
31
        $this->publicDirectory = $publicDirectory;
32
        $this->staticFiles = $staticFiles;
33
        $this->cacheStaticFiles = $cacheStaticFiles;
34
    }
35
36
    public function execute(): void
37
    {
38
        foreach ($this->staticFiles as $staticFile) {
39
            $staticFile = trim($staticFile, '/');
40
41
            $publicPath = File::path("{$this->publicDirectory}/{$staticFile}");
42
43
            if ($this->cacheStaticFiles && $this->fs->exists($publicPath)) {
44
                continue;
45
            }
46
47
            $sourcePath = File::path($staticFile);
48
49
            try {
50
                $this->copyStaticFile($sourcePath, $publicPath);
51
            } catch (FileNotFoundException $exception) {
52
                throw FileNotFound::staticFile($sourcePath);
53
            }
54
        }
55
    }
56
57
    private function copyStaticFile(
58
        string $sourcePath,
59
        string $publicPath
60
    ): void {
61
        if (is_dir($sourcePath)) {
62
            $this->fs->mirror($sourcePath, $publicPath);
63
64
            return;
65
        }
66
67
        $this->fs->copy($sourcePath, $publicPath);
68
    }
69
}
70