CopyStaticFiles   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A execute() 0 20 5
A copyStaticFile() 0 12 2
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