Passed
Push — master ( b328e0...054509 )
by Enjoys
02:07
created

OneFileStrategy::getResult()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.3183

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 5
eloc 16
c 2
b 0
f 1
nc 9
nop 0
dl 0
loc 26
ccs 10
cts 16
cp 0.625
crap 6.3183
rs 9.4222
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
45 2
    }
46
47
    /**
48
     * @param string $type css|js
49
     * @return string
50
     */
51 2
    private function generateFilename(string $type): string
52
    {
53 2
        return '_' . $type . DIRECTORY_SEPARATOR . md5(serialize($this->assetsCollection)) . '.' . $type;
54
    }
55
56
    /**
57
     * @throws \Exception
58
     */
59 2
    private function init(): void
60
    {
61 2
        Helpers::createDirectory(pathinfo($this->filePath, PATHINFO_DIRNAME), 0755, $this->logger);
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\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 */ pathinfo($this->filePath, PATHINFO_DIRNAME), 0755, $this->logger);
Loading history...
62
63 2
        if (!file_exists($this->filePath)) {
64 2
            Helpers::createEmptyFile($this->filePath, $this->logger);
65 2
            $this->fileCreated = true;
66
        }
67 2
    }
68
69 1
    private function isCacheValid(): bool
70
    {
71 1
        if ($this->fileCreated) {
72 1
            return false;
73
        }
74
        return (filemtime($this->filePath) + $this->cacheTime) > time();
75
    }
76
77
    /**
78
     * @return array<string>
79
     */
80 1
    public function getResult(): array
81
    {
82
        try {
83 1
            if ($this->isCacheValid()) {
84
                $this->logger->info(sprintf('Use cached file: %s', $this->filePath));
85
                $this->logger->info(sprintf('Return url: %s', $this->fileUrl));
86
                return [$this->fileUrl];
87
            }
88
89 1
            $output = '';
90
91 1
            foreach ($this->assetsCollection as $asset) {
92 1
                $output .= (new Reader($asset, $this->minifyOptions, $this->environment, $this->logger))->getContents();
93
94 1
                $optSymlinks = (array)$asset->getOption(Asset::CREATE_SYMLINK, []);
95 1
                foreach ($optSymlinks as $optLink => $optTarget) {
96
                    Helpers::createSymlink($optLink, $optTarget, $this->logger);
97
                }
98
            }
99 1
            Helpers::writeFile($this->filePath, $output, 'w', $this->logger);
100
        } catch (\Exception $e) {
101
            $this->logger->notice($e->getMessage());
102
        }
103
104 1
        $this->logger->info(sprintf('Return url: %s', $this->fileUrl));
105 1
        return [$this->fileUrl];
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getFileUrl(): string
112
    {
113
        return $this->fileUrl;
114
    }
115
116
    /**
117
     * @return string
118
     */
119 1
    public function getFilePath(): string
120
    {
121 1
        return $this->filePath;
122
    }
123
}
124