Completed
Push — master ( d2f5b0...68e2ed )
by Sergi Tur
03:33
created

PublishAdminLTE::publishGravatar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Acacha\AdminLTETemplateLaravel\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use League\Flysystem\Adapter\Local as LocalAdapter;
8
use League\Flysystem\Filesystem as Flysystem;
9
use League\Flysystem\MountManager;
10
11
/**
12
 * Class PublishAdminLTE.
13
 */
14
class PublishAdminLTE extends Command
15
{
16
    /**
17
     * The filesystem instance.
18
     *
19
     * @var \Illuminate\Filesystem\Filesystem
20
     */
21
    protected $files;
22
23
    /**
24
     * The name and signature of the console command.
25
     */
26
    protected $signature = 'adminlte-laravel:publish {--f|force : Force overwrite of files}';
27
28
    /**
29
     * The console command description.
30
     *
31
     * @var string
32
     */
33
    protected $description = 'Publish Acacha AdminLTE Template files into laravel project';
34
35
    /**
36
     * Force overwrite of files.
37
     *
38
     * @var bool
39
     */
40
    protected $force = false;
41
42
    /**
43
     * Create a new command instance.
44
     *
45
     * @param \Illuminate\Filesystem\Filesystem $files
46
     *
47
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
48
     */
49
    public function __construct(Filesystem $files)
50
    {
51
        parent::__construct();
52
        $this->files = $files;
53
    }
54
55
    /**
56
     * Execute the console command.
57
     */
58 View Code Duplication
    public function handle()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $this->processOptions();
61
        $this->publishHomeController();
62
        $this->changeRegisterController();
63
        $this->changeLoginController();
64
        $this->changeForgotPasswordController();
65
        $this->changeResetPasswordController();
66
        $this->publishPublicAssets();
67
        $this->publishViews();
68
        $this->publishResourceAssets();
69
        $this->publishTests();
70
        $this->publishLanguages();
71
        $this->publishGravatar();
72
    }
73
74
    /**
75
     * Install Home Controller.
76
     */
77
    private function publishHomeController()
78
    {
79
        $this->install(\Acacha\AdminLTETemplateLaravel\Facades\AdminLTE::homeController());
80
    }
81
82
    /**
83
     * Change Auth Register controller.
84
     */
85
    private function changeRegisterController()
86
    {
87
        $this->install(\Acacha\AdminLTETemplateLaravel\Facades\AdminLTE::registerController());
88
    }
89
90
    /**
91
     * Change Auth Login controller.
92
     */
93
    private function changeLoginController()
94
    {
95
        $this->install(\Acacha\AdminLTETemplateLaravel\Facades\AdminLTE::loginController());
96
    }
97
98
    /**
99
     * Change Auth Forgot Password controller.
100
     */
101
    private function changeForgotPasswordController()
102
    {
103
        $this->install(\Acacha\AdminLTETemplateLaravel\Facades\AdminLTE::forgotPasswordController());
104
    }
105
106
    /**
107
     * Change Auth Reset Password controller.
108
     */
109
    private function changeResetPasswordController()
110
    {
111
        $this->install(\Acacha\AdminLTETemplateLaravel\Facades\AdminLTE::resetPasswordController());
112
    }
113
114
    /**
115
     * Install public assets.
116
     */
117
    private function publishPublicAssets()
118
    {
119
        $this->install(\Acacha\AdminLTETemplateLaravel\Facades\AdminLTE::publicAssets());
120
    }
121
122
    /**
123
     * Install views.
124
     */
125
    private function publishViews()
126
    {
127
        $this->install(\Acacha\AdminLTETemplateLaravel\Facades\AdminLTE::viewsToOverwrite());
128
    }
129
130
    /**
131
     * Install resource assets.
132
     */
133
    private function publishResourceAssets()
134
    {
135
        $this->install(\Acacha\AdminLTETemplateLaravel\Facades\AdminLTE::resourceAssets());
136
    }
137
138
    /**
139
     * Install resource assets.
140
     */
141
    private function publishTests()
