Builder::sri()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace ExoUNX\Vasri;
5
6
use Exception;
7
8
/**
9
 * Builds resources for the access class
10
 * Class Builder
11
 *
12
 * @package ExoUNX\Vasri
13
 * @author  Gaige Lama <[email protected]>
14
 * @license MIT License
15
 * @link    https://github.com/ExoUNX/Vasri
16
 */
17
class Builder
18
{
19
20
    /**
21
     * SHA256 Hash Algorithm
22
     */
23
    private const SHA256 = 'sha256';
24
25
    /**
26
     * SHA384 Hash Algorithm
27
     */
28
    private const SHA384 = 'sha384';
29
30
    /**
31
     * SHA512 Hash Algorithm
32
     */
33
    private const SHA512 = 'sha512';
34
35
    /**
36
     * Constructs a valid SRI string based on the input file
37
     *
38
     * @param  string  $file
39
     *
40
     *
41
     * @return string
42
     * @throws Exception
43
     */
44
    public function sri(string $file): string
45
    {
46
        $algorithm = self::selectAlgorithm();
47
48
        return sprintf('%s-%s', $algorithm, base64_encode(
49
            hash_file($algorithm, public_path($file), true)
50
        ));
51
    }
52
53
    /**
54
     * Constructs a query string containing the md5 hash of the input file
55
     *
56
     * @param  string  $file
57
     *
58
     * @return string
59
     */
60
    public function versioning(string $file): string
61
    {
62
        return sprintf('?id=%s', hash_file('md5', public_path($file)));
63
    }
64
65
    /**
66
     * Sets the HTML attribute based on the file extension and throws an exception if invalid
67
     *
68
     * @param  string  $file
69
     *
70
     * @return string
71
     * @throws Exception
72
     */
73
    public function attribute(string $file): string
74
    {
75
        $extension = self::parseExtension($file);
76
77
        switch ($extension) {
78
            case 'css':
79
                $attribute = 'href';
80
                break;
81
            case 'js':
82
                $attribute = 'src';
83
                break;
84
            default:
85
                throw new Exception('Invalid or Unsupported Extension');
86
        }
87
88
        return $attribute;
89
    }
90
91
    /**
92
     * Constructs crossOrigin attribute based on the keyword
93
     * Valid HTML options
94
     * anonymous | use-credentials | blank
95
     *
96
     *
97
     * @param  string  $keyword
98
     *
99
     * @return string
100
     */
101
    public function crossOrigin(string $keyword): string
102
    {
103
        return sprintf('crossorigin="%s"', $keyword);
104
    }
105
106
    /**
107
     * Checks config for hash algorithm and if nothing is found default to SHA384
108
     *
109
     * @return string
110
     * @throws Exception
111
     */
112
    private static function selectAlgorithm(): string
113
    {
114
        if ( ! empty(config('vasri.hash-algorithm'))) {
115
116
            $algorithm = config('vasri.hash-algorithm');
117
118
            if ($algorithm !== self::SHA256
119
                && $algorithm !== self::SHA384
120
                && $algorithm !== self::SHA512
121
            ) {
122
123
                throw new Exception('Invalid or Unsupported Hash Algorithm');
124
125
            }
126
        } else {
127
128
            $algorithm = self::SHA384;
129
130
        }
131
132
        return $algorithm;
133
    }
134
135
    /**
136
     * Removes everything but the short extension
137
     *
138
     * @param  string  $path
139
     *
140
     * @return string
141
     */
142
    public static function parseExtension(string $path): string
143
    {
144
        return preg_replace("#\?.*#", '', pathinfo($path, PATHINFO_EXTENSION));
145
    }
146
147
}
148