Passed
Push — master ( 69e1b4...115f1e )
by Caen
03:08 queued 13s
created

AssetService::hasMediaFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Hyde\Framework\Services;
4
5
use Hyde\Framework\Hyde;
6
7
/**
8
 * Handles the retrieval of core asset files. Commonly used through the Asset facade.
9
 *
10
 * This class is loaded into the service container, making it easy to access and modify.
11
 *
12
 * @see \Hyde\Framework\Helpers\Asset
13
 */
14
class AssetService
15
{
16
    /**
17
     * The default HydeFront version to load.
18
     *
19
     * @property string $version HydeFront SemVer Tag
20
     */
21
    public string $version = 'v2.0';
22
23
    public function version(): string
24
    {
25
        return $this->version;
26
    }
27
28
    public function constructCdnPath(string $file): string
29
    {
30
        return 'https://cdn.jsdelivr.net/npm/hydefront@'.$this->version().'/dist/'.$file;
31
    }
32
33
    /**
34
     * Alias for constructCdnPath.
35
     */
36
    public function cdnLink(string $file): string
37
    {
38
        return $this->constructCdnPath($file);
39
    }
40
41
    public function mediaLink(string $file): string
42
    {
43
        return Hyde::relativeLink("media/$file").$this->getCacheBustKey($file);
44
    }
45
46
    public function hasMediaFile(string $file): bool
47
    {
48
        return file_exists(Hyde::path('_media').'/'.$file);
49
    }
50
51
    protected function getCacheBustKey(string $file): string
52
    {
53
        if (! config('hyde.cache_busting', true)) {
54
            return '';
55
        }
56
57
        return '?v='.md5_file(Hyde::path("_media/$file"));
58
    }
59
}
60