Passed
Pull Request — develop (#20)
by Brent
02:33
created

Cdn::copyCdnFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Lib;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
7
class Cdn
8
{
9
    private $browser;
10
    private $files;
11
    private $enableCache;
12
13
    public function __construct(Browser $browser, array $files, bool $enableCache) {
14
        $this->browser = $browser;
15
        $this->files = $files;
16
        $this->enableCache = $enableCache;
17
    }
18
19
    public function save() {
20
        foreach ($this->files as $resource) {
21
            $resource = trim($resource, '/');
22
            $publicResourcePath = "{$this->browser->getPublicDir()}/{$resource}";
23
24
            if ($this->enableCache && file_exists($publicResourcePath)) {
25
                continue;
26
            }
27
28
            $sourceResourcePath = "{$this->browser->getSrcDir()}/{$resource}";
29
            $this->copyCdnFiles($sourceResourcePath, $publicResourcePath);
30
        }
31
    }
32
33
    private function copyCdnFiles(string $sourcePath, string $publicPath) {
34
        $fs = new Filesystem();
35
36
        if (is_dir($sourcePath)) {
37
            $fs->mirror($sourcePath, $publicPath);
38
        } else {
39
            $fs->copy($sourcePath, $publicPath, true);
40
        }
41
    }
42
}
43