Completed
Push — dev ( 6b9c98...6e7249 )
by Gaige
01:06
created

Vasri::checkFileName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
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
     * @var mixed
39
     */
40
    private $appEnvironment;
41
42
    /**
43
     * @var
44
     */
45
    private $vasriConfig;
46
47
    /**
48
     * @var
49
     */
50
    private $isMixManifestAltEnabled;
51
52
    /**
53
     * Vasri constructor.
54
     */
55 View Code Duplication
    public function __construct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $this->builder                 = new Builder();
58
        $this->manifestReader          = new ManifestReader();
59
        $this->vasriConfig             = config('vasri');
60
        $this->isMixManifestAltEnabled = $this->vasriConfig['mix-manifest-alt'];
61
        $this->vasriManifest           = $this->manifestReader->getManifest(base_path('vasri-manifest.json'));
62
        $this->appEnvironment          = env('APP_ENV', 'production');
63
    }
64
65
    /**
66
     * The Vasri helper function
67
     *
68
     * @param  string  $file
69
     * @param  bool  $enableVersioning
70
     * @param  bool  $enableSRI
71
     *
72
     * @param  string  $keyword
73
     *
74
     * @return string
75
     * @throws Exception
76
     */
77
    public function vasri(
78
        string $file,
79
        bool $enableVersioning = true,
80
        bool $enableSRI = true,
81
        string $keyword = 'anonymous'
82
    ): string {
83
        if (self::isPublicFile($file)) {
84
85
            $this->checkFileName($file);
86
87
            return $this->addAttributes($file, $enableVersioning, $enableSRI, $keyword);
88
89
        } else {
90
91
            throw new Exception('Incorrect file path or file does not exist for local asset');
92
93
        }
94
    }
95
96
    /**
97
     * Fetches the SRI hash from the Vasri Manifest and adds the crossorigin attribute
98
     *
99
     * @param  string  $file
100
     *
101
     * @param  string  $keyword
102
     *
103
     * @return string
104
     */
105
    private function getSRI(string $file, string $keyword): string
106
    {
107
108
        return sprintf('integrity="%s" %s', $this->vasriManifest[$file]['sri'], $this->builder->crossOrigin($keyword));
109
    }
110
111
    /**
112
     * @param  string  $file
113
     */
114
    private function checkFileName(string &$file): void
115
    {
116
        if ($this->isMixManifestAltEnabled) {
117
118
            $file = $this->vasriManifest[$file]['alt'];
119
120
        }
121
    }
122
123
    /**
124
     * Builds all the attributes
125
     *
126
     * @param  string  $file
127
     * @param  bool  $enableVersioning
128
     *
129
     * @param  bool  $enableSRI
130
     * @param  string  $keyword
131
     *
132
     * @return string
133
     * @throws Exception
134
     */
135
    private function addAttributes(string $file, bool $enableVersioning, bool $enableSRI, string $keyword): string
136
    {
137
        $option = $this->getOptions($enableVersioning, $enableSRI);
138
        $output = $this->getSourceAttribute($file);
139
140
        if ($option['versioning']) {
141
142
            $output = $this->getSourceAttribute($file, $this->getVersioning($file));
143
144
        }
145
        if ($option['sri']) {
146
147
            $output = sprintf('%s %s', $output, $this->getSRI($file, $keyword));
148
149
        }
150
151
        return $output;
152
    }
153
154
    /**
155
     * Fetches the version query string from the Vasri Manifest
156
     *
157
     * @param  string  $file
158
     *
159
     * @return string
160
     */
161
    private function getVersioning(string $file): string
162
    {
163
        return $this->vasriManifest[$file]['version'];
164
    }
165
166
    /**
167
     * Figures out whether or not to toggle versioning and sri
168
     *
169
     * @param  bool  $enableVersioning
170
     * @param  bool  $enableSRI
171
     *
172
     * @return array
173
     */
174
    private function getOptions(bool $enableVersioning, bool $enableSRI): array
175
    {
176
        return [
177
            'versioning' => ! ($this->appEnvironment === 'local'
178
                               && ! config('vasri.local-versioning')
179
                               || ! $enableVersioning
180
                               || ! $this->vasriConfig['versioning']
181
            ),
182
            'sri'        => $enableSRI && $this->vasriConfig['sri'],
183
        ];
184
    }
185
186
    /**
187
     * Gets source attribute based on the extension, adds file path and version
188
     *
189
     * @param  string  $file
190
     * @param  string  $version
191
     *
192
     * @return string
193
     * @throws Exception
194
     */
195
    private function getSourceAttribute(string $file, string $version = ''): string
196
    {
197
        return sprintf('%s="%s%s"', $this->builder->attribute($file), $file, $version);
198
    }
199
200
    /**
201
     * Checks if the file is in the Laravel public path
202
     *
203
     * @param  string  $file
204
     *
205
     * @return bool
206
     */
207
    private static function isPublicFile(string $file): bool
208
    {
209
        return File::exists(public_path($file));
210
    }
211
212
}
213