PublishKeyGenerate::keyIsAlreadyInstalled()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Acacha\ForgePublish\Commands;
4
5
use Acacha\ForgePublish\Commands\Traits\ChecksEnv;
6
use Acacha\ForgePublish\Commands\Traits\ChecksSSHConnection;
7
use Acacha\ForgePublish\Commands\Traits\RunsSSHCommands;
8
use GuzzleHttp\Client;
9
use Illuminate\Console\Command;
10
11
/**
12
 * Class PublishKeyGenerate.
13
 *
14
 * @package Acacha\ForgePublish\Commands
15
 */
16
class PublishKeyGenerate extends Command
17
{
18
    use ChecksEnv, RunsSSHCommands;
19
20
    /**
21
     * Server name
22
     *
23
     * @var String
24
     */
25
    protected $server;
26
27
    /**
28
     * Domain
29
     *
30
     * @var String
31
     */
32
    protected $domain;
33
34
    /**
35
     * The name and signature of the console command.
36
     *
37
     * @var string
38
     */
39
    protected $signature = 'publish:key_generate {--server=} {--domain=}';
40
41
    /**
42
     * The console command description.
43
     *
44
     * @var string
45
     */
46
    protected $description = 'Run artisan key:generate command on production server';
47
48
    /**
49
     * Guzzle http client.
50
     *
51
     * @var Client
52
     */
53
    protected $http;
54
55
    /**
56
     * Create a new command instance.
57
     *
58
     */
59
    public function __construct(Client $http)
60
    {
61
        parent::__construct();
62
        $this->http = $http;
63
    }
64
65
    /**
66
     * Execute the console command.
67
     *
68
     */
69
    public function handle()
70
    {
71
        $this->abortCommandExecution();
72
73
        if ($this->keyIsAlreadyInstalled()) {
74
            $this->info('Key is already installed on production. Skipping...');
75
            return;
76
        }
77
78
        $this->call('publish:artisan', [
79
            'artisan_command' => 'key:generate'
80
        ]);
81
    }
82
83
    /**
84
     * Key is already installed on production?
85
     *
86
     * @return bool
87
     */
88
    protected function keyIsAlreadyInstalled()
89
    {
90
        $key = 'APP_KEY=base64:';
91
        $output = $this->execSSH("cd $this->domain;cat .env");
92
        if (str_contains($output, $key)) {
93
            return true;
94
        }
95
        return false;
96
    }
97
98
    /**
99
     * Abort command execution?
100
     */
101
    protected function abortCommandExecution()
102
    {
103
        $this->server = $this->checkEnv('server', 'ACACHA_FORGE_SERVER');
104
        $this->domain = $this->checkEnv('domain', 'ACACHA_FORGE_DOMAIN');
105
106
        $this->abortIfNoSSHConnection();
107
    }
108
}
109