Passed
Pull Request — master (#679)
by Diego
06:03
created

AssetsResource::exists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Console\PackageResources;
4
5
use JeroenNoten\LaravelAdminLte\Console\PackageResources\PackageResource;
6
use JeroenNoten\LaravelAdminLte\Helpers\CommandHelper;
7
8
class AssetsResource extends PackageResource
9
{
10
    /**
11
     * Create a new resource instance.
12
     *
13
     * @return void
14
     */
15 15
    public function __construct()
16
    {
17
        // Fill the resource data.
18
19 15
        $this->description = 'The AdminLTE required assets';
20 15
        $this->target = public_path('vendor');
21 15
        $this->required = true;
22
23
        // Define the array of required assets (source).
24
25 15
        $this->source = [
26 15
            'adminlte' => [
27 15
                'name' => 'AdminLTE v3',
28 15
                'source' => base_path('vendor/almasaeed2010/adminlte/'),
29 15
                'target' => public_path('vendor/adminlte/'),
30
                'resources' => [
31
                    [
32
                        'source' => 'dist/css',
33
                        'recursive' => false,
34
                    ],
35
                    [
36
                        'source' => 'dist/js',
37
                        'recursive' => false,
38
                        'ignore' => [
39
                            'demo.js',
40
                        ],
41
                    ],
42
                    [
43
                        'source' => 'dist/img/AdminLTELogo.png',
44
                    ],
45
                ],
46
            ],
47
            'fontawesome' => [
48 15
                'name' => 'FontAwesome 5 Free',
49 15
                'source' => base_path('vendor/almasaeed2010/adminlte/plugins/fontawesome-free'),
50 15
                'target' => public_path('vendor/fontawesome-free'),
51
            ],
52
            'bootstrap' => [
53 15
                'name' => 'Bootstrap 4 (js files only)',
54 15
                'source' => base_path('vendor/almasaeed2010/adminlte/plugins/bootstrap'),
55 15
                'target' => public_path('vendor/bootstrap'),
56
            ],
57
            'popper' => [
58 15
                'name' => 'Popper.js (Bootstrap 4 requirement)',
59 15
                'source' => base_path('vendor/almasaeed2010/adminlte/plugins/popper'),
60 15
                'target' => public_path('vendor/popper'),
61
            ],
62
            'jquery' => [
63 15
                'name' => 'jQuery (Bootstrap 4 requirement)',
64 15
                'source' => base_path('vendor/almasaeed2010/adminlte/plugins/jquery'),
65 15
                'target' => public_path('vendor/jquery'),
66
                'ignore' => [
67
                    'core.js',
68
                    'jquery.slim.js',
69
                    'jquery.slim.min.js',
70
                    'jquery.slim.min.map',
71
                ],
72
            ],
73
            'overlay' => [
74 15
                'name' => 'Overlay Scrollbars',
75 15
                'source' => base_path('vendor/almasaeed2010/adminlte/plugins/overlayScrollbars'),
76 15
                'target' => public_path('vendor/overlayScrollbars'),
77
            ],
78
        ];
79
80
        // Fill the set of installation messages.
81
82 15
        $this->messages = [
83
            'install'   => 'Install the basic package assets?',
84
            'overwrite' => 'The basic assets already exists. Want to replace the assets?',
85
            'success'   => 'Basic assets installed successfully.',
86
        ];
87 15
    }
88
89
    /**
90
     * Install/Export the resource.
91
     *
92
     * @return void
93
     */
94 10
    public function install()
95
    {
96
        // Install the AdminLTE basic assets.
97
98 10
        foreach ($this->source as $asset) {
99 10
            $this->installAsset($asset);
100
        }
101 10
    }
102
103
    /**
104
     * Uninstall/Remove the resource.
105
     *
106
     * @return void
107
     */
108 10
    public function uninstall()
109
    {
110
        // Uninstall the AdminLTE basic assets.
111
112 10
        foreach ($this->source as $asset) {
113 10
            $this->uninstallAsset($asset);
114
        }
115 10
    }
116
117
    /**
118
     * Check if the resource already exists on the target destination.
119
     *
120
     * @return bool
121
     */
122 9
    public function exists()
123
    {
124 9
        foreach ($this->source as $asset) {
125 9
            if ($this->assetExists($asset)) {
126 1
                return true;
127
            }
128
        }
129
130 8
        return false;
131
    }
132
133
    /**
134
     * Check if the resource is correctly installed.
135
     *
136
     * @return bool
137
     */
138 11
    public function installed()
139
    {
140 11
        foreach ($this->source as $asset) {
141 11
            if (! $this->assetInstalled($asset)) {
142 6
                return false;
143
            }
144
        }
145
146 10
        return true;
147
    }
148
149
    /**
150
     * Install the specified AdminLTE asset.
151
     *
152
     * @param array $asset An array with the asset data
153
     * @return void
154
     */
155 10
    protected function installAsset($asset)
156
    {
157
        // Check if we just need to export the entire asset.
158
159 10
        if (! isset($asset['resources'])) {
160 10
            $this->exportResource($asset);
161
162 10
            return;
163
        }
164
165
        // Otherwise, export only the specified asset resources.
166
167 10
        foreach ($asset['resources'] as $res) {
168 10
            $res['target'] = $res['target'] ?? $res['source'];
169 10
            $res['target'] = $asset['target'].$res['target'];
170 10
            $res['source'] = $asset['source'].$res['source'];
171 10
            $this->exportResource($res);
172
        }
173 10
    }
174
175
    /**
176
     * Exports the specified resource (usually a file or folder).
177
     *
178
     * @param array $res An array with the resource data
179
     * @return void
180
     */
181 10
    protected function exportResource($res)
182
    {
183
        // Check the resource type in order to copy it.
184
185 10
        if (is_dir($res['source'])) {
186 10
            CommandHelper::copyDirectory(
187 10
                $res['source'],
188 10
                $res['target'],
189 10
                $res['force'] ?? true,
190 10
                $res['recursive'] ?? true,
191 10
                $res['ignore'] ?? []
192
            );
193 10
        } elseif (is_file($res['source'])) {
194 10
            CommandHelper::ensureDirectoryExists(dirname($res['target']));
195 10
            copy($res['source'], $res['target']);
196
        }
197 10
    }
198
199
    /**
200
     * Check if the specified asset already exists on the target destination.
201
     *
202
     * @param array $asset An array with the asset data
203
     * @return bool
204
     */
205 9
    protected function assetExists($asset)
206
    {
207 9
        return is_dir($asset['target']);
208
    }
209
210
    /**
211
     * Check if the specified asset is correctly installed.
212
     *
213
     * @param array $asset An array with the asset data
214
     * @return bool
215
     */
216 11
    protected function assetInstalled($asset)
217
    {
218
        // Check if the asset have resources.
219
220 11
        if (! isset($asset['resources'])) {
221 10
            return $this->resourceInstalled($asset);
222
        }
223
224 11
        foreach ($asset['resources'] as $res) {
225 11
            $res['target'] = $res['target'] ?? $res['source'];
226 11
            $res['target'] = $asset['target'].$res['target'];
227 11
            $res['source'] = $asset['source'].$res['source'];
228
229 11
            if (! $this->resourceInstalled($res)) {
230 6
                return false;
231
            }
232
        }
233
234 10
        return true;
235
    }
236
237
    /**
238
     * Check if the specified resource is correctly installed.
239
     *
240
     * @param array $res An array with the resource data
241
     * @return bool
242
     */
243 11
    protected function resourceInstalled($res)
244
    {
245 11
        $installed = false;
246
247 11
        if (is_dir($res['source'])) {
248 11
            $installed = (bool) CommandHelper::compareDirectories(
249 11
                $res['source'],
250 11
                $res['target'],
251 11
                $res['recursive'] ?? true,
252 11
                $res['ignore'] ?? []
253
            );
254 10
        } elseif (is_file($res['source'])) {
255 10
            $installed = CommandHelper::compareFiles(
256 10
                $res['source'],
257 10
                $res['target']
258
            );
259
        }
260
261 11
        return $installed;
262
    }
263
264
    /**
265
     * Uninstall or remove the specified asset.
266
     *
267
     * @param array $asset An array with the asset data
268
     * @return void
269
     */
270 10
    protected function uninstallAsset($asset)
271
    {
272 10
        $target = $asset['target'];
273
274
        // Uninstall the asset (actually, the target should be a folder).
275
276 10
        if (is_dir($target)) {
277 10
            CommandHelper::removeDirectory($target);
278
        }
279 10
    }
280
}
281