Completed
Push — master ( becb2b...3f1ad0 )
by Sergi Tur
07:46
created

PublishAdminLTE::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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