Passed
Push — master ( bd3c1c...f4ac9e )
by Enjoys
02:18
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 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
        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

60
        Helpers::createDirectory(/** @scrutinizer ignore-type */ pathinfo($this->filePath, PATHINFO_DIRNAME), 0755, $this->logger);
Loading history...
61
62 2
        if (!file_exists($this->filePath)) {
63 2
            Helpers::createEmptyFile($this->filePath, $this->logger);
64 2
            $this->fileCreated = true;
65
        }
66 2
    }
67
68 1
    private function isCacheValid(): bool
69
    {
70 1
        if ($this->fileCreated) {
71 1
            return false;
72
        }
73
        return (filemtime($this->filePath) + $this->cacheTime) > time();
74
    }
75
76
    /**
77
     * @return array<string>
78
     */
79 1
    public function getResult(): array
80
    {
81
        try {
82 1
            if ($this->isCacheValid()) {
83
                $this->logger->info(sprintf('Use cached file: %s', $this->filePath));
84
                $this->logger->info(sprintf('Return url: %s', $this->fileUrl));
85
                return [$this->fileUrl];
86
            }
87
88 1
            $output = '';
89
90 1
            foreach ($this->assetsCollection as $asset) {
91 1
                $output .= (new Reader($asset, $this->minifyOptions, $this->logger))->getContents();
92
93 1
                $optSymlinks = (array)$asset->getOption(Asset::CREATE_SYMLINK, []);
94 1
                foreach ($optSymlinks as $optLink => $optTarget) {
95
                    Helpers::createSymlink($optLink, $optTarget, $this->logger);
96
                }
97
            }
98 1
            Helpers::writeFile($this->filePath, $output, 'w', $this->logger);
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