Completed
Push — master ( 66beca...4d3c05 )
by wen
03:46
created

InstallCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 9
c 2
b 0
f 2
lcom 1
cbo 4
dl 0
loc 103
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 14 1
A publish() 0 9 1
A migrate() 0 16 1
A npm() 0 13 2
A executeCommand() 0 15 3
1
<?php
2
3
namespace Sco\Admin\Console;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Process\Exception\ProcessFailedException;
7
use Symfony\Component\Process\ExecutableFinder;
8
use Symfony\Component\Process\Process;
9
10
class InstallCommand extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'admin:install';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Install Sco Admin Package(publish/migrate).';
25
26
    /**
27
     * Create a new command instance.
28
     *
29
     * @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...
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return mixed
40
     */
41
    public function handle()
42
    {
43
        $this->line('');
44
        $this->line('************************');
45
        $this->line('  Welcome to Sco Admin  ');
46
        $this->line('************************');
47
48
        $this->publish();
49
        $this->migrate();
50
        $this->npm();
51
52
53
        $this->info('Successfully Installed Sco Admin!');
54
    }
55
56
    protected function publish()
57
    {
58
        $this->line('Publish Resources');
59
        $this->line('');
60
        $this->call('vendor:publish', [
61
            '--provider' => 'Sco\Admin\Providers\AdminServiceProvider',
62
            '--force'    => true,
63
        ]);
64
    }
65
66
    protected function migrate()
67
    {
68
        $this->line('Running Database Migrations');
69
        $this->line('');
70
        $this->call('migrate', ['--force' => true]);
71
        $this->line('');
72
73
        $this->line('Seeding database');
74
        $this->line('');
75
        $this->call('db:seed', [
76
            '--force' => true,
77
            '--class' => \AdminTableSeeder::class,
78
        ]);
79
        $this->line('');
80
81
    }
82
83
    protected function npm()
84
    {
85
        $finder = new ExecutableFinder();
86
        $result = $finder->find('npm');
87
        if ($result === null) {
88
            $this->error('Not Found Npm Command');
89
            $this->error('You Must install Node.js And execute Command "npm run production"');
90
        } else {
91
92
            $this->executeCommand("npm install");
93
            $this->executeCommand("npm run production");
94
        }
95
    }
96
97
    private function executeCommand($command)
98
    {
99
        $process = new Process($command);
100
        try {
101
            $process->mustRun(function ($type, $buffer) {
102
                if (Process::ERR === $type) {
103
                    $this->error($buffer);
104
                } else {
105
                    $this->line($buffer);
106
                }
107
            });
108
        } catch (ProcessFailedException $exception) {
109
            $this->error($exception->getMessage());
110
        }
111
    }
112
}
113