AdminlteAssetsResource::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 73
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 63
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 46
nc 1
nop 0
dl 0
loc 73
ccs 63
cts 63
cp 1
crap 1
rs 9.1781
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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