lara_gen_adv_asset()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
use CSlant\LaraGenAdv\Exceptions\LaravelGeneratorAdvancedException;
4
5
if (!function_exists('lara_gen_adv_dist_path')) {
6
    /**
7
     * Returns laravel-generator-advanced composer dist path.
8
     *
9
     * @param  string|null  $asset
10
     * @return string
11
     */
12
    function lara_gen_adv_dist_path(string $asset = null): string
13
    {
14
        $defaultPath = config('lara-gen-adv.defaults.paths.ui_package_path').'/dist/';
15
        $assetPath = config('lara-gen-adv.defaults.paths.lara_gen_adv_assets_path', $defaultPath);
16
        if (!str_ends_with($assetPath, '/')) {
17
            $assetPath .= '/';
18
        }
19
        $path = base_path($assetPath);
20
21
        if (!$asset) {
22
            return realpath($path);
23
        }
24
25
        return realpath($path.$asset);
26
    }
27
}
28
29
if (!function_exists('lara_gen_adv_asset')) {
30
    /**
31
     * Returns asset from laravel-generator-advanced composer package.
32
     *
33
     * @param  string  $asset
34
     *
35
     * @return string
36
     *
37
     * @throws LaravelGeneratorAdvancedException
38
     */
39
    function lara_gen_adv_asset(string $asset): string
40
    {
41
        $file = lara_gen_adv_dist_path($asset);
42
43
        if (!file_exists($file)) {
44
            throw new LaravelGeneratorAdvancedException(sprintf('%s - this Laravel Generator asset does not exist', $asset));
45
        }
46
47
        $useAbsolutePath = config('lara-gen-adv.defaults.paths.use_absolute_path');
48
49
        return route('lara_gen_adv.asset', ['asset' => $asset], $useAbsolutePath).'?v='.filemtime($file);
50
    }
51
}
52
53
if (!function_exists('lara_gen_adv_dist_path_allowed')) {
54
    /**
55
     * Returns asset allowed from laravel-generator-advanced composer package.
56
     *
57
     * @param  string  $asset
58
     *
59
     * @return string
60
     *
61
     * @throws LaravelGeneratorAdvancedException
62
     */
63
    function lara_gen_adv_asset_allowed(string $asset): string
64
    {
65
        $allowed_files = [
66
            'favicon-16x16.png',
67
            'favicon-32x32.png',
68
        ];
69
70
        if (!in_array($asset, $allowed_files)) {
71
            throw new LaravelGeneratorAdvancedException(sprintf('%s - this Laravel Generator Advanced asset is not allowed', $asset));
72
        }
73
74
        return lara_gen_adv_asset($asset);
75
    }
76
}
77