Completed
Push — master ( 42ff79...6b7c56 )
by Sergi Tur
02:23
created

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