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

Cdn   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A save() 0 13 4
A copyCdnFiles() 0 9 2
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