PublishCertificates   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B handle() 0 43 4
A obtainAPIURLEndpoint() 0 6 1
A abortCommandExecution() 0 6 1
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 PublishCertificates.
12
 *
13
 * @package Acacha\ForgePublish\Commands
14
 */
15
class PublishCertificates extends Command
16
{
17
    use ChecksEnv, DiesIfEnvVariableIsnotInstalled;
18
19
    /**
20
    * The name and signature of the console command.
21
    *
22
    * @var string
23
    */
24
    protected $signature = 'publish:certificates {--server=} {--site=} {--dump}';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'List SSL certificates';
32
33
    /**
34
     * API endpoint URL
35
     *
36
     * @var string
37
     */
38
    protected $url;
39
40
    /**
41
     * Server forge id.
42
     *
43
     * @var string
44
     */
45
    protected $server;
46
47
    /**
48
     * Laravel forge site id.
49
     *
50
     * @var string
51
     */
52
    protected $site;
53
54
    /**
55
     * Guzzle http client.
56
     *
57
     * @var Client
58
     */
59
    protected $http;
60
61
    /**
62
     * Create a new command instance.
63
     *
64
     */
65
    public function __construct(Client $http)
66
    {
67
        parent::__construct();
68
        $this->http = $http;
69
    }
70
71
    /**
72
     * Execute the console command.
73
     *
74
     */
75
    public function handle()
76
    {
77
        $this->abortCommandExecution();
78
79
        $this->url = $this->obtainAPIURLEndpoint();
80
        
81
        $response = $this->http->get($this->url, [
82
                'headers' => [
83
                    'X-Requested-With' => 'XMLHttpRequest',
84
                    'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN')
85
                ]
86
            ]
87
        );
88
89
        $certificates = json_decode($response->getBody(), true) ;
90
        if ($this->option('dump')) {
91
            dump($certificates);
92
        }
93
94
        if (empty($certificates)) {
95
            $this->error('No SSL certificates found.');
96
            die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method handle() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
97
        }
98
99
        $headers = ['Id', 'ServerId','SiteId','Domain','Type','Request Status','Existing','Active','Created at'];
100
101
        $rows = [];
102
        foreach ($certificates as $certificate) {
103
            $rows[] = [
104
                $certificate['id'],
105
                $certificate['serverId'],
106
                $certificate['siteId'],
107
                $certificate['domain'],
108
                $certificate['type'],
109
                $certificate['requestStatus'],
110
                $certificate['createdAt'],
111
                $certificate['existing'],
112
                $certificate['active']
113
            ];
114
        }
115
116
        $this->table($headers, $rows);
117
    }
118
119
    /**
120
     * Obtain API URL endpoint.
121
     *
122
     * @return string
123
     */
124
    protected function obtainAPIURLEndpoint()
125
    {
126
        $uri = str_replace('{forgeserver}', $this->server, config('forge-publish.get_certificates_uri'));
127
        $uri = str_replace('{forgesite}', $this->site, $uri);
128
        return config('forge-publish.url') . $uri;
129
    }
130
131
    /**
132
     * Abort command execution?
133
     */
134
    protected function abortCommandExecution()
135
    {
136
        $this->server = $this->checkEnv('server', 'ACACHA_FORGE_SERVER');
137
        $this->site = $this->checkEnv('site', 'ACACHA_FORGE_SITE');
138
        $this->dieIfEnvVarIsNotInstalled('ACACHA_FORGE_ACCESS_TOKEN');
139
    }
140
}
141