Completed
Push — dev ( 04c187...fb69e9 )
by Gaige
01:11
created

Vasri::addAttributes()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 13
nop 4
dl 0
loc 19
rs 8.4444
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
        try {
101
            if ($enableVersioning && ($this->appEnvironment !== 'local' || config('vasri.local-versioning'))) {
102
                $output = $this->getSourceAttribute($file, $this->getVersioning($file));
103
            } elseif ($this->appEnvironment === 'local' && ! config('vasri.local-versioning')) {
104
                $output = $this->getSourceAttribute($file);
105
            } else {
106
                $output = $this->getSourceAttribute($file);
107
            }
108
            if ($enableSRI) {
109
                $output .= $this->getSRI($file, $keyword);
110
            }
111
112
            return $output;
113
        } catch (Exception $e) {
114
            throw new Exception($e);
115
        }
116
    }
117
118
    private function getVersioning(string $file): string
119
    {
120
        return $this->vasriManifest[$file]['version'];
121
    }
122
123
    private function getSourceAttribute(string $file, string $version = ''): string
124
    {
125
        return $this->builder->attribute($file).'="'.$file.$version.'"';
126
    }
127
128
    /**
129
     *
130
     * @param  string  $file
131
     *
132
     * @return bool
133
     */
134
    private static function isPublicFile(string $file): bool
135
    {
136
        return File::exists(public_path($file));
137
    }
138
139
}
140