BaseCommand   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 0
loc 118
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 2
A dep() 0 17 2
A getDeployFile() 0 17 4
A getConfigFile() 0 14 2
A getCustomDeployFile() 0 7 4
A process() 0 11 1
A isTtySupported() 0 12 2
1
<?php
2
3
namespace Reallyli\LaravelDeployer\Commands;
4
5
use Illuminate\Console\Command;
6
use Reallyli\LaravelDeployer\Concerns\DeployBuilder;
7
use Reallyli\LaravelDeployer\Concerns\ParsesCliParameters;
8
use Reallyli\LaravelDeployer\ConfigFile;
9
use Reallyli\LaravelDeployer\LaravelDeployerException;
10
use Symfony\Component\Process\Process;
11
use Symfony\Component\Yaml\Yaml;
12
13
class BaseCommand extends Command
14
{
15
    use ParsesCliParameters;
16
    use DeployBuilder;
17
18
    protected $parameters;
19
    protected $providedFile;
20
    protected $providedStrategy;
21
    protected $useDeployerOptions = true;
22
23
    public function __construct()
24
    {
25
        $deployerOptions = '
26
            {--s|strategy= : Default deployement strategy}
27
            {--p|parallel : Run tasks in parallel}
28
            {--l|limit= : How many host to run in parallel?}
29
            {--no-hooks : Run task without after/before hooks}
30
            {--log= : Log to file}
31
            {--roles= : Roles to deploy}
32
            {--hosts= : Host to deploy, comma separated, supports ranges [:]}
33
            {--o|option=* : Sets configuration option}
34
            {--f|file= : Specify Deployer file}
35
            {--tag= : Tag to deploy}
36
            {--revision= : Revision to deploy}
37
            {--branch= : Branch to deploy}
38
        ';
39
40
        if ($this->useDeployerOptions) {
41
            $this->signature .= $deployerOptions;
42
        }
43
44
        parent::__construct();
45
    }
46
47
    public function dep($command)
48
    {
49
        $this->parameters = $this->getParameters();
50
        $this->providedFile = $this->parameters->pull('--file');
51
        $this->providedStrategy = $this->parameters->pull('--strategy');
52
53
        if (! $deployFile = $this->getDeployFile()) {
54
            $this->error('config/deploy.php file not found.');
55
            $this->error('Please run `php artisan deploy:init` to get started.');
56
57
            return;
58
        }
59
60
        $parameters = $this->getParametersAsString($this->parameters);
61
        $depBinary = 'vendor'.DIRECTORY_SEPARATOR.'bin'.DIRECTORY_SEPARATOR.'dep';
62
        $this->process("$depBinary --file=$deployFile $command $parameters");
63
    }
64
65
    public function getDeployFile()
66
    {
67
        if ($this->providedFile) {
68
            return $this->providedFile;
69
        }
70
71
        if ($customDeployFile = $this->getCustomDeployFile()) {
72
            return $customDeployFile;
73
        }
74
75
        if ($configFile = $this->getConfigFile()) {
76
            return $configFile
77
                ->toDeployFile()
78
                ->updateStrategy($this->providedStrategy)
79
                ->store();
80
        }
81
    }
82
83
    public function getConfigFile()
84
    {
85
        $filepath = $this->getConfigFullPath();
86
        throw_unless(file_exists($filepath), LaravelDeployerException::class, 'deploy.yml file not found!');
87
88
        try {
89
            $deployConfig = Yaml::parseFile($filepath);
90
        } catch (LaravelDeployerException $e) {
91
            throw $e;
92
        }
93
        throw_unless($deployConfig, LaravelDeployerException::class, 'deploy.yml is empty!');
94
95
        return new ConfigFile($deployConfig);
96
    }
97
98
    public function getCustomDeployFile()
99
    {
100
        $custom = $this->getConfigFile()->get('custom_deployer_file');
101
        if (is_string($custom) && $custom) {
102
            return file_exists(base_path($custom)) ? base_path($custom) : null;
103
        }
104
    }
105
106
    public function process($command)
107
    {
108
        $process = (new Process($command))
0 ignored issues
show
Unused Code introduced by
$process is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
109
            ->setTty($this->isTtySupported())
110
            ->setWorkingDirectory(base_path())
111
            ->setTimeout(null)
112
            ->setIdleTimeout(null)
113
            ->mustRun(function ($type, $buffer) {
114
                $this->output->write($buffer);
115
            });
116
    }
117
118
    public function isTtySupported()
119
    {
120
        if (env('APP_ENV') === 'testing') {
121
            return false;
122
        }
123
124
        return (bool) @proc_open('echo 1 >/dev/null', [
125
            ['file', '/dev/tty', 'r'],
126
            ['file', '/dev/tty', 'w'],
127
            ['file', '/dev/tty', 'w'],
128
        ], $pipes);
129
    }
130
}
131