Completed
Push — master ( 9d1723...8bd216 )
by Cristian
02:08
created

Install::echo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 11
rs 9.4285
1
<?php
2
3
namespace Backpack\Base\app\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Process\Exception\ProcessFailedException;
7
use Symfony\Component\Process\Process;
8
9
class Install extends Command
10
{
11
    protected $progressBar;
12
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'backpack:base:install
19
                                {--timeout=300} : How many seconds to allow each process to run.
20
                                {--debug} : Show process output or not. Useful for debugging.';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Require dev packages and publish files for Backpack\Base to work';
28
29
    /**
30
     * Create a new command instance.
31
     *
32
     * @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...
33
     */
34
    public function __construct()
35
    {
36
        parent::__construct();
37
    }
38
39
    /**
40
     * Execute the console command.
41
     *
42
     * @return mixed
43
     */
44
    public function handle()
45
    {
46
        $this->progressBar = $this->output->createProgressBar(6);
47
        $this->progressBar->start();
48
        $this->info(" Backpack\Base installation started. Please wait...");
49
        $this->progressBar->advance();
50
51
        $this->line(" Installing backpack/generators");
52
        $this->executeProcess('composer require backpack/generators --dev');
53
54
        $this->line(" Installing laracasts/generators");
55
        $this->executeProcess('composer require laracasts/generators:dev-master --dev');
56
57
        $this->line(" Publishing configs, langs, views and AdminLTE files");
58
        $this->executeProcess('php artisan vendor:publish --provider="Backpack\Base\BaseServiceProvider"');
59
60
        $this->line(" Publishing config for notifications - prologue/alerts");
61
        $this->executeProcess('php artisan vendor:publish --provider="Prologue\Alerts\AlertsServiceProvider"');
62
63
        $this->line(" Generating users table (using Laravel's default migrations)");
64
        $this->executeProcess('php artisan migrate');
65
66
        $this->progressBar->finish();
67
        $this->info(" Backpack\Base installation finished.");
68
    }
69
70
    /**
71
     * Run a SSH command.
72
     *
73
     * @param string $command      The SSH command that needs to be run
74
     * @param bool   $beforeNotice Information for the user before the command is run
75
     * @param bool   $afterNotice  Information for the user after the command is run
76
     *
77
     * @return mixed Command-line output
78
     */
79
    public function executeProcess($command, $beforeNotice = false, $afterNotice = false)
80
    {
81
        $this->echo('info', $beforeNotice?' '.$beforeNotice:$command);
82
83
        $process = new Process($command, null, null, null, $this->option('timeout'), null);
0 ignored issues
show
Documentation introduced by
$this->option('timeout') is of type string|array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
        $process->run(function ($type, $buffer) {
85
            if (Process::ERR === $type) {
86
                $this->echo('comment', $buffer);
87
            } else {
88
                $this->echo('line', $buffer);
89
            }
90
        });
91
92
        // executes after the command finishes
93
        if (!$process->isSuccessful()) {
94
            throw new ProcessFailedException($process);
95
        }
96
97
        if ($this->progressBar) {
98
            $this->progressBar->advance();
99
        }
100
101
        if ($afterNotice) {
102
            $this->echo('info', $afterNotice);
103
        }
104
    }
105
106
    /**
107
     * Write text to the screen for the user to see.
108
     *
109
     * @param  [string] $type    line, info, comment, question, error
0 ignored issues
show
Documentation introduced by
The doc-type [string] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
110
     * @param  [string] $content
0 ignored issues
show
Documentation introduced by
The doc-type [string] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
111
     */
112
    public function echo($type, $content)
113
    {
114
        if ($this->option('debug') == false) {
115
            return;
116
        }
117
118
        // skip empty lines
119
        if (trim($content)) {
120
            $this->{$type}($content);
121
        }
122
    }
123
}
124