Completed
Push — master ( 314785...a2e7bc )
by Gaige
19s queued 11s
created

Vasri::__construct()   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 0
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
 * Access class for Vasri
11
 * Class Vasri
12
 *
13
 * @package ExoUNX\Vasri
14
 * @author  Gaige Lama <[email protected]>
15
 * @license MIT License
16
 * @link    https://github.com/ExoUNX/Vasri
17
 */
18
class Vasri
19
{
20
21
    /**
22
     * @var Builder|null
23
     */
24
    private static $builder = null;
25
26
    /**
27
     * Vasri constructor.
28
     */
29
    public function __construct()
30
    {
31
        self::$builder = self::loadBuilder();
32
    }
33
34
    /**
35
     * @param  string  $path
36
     * @param  bool  $enableVersioning
37
     * @param  bool  $enableSRI
38
     *
39
     * @return string
40
     * @throws Exception
41
     */
42
    public static function vasri(
43
        string $path,
44
        bool $enableVersioning = true,
45
        bool $enableSRI = true
46
    ): string {
47
        $output = '';
48
49
        if (self::isPath($path) === true) {
50
            $output .= self::addAttribute($path, $enableVersioning);
51
            if ($enableSRI === true) {
52
                $output .= self::addSRI($path);
53
            }
54
55
            return $output;
56
57
        } else {
58
            throw new Exception('Incorrect file path or file does not exist');
59
        }
60
    }
61
62
    /**
63
     * @param  string  $path
64
     *
65
     * @return string
66
     */
67
    private static function addVersioning(string $path): string
68
    {
69
        return self::$builder->versioning($path);
70
    }
71
72
    /**
73
     * @param  string  $path
74
     *
75
     * @return string
76
     * @throws Exception
77
     */
78
    private static function addSRI(string $path): string
79
    {
80
        return self::$builder->sri($path);
81
    }
82
83
    /**
84
     * @return Builder
85
     */
86
    private static function loadBuilder(): Builder
87
    {
88
        return new Builder();
89
    }
90
91
    /**
92
     * @param  string  $path
93
     * @param  bool  $enableVersioning
94
     *
95
     * @return string
96
     * @throws Exception
97
     */
98
    private static function addAttribute(string $path, bool $enableVersioning)
99
    {
100
        try {
101
            if ($enableVersioning === true) {
102
                $output = self::$builder->attribute($path)."=\"".self::addVersioning($path)."\"";
103
            } else {
104
                $output = self::$builder->attribute($path)."=\"".$path."\"";
105
            }
106
107
            return $output;
108
        } catch (Exception $e) {
109
            throw new Exception($e);
110
        }
111
    }
112
113
    /**
114
     *
115
     * @param  string  $path
116
     *
117
     * @return bool
118
     */
119
    private static function isPath(string $path): bool
120
    {
121
        return File::exists(public_path($path));
122
    }
123
124
}
125