Passed
Pull Request — master (#1285)
by Diego
06:46
created

AdminlteAssetsResource::install()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

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