Completed
Push — master ( 13a1bf...61ee69 )
by Gaige
13s queued 10s
created

Vasri::isPath()   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 Illuminate\Support\Facades\File;
7
use Exception;
8
9
/**
10
 * Access class for Vasri
11
 * Class Vasri
12
 *
13
 * @package ExoUNX\Vasri
14
 * @author  Gaige Lama <[email protected]>
15
 * @license MIT License
16
 * @link    https://github.com/ExoUNX/Vasri
17
 */
18
class Vasri
19
{
20
21
22
    /**
23
     * @var Builder
24
     */
25
    private $builder;
26
27
    /**
28
     * @var ManifestReader
29
     */
30
    private $manifestReader;
31
32
    /**
33
     * @var array
34
     */
35
    private $vasriManifest;
36
37
    /**
38
     * Vasri constructor.
39
     */
40
    public function __construct()
41
    {
42
        $this->builder        = new Builder();
43
        $this->manifestReader = new ManifestReader();
44
        $this->vasriManifest  = $this->manifestReader->getVasriManifest();
45
    }
46
47
    /**
48
     * @param  string  $file
49
     * @param  bool  $enableVersioning
50
     * @param  bool  $enableSRI
51
     *
52
     * @param  string  $keyword
53
     *
54
     * @return string
55
     * @throws Exception
56
     */
57
    public function vasri(
58
        string $file,
59
        bool $enableVersioning = true,
60
        bool $enableSRI = true,
61
        string $keyword = 'anonymous'
62
    ): string {
63
        $output = '';
64
65
        if (self::isFile($file) === true) {
66
            $output .= $this->addAttribute($file, $enableVersioning);
67
            if ($enableSRI === true) {
68
                $output .= $this->addSRI($file, $keyword);
69
            }
70
71
            return $output;
72
73
        } else {
74
            throw new Exception('Incorrect file path or file does not exist for local asset');
75
        }
76
    }
77
78
    /**
79
     * @param  string  $file
80
     *
81
     * @return string
82
     * @throws Exception
83
     */
84
    private function addSRI(string $file, string $keyword): string
85
    {
86
        return " integrity=\"".$this->vasriManifest[$file]['sri']."\" ".$this->builder->crossOrigin($keyword);
87
    }
88
89
    /**
90
     * @param  string  $file
91
     * @param  bool  $enableVersioning
92
     *
93
     * @return string
94
     * @throws Exception
95
     */
96
    private function addAttribute(string $file, bool $enableVersioning)
97
    {
98
        try {
99
            if ($enableVersioning === true) {
100
                $output = $this->builder->attribute($file)."=\"".$file.$this->vasriManifest[$file]['version']."\"";
101
            } else {
102
                $output = $this->builder->attribute($file)."=\"".$file."\"";
103
            }
104
105
            return $output;
106
        } catch (Exception $e) {
107
            throw new Exception($e);
108
        }
109
    }
110
111
    /**
112
     *
113
     * @param  string  $file
114
     *
115
     * @return bool
116
     */
117
    private static function isFile(string $file): bool
118
    {
119
        return File::exists(public_path($file));
120
    }
121
122
}
123