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

CopyStaticFiles   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 21
cts 23
cp 0.913
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
B 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 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