Passed
Push — master ( 26c354...92479f )
by Richard
05:49 queued 10s
created

InstallCommand::handle()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 92
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 60
c 5
b 0
f 0
dl 0
loc 92
rs 8.2505
cc 6
nc 18
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PWWEB\Artomator\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use Symfony\Component\Process\Process;
8
9
class InstallCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'artomator:install {--composer=global : Absolute path to the Composer binary which should be used to install packages}';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Install the Artomator components and resources';
24
25
    /**
26
     * Execute the console command.
27
     *
28
     * @return void
29
     */
30
    public function handle()
31
    {
32
        $this->info('Starting Installation of Artomator.');
33
        $bar = $this->output->createProgressBar(4);
34
        $bar->start();
35
        // Publish...
36
        $this->callSilent(
37
            'vendor:publish',
38
            [
39
                '--tag' => 'artomator',
40
                '--force' => true,
41
            ]
42
        );
43
        $bar->advance();
44
        $this->callSilent(
45
            'vendor:publish',
46
            [
47
                '--provider' => 'InfyOm\Generator\InfyOmGeneratorServiceProvider',
48
                '--force' => true,
49
            ]
50
        );
51
        $bar->advance();
52
        $this->callSilent(
53
            'vendor:publish',
54
            [
55
                '--tag' => 'lighthouse-schema',
56
                '--force' => true,
57
            ]
58
        );
59
        $bar->advance();
60
        $this->callSilent(
61
            'vendor:publish',
62
            [
63
                '--tag' => 'lighthouse-config',
64
                '--force' => true,
65
            ]
66
        );
67
        $bar->finish();
68
        $this->newLine();
69
        $this->info('Config files published. (4/4)');
70
71
        $this->call('artomator:publish');
72
73
        $this->info('Artomator base files published.');
74
75
        $aliases = [
76
            "'View' => Illuminate\Support\Facades\View::class",
77
            "'Form' => Collective\Html\FormFacade::class",
78
            "'Html' => Collective\Html\HtmlFacade::class",
79
            "'Flash' => Laracasts\Flash\Flash::class",
80
        ];
81
82
        $this->replaceInFile(
83
            "'View' => Illuminate\Support\Facades\View::class",
84
            implode(",\n\t\t", $aliases),
85
            config_path('app.php')
86
        );
87
88
        $template = $this->choice(
89
            'Choose your templating package',
90
            ['CoreUI', 'AdminLTE'],
91
            0
92
        );
93
94
        if ('CoreUI' === $template) {
95
            $this->replaceInFile(
96
                "'templates'         => 'adminlte-templates',",
97
                "'templates'         => 'coreui-templates',",
98
                config_path('infyom/laravel_generator.php')
99
            );
100
            $this->requireComposerPackages('infyomlabs/coreui-templates:^8.0.x-dev');
101
        } elseif ('AdminLTE' === $template) {
102
            $this->requireComposerPackages('infyomlabs/adminlte-templates:^8.0.x-dev');
103
        }
104
        $this->newLine();
105
        $this->info('Template package added to composer.');
106
107
        if (true === $this->confirm('Do you want to install Laravel Jetstream (Inertia & Vue)?')) {
108
            $this->requireComposerPackages('laravel/jetstream');
109
            if (true === $this->confirm('Do you want to support Laravel Jetstream Teams?')) {
110
                $this->call('jetstream:install', ['stack' => 'inertia', '--teams']);
111
            } else {
112
                $this->call('jetstream:install', ['stack' => 'inertia']);
113
            }
114
            $this->info('Laravel Jetstream Installed.');
115
        }
116
117
        if (true === $this->confirm('Do you want to publish the stub files?')) {
118
            $this->call('artomator.publish:templates');
119
            $this->info('Stub files published.');
120
        }
121
        $this->info('Thanks for installing Artomator.');
122
    }
123
124
    /**
125
     * Installs the given Composer Packages into the application.
126
     *
127
     * @param mixed $packages Packages to install.
128
     *
129
     * @return void
130
     */
131
    protected function requireComposerPackages($packages)
132
    {
133
        $composer = $this->option('composer');
134
135
        if ('global' !== $composer) {
136
            $command = ['php', $composer, 'require'];
137
        }
138
139
        $command = array_merge(
140
            ($command ?? ['composer', 'require']),
141
            (true === is_array($packages)) ? $packages : func_get_args()
142
        );
143
144
        (new Process($command, base_path(), ['COMPOSER_MEMORY_LIMIT' => '-1']))
145
            ->setTimeout(null)
146
            ->run(
147
                function ($type, $output) {
148
                    $this->output->write($output);
149
                }
150
            );
151
    }
152
153
    /**
154
     * Update the "package.json" file.
155
     *
156
     * @param callable $callback Callback function.
157
     * @param bool     $dev      Dev.
158
     *
159
     * @return void
160
     */
161
    protected static function updateNodePackages(callable $callback, $dev = true)
162
    {
163
        if (false === file_exists(base_path('package.json'))) {
164
            return;
165
        }
166
167
        $configurationKey = (true === $dev) ? 'devDependencies' : 'dependencies';
168
169
        $packages = json_decode(file_get_contents(base_path('package.json')), true);
170
171
        $packages[$configurationKey] = $callback(
172
            (true === array_key_exists($configurationKey, $packages)) ? $packages[$configurationKey] : [],
173
            $configurationKey
174
        );
175
176
        ksort($packages[$configurationKey]);
177
178
        file_put_contents(
179
            base_path('package.json'),
180
            json_encode($packages, (JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)).PHP_EOL
181
        );
182
    }
183
184
    /**
185
     * Delete the "node_modules" directory and remove the associated lock files.
186
     *
187
     * @return void
188
     */
189
    protected static function flushNodeModules()
190
    {
191
        tap(
192
            new Filesystem,
193
            function ($files) {
194
                $files->deleteDirectory(base_path('node_modules'));
195
196
                $files->delete(base_path('yarn.lock'));
197
                $files->delete(base_path('package-lock.json'));
198
            }
199
        );
200
    }
201
202
    /**
203
     * Replace a given string within a given file.
204
     *
205
     * @param string $search  Search term.
206
     * @param string $replace Replace term.
207
     * @param string $path    Path.
208
     *
209
     * @return void
210
     */
211
    protected function replaceInFile($search, $replace, $path)
212
    {
213
        file_put_contents($path, str_replace($search, $replace, file_get_contents($path)));
214
    }
215
}
216