Completed
Push — dev ( d88586...465712 )
by Gaige
01:08
created

ManifestBuilder::buildAssets()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 2
dl 0
loc 32
rs 8.4746
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
 * Constructs Manifest
11
 * Class ManifestBuilder
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 ManifestBuilder
19
{
20
21
    /**
22
     * @var array
23
     */
24
    private $vasriConfig;
25
    /**
26
     * @var bool
27
     */
28
    private $isMixManifestEnabled;
29
30
    /**
31
     * @var string
32
     */
33
    private $mixManifestPath;
34
    /**
35
     * @var array
36
     */
37
    private $mixManifest;
38
    /**
39
     * @var ManifestReader
40
     */
41
    private $manifestReader;
42
43
    /**
44
     * @var Builder
45
     */
46
    private $builder;
47
48
    /**
49
     * ManifestBuilder constructor.
50
     * @throws Exception
51
     */
52
    public function __construct()
53
    {
54
        $this->manifestReader          = new ManifestReader();
55
        $this->builder                 = new Builder();
56
        $this->vasriConfig             = config('vasri');
57
        $this->isMixManifestEnabled    = $this->vasriConfig['mix-manifest'];
58
        $this->isMixManifestAltEnabled = $this->vasriConfig['mix-manifest-alt'];
0 ignored issues
show
Bug introduced by
The property isMixManifestAltEnabled does not seem to exist. Did you mean mixManifest?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
59
        $this->mixManifestPath         = public_path('mix-manifest.json');
60
    }
61
62
    /**
63
     * Builds the basic asset list
64
     *
65
     * @param  array  $mixManifest
66
     * @param  array  $vasriConfigAssets
67
     *
68
     * @return array
69
     * @throws Exception
70
     */
71
    private function buildAssets(array $mixManifest = [], array $vasriConfigAssets = []): array
72
    {
73
        $vasriManifest = [];
74
75
        if ($this->isMixManifestEnabled && File::exists($this->mixManifestPath)) {
76
77
            if ($this->isMixManifestAltEnabled) {
0 ignored issues
show
Bug introduced by
The property isMixManifestAltEnabled does not seem to exist. Did you mean mixManifest?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
78
79
                foreach ($mixManifest as $key => $val) {
80
                    $vasriManifest[] = $val;
81
                }
82
83
            } else {
84
85
                foreach ($mixManifest as $key => $val) {
86
                    $vasriManifest[] = $key;
87
                }
88
89
            }
90
91
        } elseif ( ! empty($vasriConfigAssets)) {
92
93
            $vasriManifest = $this->vasriConfig['assets'];
94
95
        } else {
96
97
            throw new Exception('No manifest or valid assets found');
98
99
        }
100
101
        return $vasriManifest;
102
    }
103
104
    /**
105
     * Builds the manifest based off the asset list
106
     *
107
     * @return array
108
     * @throws Exception
109
     */
110
    private function buildManifest(): array
111
    {
112
        $manifest = [];
113
114
        foreach (
115
            $this->buildAssets(
116
                $this->manifestReader->getManifest($this->mixManifestPath),
117
                $this->vasriConfig['assets']
118
            ) as $asset
119
        ) {
120
            $manifest[$asset] = [
121
                'sri'     => $this->builder->sri($asset),
122
                'version' => $this->builder->versioning($asset)
123
            ];
124
        }
125
126
        return $manifest;
127
    }
128
129
    /**
130
     * Deploys the manifest as json file in the Laravel base directory
131
     *
132
     * @throws Exception
133
     */
134
    public function deployManifest(): void
135
    {
136
        file_put_contents(base_path('vasri-manifest.json'),
137
            stripslashes(json_encode($this->buildManifest(), JSON_PRETTY_PRINT)));
138
    }
139
140
}
141