Completed
Pull Request — master (#12)
by Gaige
01:01
created

Vasri::getOptions()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 2
dl 0
loc 11
rs 9.6111
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
     * @var mixed
39
     */
40
    private $appEnvironment;
41
42
    /**
43
     * @var
44
     */
45
    private $vasriConfig;
46
47
    /**
48
     * Vasri constructor.
49
     */
50
    public function __construct()
51
    {
52
        $this->builder        = new Builder();
53
        $this->manifestReader = new ManifestReader();
54
        $this->vasriConfig    = config('vasri');
55
        $this->vasriManifest  = $this->manifestReader->getManifest(base_path('vasri-manifest.json'));
56
        $this->appEnvironment = env('APP_ENV', 'production');
57
    }
58
59
    /**
60
     * The Vasri helper function
61
     *
62
     * @param  string  $file
63
     * @param  bool  $enableVersioning
64
     * @param  bool  $enableSRI
65
     *
66
     * @param  string  $keyword
67
     *
68
     * @return string
69
     * @throws Exception
70
     */
71
    public function vasri(
72
        string $file,
73
        bool $enableVersioning = true,
74
        bool $enableSRI = true,
75
        string $keyword = 'anonymous'
76
    ): string {
77
        if (self::isPublicFile($file)) {
78
79
            return $this->addAttributes($file, $enableVersioning, $enableSRI, $keyword);
80
81
        } else {
82
83
            throw new Exception('Incorrect file path or file does not exist for local asset');
84
85
        }
86
    }
87
88
    /**
89
     * Fetches the SRI hash from the Vasri Manifest and adds the crossorigin attribute
90
     *
91
     * @param  string  $file
92
     *
93
     * @param  string  $keyword
94
     *
95
     * @return string
96
     */
97
    private function getSRI(string $file, string $keyword): string
98
    {
99
100
        return sprintf('integrity="%s" %s', $this->vasriManifest[$file]['sri'], $this->builder->crossOrigin($keyword));
101
    }
102
103
    /**
104
     * Builds all the attributes
105
     *
106
     * @param  string  $file
107
     * @param  bool  $enableVersioning
108
     *
109
     * @param  bool  $enableSRI
110
     * @param  string  $keyword
111
     *
112
     * @return string
113
     * @throws Exception
114
     */
115
    private function addAttributes(string $file, bool $enableVersioning, bool $enableSRI, string $keyword): string
116
    {
117
118
        $option = $this->getOptions($enableVersioning, $enableSRI);
119
120
        $output = $this->getSourceAttribute($file, $this->getVersioning($file));
121
122
        if ( ! $option['versioning']) {
123
124
            $output = $this->getSourceAttribute($file);
125
126
        }
127
        if ($option['sri']) {
128
129
            $output = sprintf('%s %s', $output, $this->getSRI($file, $keyword));
130
131
        }
132
133
        return $output;
134
    }
135
136
    /**
137
     * Fetches the version query string from the Vasri Manifest
138
     *
139
     * @param  string  $file
140
     *
141
     * @return string
142
     */
143
    private function getVersioning(string $file): string
144
    {
145
        return $this->vasriManifest[$file]['version'];
146
    }
147
148
    /**
149
     * Figures out whether or not to toggle versioning and sri
150
     *
151
     * @param  bool  $enableVersioning
152
     * @param  bool  $enableSRI
153
     *
154
     * @return array
155
     */
156
    private function getOptions(bool $enableVersioning, bool $enableSRI): array
157
    {
158
        return [
159
            'versioning' => ! ($this->appEnvironment === 'local'
160
                               && ! config('vasri.local-versioning')
161
                               || ! $enableVersioning
162
                               || ! $this->vasriConfig['versioning']
163
            ),
164
            'sri'        => $enableSRI && $this->vasriConfig['sri'],
165
        ];
166
    }
167
168
    /**
169
     * Gets source attribute based on the extension, adds file path and version
170
     *
171
     * @param  string  $file
172
     * @param  string  $version
173
     *
174
     * @return string
175
     * @throws Exception
176
     */
177
    private function getSourceAttribute(string $file, string $version = ''): string
178
    {
179
        return sprintf('%s="%s%s"', $this->builder->attribute($file), $file, $version);
180
    }
181
182
    /**
183
     * Checks if the file is in the Laravel public path
184
     *
185
     * @param  string  $file
186
     *
187
     * @return bool
188
     */
189
    private static function isPublicFile(string $file): bool
190
    {
191
        return File::exists(public_path($file));
192
    }
193
194
}
195