Completed
Push — dev ( e6a229...fe6897 )
by Gaige
01:05
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
    public function crossOrigin(string $keyword): string
100
    {
101
        return "crossorigin=\"".$keyword."\"";
102
    }
103
104
    /**
105
     * Checks config for hash algorithm and if nothing is found default to SHA384
106
     *
107
     * @return string
108
     * @throws Exception
109
     */
110
    private
111
    static function selectAlgorithm(): string
112
    {
113
        if ( ! empty(config('vasri.hash-algorithm'))) {
114
            $algorithm = config('vasri.hash-algorithm');
115
            if ($algorithm !== self::SHA256
116
                && $algorithm !== self::SHA384
117
                && $algorithm !== self::SHA512
118
            ) {
119
                throw new Exception('Invalid or Unsupported Hash Algorithm');
120
            }
121
        } else {
122
            $algorithm = self::SHA384;
123
        }
124
125
        return $algorithm;
126
    }
127
128
    /**
129
     * Removes everything but the short extension
130
     *
131
     * @param  string  $path
132
     *
133
     * @return string
134
     */
135
    private
136
    static function parseExtension(
137
        string $path
138
    ): string {
139
        return preg_replace("#\?.*#", "", pathinfo($path, PATHINFO_EXTENSION));
140
    }
141
142
}
143