Completed
Push — dev ( fb69e9...9f0630 )
by Gaige
01:01
created

Vasri::addAttributes()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 4
dl 0
loc 13
rs 9.5222
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
     * Vasri constructor.
44
     */
45
    public function __construct()
46
    {
47
        $this->builder        = new Builder();
48
        $this->manifestReader = new ManifestReader();
49
        $this->vasriManifest  = $this->manifestReader->getManifest(base_path('vasri-manifest.json'));
50
        $this->appEnvironment = env('APP_ENV', 'production');
51
    }
52
53
    /**
54
     * @param  string  $file
55
     * @param  bool  $enableVersioning
56
     * @param  bool  $enableSRI
57
     *
58
     * @param  string  $keyword
59
     *
60
     * @return string
61
     * @throws Exception
62
     */
63
    public function vasri(
64
        string $file,
65
        bool $enableVersioning = true,
66
        bool $enableSRI = true,
67
        string $keyword = 'anonymous'
68
    ): string {
69
        if (self::isPublicFile($file) === true) {
70
            return $this->addAttributes($file, $enableVersioning, $enableSRI, $keyword);
71
        } else {
72
            throw new Exception('Incorrect file path or file does not exist for local asset');
73
        }
74
    }
75
76
    /**
77
     * @param  string  $file
78
     *
79
     * @param  string  $keyword
80
     *
81
     * @return string
82
     */
83
    private function getSRI(string $file, string $keyword): string
84
    {
85
        return ' integrity="'.$this->vasriManifest[$file]['sri'].'" '.$this->builder->crossOrigin($keyword);
86
    }
87
88
    /**
89
     * @param  string  $file
90
     * @param  bool  $enableVersioning
91
     *
92
     * @param  bool  $enableSRI
93
     * @param  string  $keyword
94
     *
95
     * @return string
96
     * @throws Exception
97
     */
98
    private function addAttributes(string $file, bool $enableVersioning, bool $enableSRI, string $keyword): string
99
    {
100
        $output = $this->getSourceAttribute($file, $this->getVersioning($file));
101
102
        if ($this->appEnvironment === 'local' && ! config('vasri.local-versioning') || ! $enableVersioning) {
103
            $output = $this->getSourceAttribute($file);
104
        }
105
        if ($enableSRI) {
106
            $output .= $this->getSRI($file, $keyword);
107
        }
108
109
        return $output;
110
    }
111
112
    private function getVersioning(string $file): string
113
    {
114
        return $this->vasriManifest[$file]['version'];
115
    }
116
117
    private function getSourceAttribute(string $file, string $version = ''): string
118
    {
119
        return $this->builder->attribute($file).'="'.$file.$version.'"';
120
    }
121
122
    /**
123
     *
124
     * @param  string  $file
125
     *
126
     * @return bool
127
     */
128
    private static function isPublicFile(string $file): bool
129
    {
130
        return File::exists(public_path($file));
131
    }
132
133
}
134