Passed
Push — master ( 3fd782...cac45a )
by Diego
04:58
created

AdminlteAssetsResource::assetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 24
    public function __construct()
16
    {
17
        // Fill the resource data.
18
19 24
        $this->description = 'The set of files required to use the AdminLTE template';
20 24
        $this->target = public_path('vendor');
21 24
        $this->required = true;
22
23
        // Define the array of required assets (source).
24
25 24
        $adminltePath = base_path('vendor/almasaeed2010/adminlte/');
26
27 24
        $this->source = [
28 24
            'adminlte' => [
29 24
                'name' => 'AdminLTE v3',
30 24
                'source' => $adminltePath,
31 24
                'target' => public_path('vendor/adminlte/'),
32 24
                'resources' => [
33 24
                    [
34 24
                        'source' => 'dist/css',
35 24
                        'recursive' => false,
36 24
                    ],
37 24
                    [
38 24
                        'source' => 'dist/js',
39 24
                        'recursive' => false,
40 24
                        'ignore' => [
41 24
                            'demo.js',
42 24
                        ],
43 24
                    ],
44 24
                    [
45 24
                        'source' => 'dist/img/AdminLTELogo.png',
46 24
                    ],
47 24
                ],
48 24
            ],
49 24
            'fontawesome' => [
50 24
                'name' => 'FontAwesome 5 Free',
51 24
                'source' => $adminltePath.'/plugins/fontawesome-free',
52 24
                'target' => public_path('vendor/fontawesome-free'),
53 24
            ],
54 24
            'bootstrap' => [
55 24
                'name' => 'Bootstrap 4 (only JS files)',
56 24
                'source' => $adminltePath.'/plugins/bootstrap',
57 24
                'target' => public_path('vendor/bootstrap'),
58 24
            ],
59 24
            'popper' => [
60 24
                'name' => 'Popper.js (Bootstrap 4 requirement)',
61 24
                'source' => $adminltePath.'/plugins/popper',
62 24
                'target' => public_path('vendor/popper'),
63 24
            ],
64 24
            'jquery' => [
65 24
                'name' => 'jQuery (Bootstrap 4 requirement)',
66 24
                'source' => $adminltePath.'/plugins/jquery',
67 24
                'target' => public_path('vendor/jquery'),
68 24
                'ignore' => [
69 24
                    'core.js',
70 24
                    'jquery.slim.js',
71 24
                    'jquery.slim.min.js',
72 24
                    'jquery.slim.min.map',
73 24
                ],
74 24
            ],
75 24
            'overlay' => [
76 24
                'name' => 'Overlay Scrollbars',
77 24
                'source' => $adminltePath.'/plugins/overlayScrollbars',
78 24
                'target' => public_path('vendor/overlayScrollbars'),
79 24
            ],
80 24
        ];
81
82
        // Fill the set of installation messages.
83
84 24
        $this->messages = [
85 24
            'install' => 'Do you want to publish the AdminLTE asset files?',
86 24
            'overwrite' => 'AdminLTE asset files were already published. Want to replace?',
87 24
            'success' => 'AdminLTE assets files published successfully',
88 24
        ];
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
            $this->publishResource($asset);
164
165 11
            return;
166
        }
167
168
        // Otherwise, publish only the specified asset resources.
169
170 11
        foreach ($asset['resources'] as $res) {
171 11
            $res['target'] = $res['target'] ?? $res['source'];
172 11
            $res['target'] = $asset['target'].$res['target'];
173 11
            $res['source'] = $asset['source'].$res['source'];
174 11
            $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 11
    protected function publishResource($res)
185
    {
186
        // Check whether the resource is a file or a directory.
187
188 11
        if (File::isDirectory($res['source'])) {
189 11
            CommandHelper::copyDirectory(
190 11
                $res['source'],
191 11
                $res['target'],
192 11
                $res['force'] ?? true,
193 11
                $res['recursive'] ?? true,
194 11
                $res['ignore'] ?? []
195 11
            );
196
        } else {
197 11
            File::ensureDirectoryExists(File::dirname($res['target']));
198 11
            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 9
    protected function assetExists($asset)
209
    {
210 9
        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 12
    protected function assetInstalled($asset)
220
    {
221
        // Check whether the asset has resources or not.
222
223 12
        if (! isset($asset['resources'])) {
224 10
            return $this->resourceInstalled($asset);
225
        }
226
227 12
        foreach ($asset['resources'] as $res) {
228 12
            $res['target'] = $res['target'] ?? $res['source'];
229 12
            $res['target'] = $asset['target'].$res['target'];
230 12
            $res['source'] = $asset['source'].$res['source'];
231
232 12
            if (! $this->resourceInstalled($res)) {
233 7
                return false;
234
            }
235
        }
236
237 10
        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 12
    protected function resourceInstalled($res)
247
    {
248
        // Check whether the resource is a file or a directory.
249
250 12
        if (File::isDirectory($res['source'])) {
251 12
            return (bool) CommandHelper::compareDirectories(
252 12
                $res['source'],
253 12
                $res['target'],
254 12
                $res['recursive'] ?? true,
255 12
                $res['ignore'] ?? []
256 12
            );
257
        }
258
259 10
        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 11
    protected function uninstallAsset($asset)
269
    {
270 11
        $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 11
        if (File::isDirectory($target)) {
277 11
            File::deleteDirectory($target);
278
        }
279
    }
280
}
281