Passed
Push — master ( b9addd...bd3c1c )
by Enjoys
01:53
created

OneFileStrategy   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 80.95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 110
ccs 34
cts 42
cp 0.8095
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A generateFilename() 0 3 1
A isCacheValid() 0 6 2
A __construct() 0 15 1
A getFilePath() 0 3 1
A init() 0 10 2
A getFileUrl() 0 3 1
A getResult() 0 23 4
1
<?php
2
3
namespace Enjoys\AssetsCollector\CollectStrategy\Strategy;
4
5
use Enjoys\AssetsCollector\Asset;
6
use Enjoys\AssetsCollector\CollectStrategy\StrategyAbstract;
7
use Enjoys\AssetsCollector\Content\Reader;
8
use Enjoys\AssetsCollector\Environment;
9
use Enjoys\AssetsCollector\Helpers;
10
11
class OneFileStrategy extends StrategyAbstract
12
{
13
    private int $cacheTime;
14
    private string $filePath;
15
    private string $fileUrl;
16
    private bool $fileCreated = false;
17
    /**
18
     * @var array{css: array<mixed>, js: array<mixed>}
19
     */
20
    private array $minifyOptions;
21
22
    /**
23
     * Build constructor.
24
     * @param Environment $environment
25
     * @param array<Asset> $assetsCollection
26
     * @param string $type
27
     * @throws \Exception
28
     */
29 2
    public function __construct(
30
        Environment $environment,
31
        array $assetsCollection,
32
        string $type
33
    ) {
34 2
        parent::__construct($environment, $assetsCollection, $type);
35
36 2
        $this->cacheTime = $environment->getCacheTime();
37 2
        $this->minifyOptions = $environment->getMinifyOptions();
38
39 2
        $filename = $this->generateFilename($type);
40
41 2
        $this->filePath = $environment->getCompileDir() . DIRECTORY_SEPARATOR . $filename;
42 2
        $this->fileUrl = $environment->getBaseUrl() . '/' . str_replace(DIRECTORY_SEPARATOR, '/', $filename);
43 2
        $this->init();
44 2
    }
45
46
    /**
47
     * @param string $type css|js
48
     * @return string
49
     */
50 2
    private function generateFilename(string $type): string
51
    {
52 2
        return '_' . $type . DIRECTORY_SEPARATOR . md5(serialize($this->assetsCollection)) . '.' . $type;
53
    }
54
55
    /**
56
     * @throws \Exception
57
     */
58 2
    private function init(): void
59
    {
60 2
        $path = pathinfo($this->filePath, PATHINFO_DIRNAME);
61 2
        Helpers::createDirectory($path);
0 ignored issues
show
Bug introduced by
It seems like $path can also be of type array; however, parameter $path of Enjoys\AssetsCollector\Helpers::createDirectory() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        Helpers::createDirectory(/** @scrutinizer ignore-type */ $path);
Loading history...
62 2
        $this->logger->info(sprintf('Create directory %s', $path));
63
64 2
        if (!file_exists($this->filePath)) {
65 2
            Helpers::writeFile($this->filePath, '');
66 2
            $this->fileCreated = true;
67 2
            $this->logger->info(sprintf('Create new file %s', $this->filePath));
68
        }
69 2
    }
70
71 1
    private function isCacheValid(): bool
72
    {
73 1
        if ($this->fileCreated) {
74 1
            return false;
75
        }
76
        return (filemtime($this->filePath) + $this->cacheTime) > time();
77
    }
78
79
    /**
80
     * @return array<string>
81
     */
82 1
    public function getResult(): array
83
    {
84
        try {
85 1
            if ($this->isCacheValid()) {
86
                $this->logger->info(sprintf('Use cached file: %s', $this->filePath));
87
                $this->logger->info(sprintf('Return url: %s', $this->fileUrl));
88
                return [$this->fileUrl];
89
            }
90
91 1
            $output = '';
92
93 1
            foreach ($this->assetsCollection as $asset) {
94 1
                $output .= (new Reader($asset, $this->minifyOptions, $this->logger))->getContents();
95
            }
96
97 1
            Helpers::writeFile($this->filePath, $output);
98 1
            $this->logger->info(sprintf('Write to: %s', $this->filePath));
99
        } catch (\Exception $e) {
100
            $this->logger->notice($e->getMessage());
101
        }
102
103 1
        $this->logger->info(sprintf('Return url: %s', $this->fileUrl));
104 1
        return [$this->fileUrl];
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getFileUrl(): string
111
    {
112
        return $this->fileUrl;
113
    }
114
115
    /**
116
     * @return string
117
     */
118 1
    public function getFilePath(): string
119
    {
120 1
        return $this->filePath;
121
    }
122
}
123