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 PublishSSL. |
12
|
|
|
* |
13
|
|
|
* @package Acacha\ForgePublish\Commands |
14
|
|
|
*/ |
15
|
|
|
class PublishSSL extends Command |
16
|
|
|
{ |
17
|
|
|
use ChecksEnv, DiesIfEnvVariableIsnotInstalled; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Server name |
21
|
|
|
* |
22
|
|
|
* @var String |
23
|
|
|
*/ |
24
|
|
|
protected $server; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Domain |
28
|
|
|
* |
29
|
|
|
* @var String |
30
|
|
|
*/ |
31
|
|
|
protected $domain; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Forge site id. |
35
|
|
|
* |
36
|
|
|
* @var String |
37
|
|
|
*/ |
38
|
|
|
protected $site; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The name and signature of the console command. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $signature = 'publish:ssl {--server=} {--domain=} {--site=}'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The console command description. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $description = 'Obtain lets encrypt certificate'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* API endpoint URL |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $url; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Guzzle http client. |
63
|
|
|
* |
64
|
|
|
* @var Client |
65
|
|
|
*/ |
66
|
|
|
protected $http; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Create a new command instance. |
70
|
|
|
* |
71
|
|
|
*/ |
72
|
|
|
public function __construct(Client $http) |
73
|
|
|
{ |
74
|
|
|
parent::__construct(); |
75
|
|
|
$this->http = $http; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Execute the console command. |
80
|
|
|
* |
81
|
|
|
*/ |
82
|
|
|
public function handle() |
83
|
|
|
{ |
84
|
|
|
$this->abortCommandExecution(); |
85
|
|
|
$this->info("Obtaining Lets Encrypt Certificate on production..."); |
86
|
|
|
|
87
|
|
|
$this->url = $this->obtainAPIURLEndpoint(); |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
$this->http->post($this->url, |
91
|
|
|
[ |
92
|
|
|
'form_params' => [ |
93
|
|
|
'domains' => [$this->domain] |
94
|
|
|
], |
95
|
|
|
'headers' => [ |
96
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
97
|
|
|
'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN') |
98
|
|
|
] |
99
|
|
|
] |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Obtain API URL endpoint. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function obtainAPIURLEndpoint() |
109
|
|
|
{ |
110
|
|
|
$uri = str_replace('{forgeserver}', $this->server, config('forge-publish.post_lets_encrypt_uri')); |
111
|
|
|
$uri = str_replace('{forgesite}', $this->site, $uri); |
112
|
|
|
return config('forge-publish.url') . $uri; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Abort command execution? |
117
|
|
|
*/ |
118
|
|
|
protected function abortCommandExecution() |
119
|
|
|
{ |
120
|
|
|
$this->server = $this->checkEnv('server', 'ACACHA_FORGE_SERVER'); |
121
|
|
|
$this->domain = $this->checkEnv('domain', 'ACACHA_FORGE_DOMAIN'); |
122
|
|
|
$this->site = $this->checkEnv('site', 'ACACHA_FORGE_SITE'); |
123
|
|
|
$this->dieIfEnvVarIsNotInstalled('ACACHA_FORGE_ACCESS_TOKEN'); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|