Completed
Push — dev ( 4eb968...60fd4e )
by Gaige
58s
created

Vasri::isPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace ExoUNX\Vasri;
5
6
use Illuminate\Support\Facades\File;
7
use Exception;
8
9
/**
10
 * Class Vasri
11
 * @package ExoUNX\Vasri
12
 */
13
class Vasri
14
{
15
16
    /**
17
     * @var Builder|null
18
     */
19
    private static $builder = null;
20
21
    /**
22
     * Vasri constructor.
23
     */
24
    public function __construct()
25
    {
26
        self::$builder = self::loadBuilder();
27
    }
28
29
    /**
30
     * @param  string  $type
31
     * @param  string  $path
32
     * @param  bool  $enableVersioning
33
     * @param  bool  $enableSRI
34
     *
35
     * @return string
36
     * @throws Exception
37
     */
38
    public static function vasri(
39
        string $type,
40
        string $path,
41
        bool $enableVersioning = true,
42
        bool $enableSRI = true
43
    ): string {
44
        $output = '';
45
46
        if (self::isPath($path) === true) {
47
            $output .= self::addAttribute($type, $path, $enableVersioning);
48
            if ($enableSRI === true) {
49
                $output .= self::addSRI($path);
50
            }
51
52
            return $output;
53
54
        } else {
55
            throw new Exception('Incorrect file path or file does not exist');
56
        }
57
    }
58
59
    /**
60
     * @param  string  $path
61
     *
62
     * @return string
63
     */
64
    private static function addVersioning(string $path): string
65
    {
66
        return self::$builder->versioning($path);
67
    }
68
69
    /**
70
     * @param  string  $path
71
     * @param  string  $hash
72
     *
73
     * @return string
74
     * @throws Exception
75
     */
76
    private static function addSRI(string $path, string $hash = 'sha384'): string
77
    {
78
        return self::$builder->sri($path, $hash);
79
    }
80
81
    /**
82
     * @return Builder
83
     */
84
    private static function loadBuilder(): Builder
85
    {
86
        return new Builder();
87
    }
88
89
    /**
90
     * @param  string  $type
91
     * @param  string  $path
92
     * @param  bool  $enableVersioning
93
     *
94
     * @return string
95
     * @throws Exception
96
     */
97
    private static function addAttribute(string $type, string $path, bool $enableVersioning)
98
    {
99
        try {
100
            if ($enableVersioning === true) {
101
                $output = self::$builder->attribute($type)."=\"".self::addVersioning($path)."\"";
102
            } else {
103
                $output = self::$builder->attribute($type)."=\"".$path."\"";
104
            }
105
106
            return $output;
107
        } catch (Exception $e) {
108
            throw new Exception($e);
109
        }
110
    }
111
112
    /**
113
     *
114
     * @param  string  $path
115
     *
116
     * @return bool
117
     */
118
    private static function isPath(string $path): bool
119
    {
120
        return File::exists(public_path($path));
121
    }
122
123
}
124