Completed
Push — master ( 13a1bf...61ee69 )
by Gaige
13s queued 10s
created

ManifestBuilder::deployManifest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace ExoUNX\Vasri;
5
6
use Exception;
7
8
/**
9
 * Constructs Manifest
10
 * Class ManifestBuilder
11
 *
12
 * @package ExoUNX\Vasri
13
 * @author  Gaige Lama <[email protected]>
14
 * @license MIT License
15
 * @link    https://github.com/ExoUNX/Vasri
16
 */
17
class ManifestBuilder
18
{
19
20
    /**
21
     * @var
22
     */
23
    private $mixManifest;
24
25
    /**
26
     * @var ManifestReader
27
     */
28
    private $manifestReader;
29
30
    /**
31
     * @var Builder
32
     */
33
    private $builder;
34
35
    /**
36
     * ManifestBuilder constructor.
37
     */
38
    public function __construct()
39
    {
40
        $this->mixManifest    = config('vasri.mix-manifest');
41
        $this->manifestReader = new ManifestReader();
42
        $this->builder        = new Builder();
43
    }
44
45
    /**
46
     * @return array
47
     * @throws Exception
48
     */
49
    private function buildAssets(): array
50
    {
51
        $vasriManifest = [];
52
        if ($this->mixManifest === true) {
53
            $manifest = $this->manifestReader->getMixManifest();
54
            foreach ($manifest as $key => $val) {
55
                $vasriManifest[] = $key;
56
            }
57
58
        } elseif ( ! empty(config('vasri.assets'))) {
59
60
            $vasriManifest = config('vasri.assets');
61
62
        } else {
63
64
            throw new Exception('No manifest or assets found');
65
66
        }
67
68
        return $vasriManifest;
69
    }
70
71
    /**
72
     * @return array
73
     * @throws Exception
74
     */
75
    private function buildManifest(): array
76
    {
77
        $manifest = [];
78
        foreach ($this->buildAssets() as $asset) {
79
            $manifest[$asset] = [
80
                'sri'     => $this->builder->sri($asset),
81
                'version' => $this->builder->versioning($asset)
82
            ];
83
        }
84
85
        return $manifest;
86
    }
87
88
    /**
89
     * @throws Exception
90
     */
91
    public function deployManifest(): void
92
    {
93
        file_put_contents(base_path('vasri-manifest.json'),
94
            stripslashes(json_encode($this->buildManifest(), JSON_PRETTY_PRINT)));
95
    }
96
97
}
98