Passed
Push — master ( effdba...b9addd )
by Enjoys
01:32
created

OneFileStrategy::generateFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
10
class OneFileStrategy extends StrategyAbstract
11
{
12
    private int $cacheTime;
13
    private string $filePath;
14
    private string $fileUrl;
15
    private bool $fileCreated = false;
16
    /**
17
     * @var array{css: array<mixed>, js: array<mixed>}
18
     */
19
    private array $minifyOptions;
20
21
    /**
22
     * Build constructor.
23
     * @param Environment $environment
24
     * @param array<Asset> $assetsCollection
25
     * @param string $type
26
     * @throws \Exception
27
     */
28 2
    public function __construct(
29
        Environment $environment,
30
        array $assetsCollection,
31
        string $type
32
    ) {
33 2
        parent::__construct($environment, $assetsCollection, $type);
34
35 2
        $this->cacheTime = $environment->getCacheTime();
36 2
        $this->minifyOptions = $environment->getMinifyOptions();
37
38 2
        $filename = $this->generateFilename($type);
39
40 2
        $this->filePath = $environment->getCompileDir() . DIRECTORY_SEPARATOR . $filename;
41 2
        $this->fileUrl = $environment->getBaseUrl() . '/' . str_replace(DIRECTORY_SEPARATOR, '/', $filename);
42 2
        $this->init();
43 2
    }
44
45
    /**
46
     * @param string $type css|js
47
     * @return string
48
     */
49 2
    private function generateFilename(string $type): string
50
    {
51 2
        return '_' . $type . DIRECTORY_SEPARATOR . md5(serialize($this->assetsCollection)) . '.' . $type;
52
    }
53
54
    /**
55
     * @throws \Exception
56
     */
57 2
    private function init(): void
58
    {
59 2
        $this->createDirectory(pathinfo($this->filePath, PATHINFO_DIRNAME));
0 ignored issues
show
Bug introduced by
It seems like pathinfo($this->filePath...ategy\PATHINFO_DIRNAME) can also be of type array; however, parameter $path of Enjoys\AssetsCollector\C...ract::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

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