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

Vasri::addAttribute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 3
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
4
namespace ExoUNX\Vasri;
5
6
use Illuminate\Support\Facades\File;
7
use Exception;
8
9
/**
10
 * Class Vasri
11
 * @package ExoUNX\Vasri
12
 */
13
class Vasri
14
{
15
16
    /**
17
     * @var Builder|null
18
     */
19
    private static $builder = null;
20
21
    /**
22
     * Vasri constructor.
23
     */
24
    public function __construct()
25
    {
26
        self::$builder = self::loadBuilder();
27
    }
28
29
    /**
30
     * @param  string  $type
31
     * @param  string  $path
32
     * @param  bool  $enable_versioning
33
     * @param  bool  $enable_sri
34
     *
35
     * @return string
36
     * @throws Exception
37
     */
38
    public static function vasri(
39
        string $type,
40
        string $path,
41
        bool $enable_versioning = true,
42
        bool $enable_sri = true
43
    ): string {
44
        $output = '';
45
46
        if (self::checkPath($path) === true) {
47
            $output .= self::addAttribute($type, $path, $enable_versioning);
0 ignored issues
show
Documentation introduced by
$enable_versioning is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
            if ($enable_sri === true) {
49
                $output .= self::addSRI($path);
50
            }
51
52
            return $output;
53
54
        } else {
55
            throw new Exception('Incorrect file path or file does not exist');
56
        }
57
58
59
    }
60
61
    /**
62
     * @param  string  $path
63
     *
64
     * @return string
65
     */
66
    private static function addVersioning(string $path): string
67
    {
68
        return self::$builder->versioning($path);
69
    }
70
71
    /**
72
     * @param  string  $path
73
     * @param  string  $hash
74
     *
75
     * @return string
76
     * @throws Exception
77
     */
78
    private static function addSRI(string $path, string $hash = 'sha384'): string
79
    {
80
        return self::$builder->sri($path, $hash);
81
    }
82
83
    /**
84
     * @return Builder
85
     */
86
    private static function loadBuilder(): Builder
87
    {
88
        return new Builder();
89
    }
90
91
    /**
92
     * @param  string  $type
93
     * @param  string  $path
94
     * @param  string  $enable_versioning
95
     *
96
     * @return string
97
     * @throws Exception
98
     */
99
    private static function addAttribute(string $type, string $path, string $enable_versioning)
100
    {
101
        if ($enable_versioning === true) {
102
            try {
103
                return self::$builder->attribute($type)."=\"".self::addVersioning($path)."\"";
104
            } catch (Exception $e) {
105
                throw new Exception($e);
106
            }
107
        } else {
108
            try {
109
                return self::$builder->attribute($type)."=\"".$path."\"";
110
            } catch (Exception $e) {
111
                throw new Exception($e);
112
            }
113
        }
114
    }
115
116
    /**
117
     *
118
     * @param  string  $path
119
     *
120
     * @return bool
121
     */
122
    private static function checkPath(string $path): bool
123
    {
124
        return File::exists(public_path($path));
125
    }
126
127
}
128