Passed
Pull Request — master (#1285)
by Diego
03:35
created

AdminlteAssetsResource::installAsset()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 3
rs 10
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 void
95
     */
96 11
    public function install()
97
    {
98
        // Install the AdminLTE asset files.
99
100 11
        foreach ($this->source as $asset) {
101 11
            $this->installAsset($asset);
102
        }
103
    }
104
105
    /**
106
     * Uninstalls the resource.
107
     *
108
     * @return void
109
     */
110 11
    public function uninstall()
111
    {
112
        // Uninstall the AdminLTE asset files.
113
114 11
        foreach ($this->source as $asset) {
115 11
            $this->uninstallAsset($asset);
116
        }
117
    }
118
119
    /**
120
     * Checks whether the resource already exists in the target location.
121
     *
122
     * @return bool
123
     */
124 9
    public function exists()
125
    {
126 9
        foreach ($this->source as $asset) {
127 9
            if ($this->assetExists($asset)) {
128 1
                return true;
129
            }
130
        }
131
132 8
        return false;
133
    }
134
135
    /**
136
     * Checks whether the resource is correctly installed, i.e. if the source
137
     * items matches with the items available at the target location.
138
     *
139
     * @return bool
140
     */
141 12
    public function installed()
142
    {
143 12
        foreach ($this->source as $asset) {
144 12
            if (! $this->assetInstalled($asset)) {
145 7
                return false;
146
            }
147
        }
148
149 10
        return true;
150
    }
151
152
    /**
153
     * Installs the specified AdminLTE asset.
154
     *
155
     * @param  array  $asset  An array with the asset data
156
     * @return void
157
     */
158 11
    protected function installAsset($asset)
159
    {
160
        // Check if we just need to publish the entire asset.
161
162 11
        if (! isset($asset['resources'])) {
163 11
            return $this->publishResource($asset);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->publishResource($asset) targeting JeroenNoten\LaravelAdmin...urce::publishResource() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
164
        }
165
166
        // Otherwise, publish only the specified asset resources.
167
168 11
        foreach ($asset['resources'] as $res) {
169 11
            $res['target'] = $res['target'] ?? $res['source'];
170 11
            $res['target'] = $asset['target'].$res['target'];
171 11
            $res['source'] = $asset['source'].$res['source'];
172 11
            $this->publishResource($res);
173
        }
174
    }
175
176
    /**
177
     * Publishes the specified resource (usually a file or folder).
178
     *
179
     * @param  array  $res  An array with the resource data
180
     * @return void
181
     */
182 11
    protected function publishResource($res)
183
    {
184
        // Check whether the resource is a file or a directory.
185
186 11
        if (File::isDirectory($res['source'])) {
187 11
            CommandHelper::copyDirectory(
188 11
                $res['source'],
189 11
                $res['target'],
190 11
                $res['force'] ?? true,
191 11
                $res['recursive'] ?? true,
192 11
                $res['ignore'] ?? []
193 11
            );
194
        } else {
195 11
            File::ensureDirectoryExists(File::dirname($res['target']));
196 11
            File::copy($res['source'], $res['target']);
197
        }
198
    }
199
200
    /**
201
     * Checks whether the specified asset already exists in the target location.
202
     *
203
     * @param  array  $asset  An array with the asset data
204
     * @return bool
205
     */
206 9
    protected function assetExists($asset)
207
    {
208 9
        return File::exists($asset['target']);
209
    }
210
211
    /**
212
     * Checks whether the specified asset is correctly installed.
213
     *
214
     * @param  array  $asset  An array with the asset data
215
     * @return bool
216
     */
217 12
    protected function assetInstalled($asset)
218
    {
219
        // Check whether the asset has resources or not.
220
221 12
        if (! isset($asset['resources'])) {
222 10
            return $this->resourceInstalled($asset);
223
        }
224
225 12
        foreach ($asset['resources'] as $res) {
226 12
            $res['target'] = $res['target'] ?? $res['source'];
227 12
            $res['target'] = $asset['target'].$res['target'];
228 12
            $res['source'] = $asset['source'].$res['source'];
229
230 12
            if (! $this->resourceInstalled($res)) {
231 7
                return false;
232
            }
233
        }
234
235 10
        return true;
236
    }
237
238
    /**
239
     * Checks whether the specified resource is correctly installed.
240
     *
241
     * @param  array  $res  An array with the resource data
242
     * @return bool
243
     */
244 12
    protected function resourceInstalled($res)
245
    {
246
        // Check whether the resource is a file or a directory.
247
248 12
        if (File::isDirectory($res['source'])) {
249 12
            return (bool) CommandHelper::compareDirectories(
250 12
                $res['source'],
251 12
                $res['target'],
252 12
                $res['recursive'] ?? true,
253 12
                $res['ignore'] ?? []
254 12
            );
255
        }
256
257 10
        return CommandHelper::compareFiles($res['source'], $res['target']);
258
    }
259
260
    /**
261
     * Uninstalls the specified asset.
262
     *
263
     * @param  array  $asset  An array with the asset data
264
     * @return void
265
     */
266 11
    protected function uninstallAsset($asset)
267
    {
268 11
        $target = $asset['target'];
269
270
        // Uninstall the specified asset. Note the asset target location is
271
        // always a folder. When the target folder does not exists, we consider
272
        // the asset as uninstalled.
273
274 11
        if (File::isDirectory($target)) {
275 11
            File::deleteDirectory($target);
276
        }
277
    }
278
}
279