Completed
Branch dev (450541)
by Gaige
03:34
created

Builder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A sri() 0 10 1
A versioning() 0 4 1
A attribute() 0 16 3
A selectAlgorithm() 0 11 4
1
<?php
2
3
4
namespace ExoUNX\Vasri;
5
6
use Exception;
7
8
/**
9
 * Class Builder
10
 * @package ExoUNX\Vasri
11
 */
12
class Builder
13
{
14
15
    /**
16
     * SHA256 Hash Algorithm
17
     */
18
    private const SHA256 = 'sha256';
19
20
    /**
21
     * SHA384 Hash Algorithm
22
     */
23
    private const SHA384 = 'sha384';
24
25
    /**
26
     * SHA512 Hash Algorithm
27
     */
28
    private const SHA512 = 'sha512';
29
30
    /**
31
     *
32
     */
33
    private const SCRIPT = 'script';
34
35
    /**
36
     *
37
     */
38
    private const LINK = 'link';
39
40
    /**
41
     * Builder constructor.
42
     */
43
    public function __construct()
44
    {
45
        //TODO Set config options here
46
    }
47
48
    /**
49
     * @param  string  $file
50
     *
51
     * @param $algo
52
     *
53
     * @return string
54
     * @throws Exception
55
     */
56
    public function sri(string $file, $algo)
57
    {
58
        return $algo.'-'.base64_encode(
59
                hash_file(
60
                    self::selectAlgorithm(
61
                        $algo
62
                    ), $file, true
63
                )
64
            );
65
    }
66
67
    /**
68
     * @param  string  $file
69
     *
70
     * @return string
71
     */
72
    public function versioning(string $file)
73
    {
74
        return '?id='.hash_file('md5', $file);
75
    }
76
77
    /**
78
     * @param  string  $type
79
     *
80
     * @return string
81
     * @throws Exception
82
     */
83
    public function attribute(string $type): string
84
    {
85
86
        switch ($type) {
87
            case self::SCRIPT:
88
                $attr = 'src';
89
                break;
90
            case self::LINK:
91
                $attr = 'href';
92
                break;
93
            default:
94
                throw new Exception("Invalid or Unsupported Attribute");
95
        }
96
97
        return $attr;
98
    }
99
100
    /**
101
     * @param  string  $algorithm
102
     *
103
     * @return string
104
     * @throws Exception
105
     */
106
    private static function selectAlgorithm(string $algorithm): string
107
    {
108
        if ($algorithm === self::SHA256
109
            || $algorithm === self::SHA384
110
            || $algorithm === self::SHA512
111
        ) {
112
            return $algorithm;
113
        } else {
114
            throw new Exception('Invalid or Unsupported Hash Algorithm');
115
        }
116
    }
117
118
}
119