142
    {
143
        $this->install(\Acacha\AdminLTETemplateLaravel\Facades\AdminLTE::tests());
144
    }
145
146
    /**
147
     * Install language assets.
148
     */
149
    private function publishLanguages()
150
    {
151
        $this->install(\Acacha\AdminLTETemplateLaravel\Facades\AdminLTE::languages());
152
    }
153
154
    /**
155
     * Install gravatar config file.
156
     */
157
    private function publishGravatar()
158
    {
159
        $this->install(\Acacha\AdminLTETemplateLaravel\Facades\AdminLTE::gravatar());
160
    }
161
162
    /**
163
     * Install files from array.
164
     *
165
     * @param $files
166
     */
167
    private function install($files)
168
    {
169
        foreach ($files as $fileSrc => $fileDst) {
170
            if (file_exists($fileDst) && !$this->force && !$this->confirmOverwrite(basename($fileDst))) {
171
                return;
172
            }
173
            if ($this->files->isFile($fileSrc)) {
174
                $this->publishFile($fileSrc, $fileDst);
175
            } elseif ($this->files->isDirectory($fileSrc)) {
176
                $this->publishDirectory($fileSrc, $fileDst);
177
            } else {
178
                $this->error("Can't locate path: <{$fileSrc}>");
179
            }
180
        }
181
    }
182
183
    /**
184
     * @param $fileName
185
     * @param string $prompt
186
     *
187
     * @return bool
188
     */
189
    protected function confirmOverwrite($fileName, $prompt = '')
190
    {
191
        $prompt = (empty($prompt))
192
            ? $fileName.' already exists. Do you want to overwrite it? [y|N]'
193
            : $prompt;
194
195
        return $this->confirm($prompt, false);
196
    }
197
198
    /**
199
     * Create the directory to house the published files if needed.
200
     *
201
     * @param string $directory
202
     *
203
     * @return void
204
     */
205
    protected function createParentDirectory($directory)
206
    {
207
        if (!$this->files->isDirectory($directory)) {
208
            $this->files->makeDirectory($directory, 0755, true);
209
        }
210
    }
211
212
    /**
213
     * Publish the file to the given path.
214
     *
215
     * @param string $from
216
     * @param string $to
217
     *
218
     * @return void
219
     */
220
    protected function publishFile($from, $to)
221
    {
222
        $this->createParentDirectory(dirname($to));
223
        $this->files->copy($from, $to);
224
        $this->status($from, $to, 'File');
225
    }
226
227
    /**
228
     * Publish the directory to the given directory.
229
     *
230
     * @param string $from
231
     * @param string $to
232
     *
233
     * @return void
234
     */
235
    protected function publishDirectory($from, $to)
236
    {
237
        $manager = new MountManager([
238
            'from' => new Flysystem(new LocalAdapter($from)),
239
            'to'   => new Flysystem(new LocalAdapter($to)),
240
        ]);
241
        foreach ($manager->listContents('from://', true) as $file) {
242
            if ($file['type'] === 'file' && (!$manager->has('to://'.$file['path']))) {
243
                $manager->put('to://'.$file['path'], $manager->read('from://'.$file['path']));
244
            }
245
        }
246
        $this->status($from, $to, 'Directory');
247
    }
248
249
    /**
250
     * Write a status message to the console.
251
     *
252
     * @param string $from
253
     * @param string $to
254
     * @param string $type
255
     *
256
     * @return void
257
     */
258
    protected function status($from, $to, $type)
259
    {
260
        $from = str_replace(base_path(), '', realpath($from));
261
        $to = str_replace(base_path(), '', realpath($to));
262
        $this->line('<info>Copied '.$type.'</info> <comment>['.$from.']</comment> <info>To</info> <comment>['.$to.']</comment>');
263
    }
264
265
    /**
266
     * Process options before running command.
267
     */
268
    private function processOptions()
269
    {
270
        $this->force = $this->option('force');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->option('force') of type string or array is incompatible with the declared type boolean of property $force.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
271
    }
272
}
273