Completed
Push — dev ( cdbba9...f71fb8 )
by Gaige
01:11
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
     * @param  string  $file
61
     * @param  bool  $enableVersioning
62
     * @param  bool  $enableSRI
63
     *
64
     * @param  string  $keyword
65
     *
66
     * @return string
67
     * @throws Exception
68
     */
69
    public function vasri(
70
        string $file,
71
        bool $enableVersioning = true,
72
        bool $enableSRI = true,
73
        string $keyword = 'anonymous'
74
    ): string {
75
        if (self::isPublicFile($file)) {
76
77
            return $this->addAttributes($file, $enableVersioning, $enableSRI, $keyword);
78
79
        } else {
80
81
            throw new Exception('Incorrect file path or file does not exist for local asset');
82
83
        }
84
    }
85
86
    /**
87
     * @param  string  $file
88
     *
89
     * @param  string  $keyword
90
     *
91
     * @return string
92
     */
93
    private function getSRI(string $file, string $keyword): string
94
    {
95
96
        return sprintf('integrity="%s" %s', $this->vasriManifest[$file]['sri'], $this->builder->crossOrigin($keyword));
97
    }
98
99
    /**
100
     * @param  string  $file
101
     * @param  bool  $enableVersioning
102
     *
103
     * @param  bool  $enableSRI
104
     * @param  string  $keyword
105
     *
106
     * @return string
107
     * @throws Exception
108
     */
109
    private function addAttributes(string $file, bool $enableVersioning, bool $enableSRI, string $keyword): string
110
    {
111
112
        $option = $this->getOptions($enableVersioning, $enableSRI);
113
114
        $output = $this->getSourceAttribute($file, $this->getVersioning($file));
115
116
        if ( ! $option['versioning']) {
117
118
            $output = $this->getSourceAttribute($file);
119
120
        }
121
        if ($option['sri']) {
122
123
            $output = sprintf('%s %s', $output, $this->getSRI($file, $keyword));
124
125
        }
126
127
        return $output;
128
    }
129
130
    /**
131
     * @param  string  $file
132
     *
133
     * @return string
134
     */
135
    private function getVersioning(string $file): string
136
    {
137
        return $this->vasriManifest[$file]['version'];
138
    }
139
140
    private function getOptions(bool $enableVersioning, bool $enableSRI): array
141
    {
142
        return [
143
            'versioning' => ! ($this->appEnvironment === 'local'
144
                               && ! config('vasri.local-versioning')
145
                               || ! $enableVersioning
146
                               || ! $this->vasriConfig['versioning']
147
            ),
148
            'sri'        => $enableSRI && $this->vasriConfig['sri'],
149
        ];
150
    }
151
152
    /**
153
     * @param  string  $file
154
     * @param  string  $version
155
     *
156
     * @return string
157
     * @throws Exception
158
     */
159
    private function getSourceAttribute(string $file, string $version = ''): string
160
    {
161
        return sprintf('%s="%s%s"', $this->builder->attribute($file), $file, $version);
162
    }
163
164
    /**
165
     *
166
     * @param  string  $file
167
     *
168
     * @return bool
169
     */
170
    private static function isPublicFile(string $file): bool
171
    {
172
        return File::exists(public_path($file));
173
    }
174
175
}
176