Completed
Push — master ( 99aaa6...13a1bf )
by Gaige
13s queued 10s
created

Builder::crossOrigin()   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 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
     * Builder constructor.
37
     */
38
    public function __construct()
39
    {
40
        //TODO Set config options here
41
    }
42
43
    /**
44
     * Constructs a valid SRI string
45
     *
46
     * @param  string  $file
47
     *
48
     *
49
     * @return string
50
     * @throws Exception
51
     */
52
    public function sri(string $file)
53
    {
54
        $algorithm = self::selectAlgorithm();
55
56
        return $algorithm.'-'.base64_encode(
57
                hash_file($algorithm, public_path($file), true)
58
            );
59
    }
60
61
    /**
62
     * Constructs a query string containing the md5 hash of the input file
63
     *
64
     * @param  string  $file
65
     *
66
     * @return string
67
     */
68
    public function versioning(string $file)
69
    {
70
        return '?id='.hash_file('md5', public_path($file));
71
    }
72
73
    /**
74
     * Sets the HTML attribute based on the file extension and throws an exception if invalid
75
     *
76
     * @param  string  $path
77
     *
78
     * @return string
79
     * @throws Exception
80
     */
81
    public function attribute(string $path): string
82
    {
83
        $extension = self::parseExtension($path);
84
85
        switch ($extension) {
86
            case 'css':
87
                $attribute = 'href';
88
                break;
89
            case 'js':
90
                $attribute = 'src';
91
                break;
92
            default:
93
                throw new Exception('Invalid or Unsupported Extension');
94
        }
95
96
        return $attribute;
97
    }
98
99
    /**
100
     * Constructs crossOrigin attribute
101
     *
102
     * @param  string  $keyword
103
     *
104
     * @return string
105
     */
106
    public function crossOrigin(string $keyword): string
107
    {
108
        return "crossorigin=\"".$keyword."\"";
109
    }
110
111
    /**
112
     * Checks config for hash algorithm and if nothing is found default to SHA384
113
     *
114
     * @return string
115
     * @throws Exception
116
     */
117
    private
118
    static function selectAlgorithm(): string
119
    {
120
        if ( ! empty(config('vasri.hash-algorithm'))) {
121
            $algorithm = config('vasri.hash-algorithm');
122
            if ($algorithm !== self::SHA256
123
                && $algorithm !== self::SHA384
124
                && $algorithm !== self::SHA512
125
            ) {
126
                throw new Exception('Invalid or Unsupported Hash Algorithm');
127
            }
128
        } else {
129
            $algorithm = self::SHA384;
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
    private
143
    static function parseExtension(
144
        string $path
145
    ): string {
146
        return preg_replace("#\?.*#", "", pathinfo($path, PATHINFO_EXTENSION));
147
    }
148
149
}
150