PublishAutodeploy::handle()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 0
dl 0
loc 29
rs 8.8571
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\DiesIfEnvVariableIsnotInstalled;
7
use GuzzleHttp\Client;
8
use Illuminate\Console\Command;
9
10
/**
11
 * Class PublishAutodeploy.
12
 *
13
 * @package Acacha\ForgePublish\Commands
14
 */
15
class PublishAutodeploy extends Command
16
{
17
    use ChecksEnv, DiesIfEnvVariableIsnotInstalled;
18
19
    /**
20
     * Server name
21
     *
22
     * @var String
23
     */
24
    protected $server;
25
26
    /**
27
     * Forge site id.
28
     *
29
     * @var String
30
     */
31
    protected $site;
32
33
    /**
34
     * The name and signature of the console command.
35
     *
36
     * @var string
37
     */
38
    protected $signature = 'publish:autodeploy {--server=} {--site=} {--disable}';
39
40
    /**
41
     * The console command description.
42
     *
43
     * @var string
44
     */
45
    protected $description = 'Enable Laravel Forge autodeploy';
46
47
    /**
48
     * Guzzle http client.
49
     *
50
     * @var Client
51
     */
52
    protected $http;
53
54
    /**
55
     * Create a new command instance.
56
     *
57
     */
58
    public function __construct(Client $http)
59
    {
60
        parent::__construct();
61
        $this->http = $http;
62
    }
63
64
    /**
65
     * Execute the console command.
66
     *
67
     */
68
    public function handle()
69
    {
70
        $this->abortCommandExecution();
71
        $http_method = 'post';
72
        $action = 'Enabling';
73
        $actionp = 'Enabled';
74
        if ($this->option('disable')) {
75
            $action = 'Disabling';
76
            $http_method = 'delete';
77
            $actionp = 'Disabled';
78
        }
79
80
        $this->info("$action autodeploy on Laravel Forge Site...");
81
82
        $uri = str_replace('{forgeserver}', $this->server, config('forge-publish.post_auto_deploy_uri'));
83
        $uri = str_replace('{forgesite}', $this->site, $uri);
84
        $url = config('forge-publish.url') . $uri;
85
86
        $this->http->$http_method($url,
87
            [
88
                'headers' => [
89
                    'X-Requested-With' => 'XMLHttpRequest',
90
                    'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN')
91
                ]
92
            ]
93
        );
94
95
        $this->info("Autodeploy on Laravel Forge Site $actionp!");
96
    }
97
98
    /**
99
     * Abort command execution?
100
     */
101
    protected function abortCommandExecution()
102
    {
103
        $this->server = $this->checkEnv('server', 'ACACHA_FORGE_SERVER');
104
        $this->site = $this->checkEnv('site', 'ACACHA_FORGE_SITE');
105
        $this->dieIfEnvVarIsNotInstalled('ACACHA_FORGE_ACCESS_TOKEN');
106
    }
107
}
108