Completed
Push — dev ( 18c6dc...e089f2 )
by Gaige
01:01
created

Builder::parseExtension()   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
     * @param  string  $file
45
     *
46
     * @param $algorithm
47
     *
48
     * @return string
49
     * @throws Exception
50
     */
51
    public function sri(string $file, $algorithm)
52
    {
53
        return $algorithm.'-'.base64_encode(
54
                hash_file(
55
                    self::selectAlgorithm(
56
                        $algorithm
57
                    ), $file, true
58
                )
59
            );
60
    }
61
62
    /**
63
     * @param  string  $file
64
     *
65
     * @return string
66
     */
67
    public function versioning(string $file)
68
    {
69
        return '?id='.hash_file('md5', $file);
70
    }
71
72
    /**
73
     * @param  string  $path
74
     *
75
     * @return string
76
     * @throws Exception
77
     */
78
    public function attribute(string $path): string
79
    {
80
        $extension = self::parseExtension($path);
81
82
        switch ($extension) {
83
            case 'css':
84
                $attribute = 'href';
85
                break;
86
            case 'js':
87
                $attribute = 'src';
88
                break;
89
            default:
90
                throw new Exception('Invalid or Unsupported Extension');
91
        }
92
93
        return $attribute;
94
    }
95
96
    /**
97
     * @param  string  $algorithm
98
     *
99
     * @return string
100
     * @throws Exception
101
     */
102
    private static function selectAlgorithm(string $algorithm): string
103
    {
104
        if ($algorithm === self::SHA256
105
            || $algorithm === self::SHA384
106
            || $algorithm === self::SHA512
107
        ) {
108
            return $algorithm;
109
        } else {
110
            throw new Exception('Invalid or Unsupported Hash Algorithm');
111
        }
112
    }
113
114
    private static function parseExtension(string $path): string
115
    {
116
        return preg_replace("#\?.*#", "", pathinfo($path, PATHINFO_EXTENSION));
117
    }
118
119
}
120