Passed
Push — master ( 588cba...7cce6c )
by Florian
03:54
created

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