|
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 PublishDeploy. |
|
12
|
|
|
* |
|
13
|
|
|
* @package Acacha\ForgePublish\Commands |
|
14
|
|
|
*/ |
|
15
|
|
|
class PublishDeploy 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:deploy {--server=} {--site=}'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The console command description. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $description = 'Deploy site'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* API endpint 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
|
|
View Code Duplication |
public function handle() |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$this->abortCommandExecution(); |
|
78
|
|
|
|
|
79
|
|
|
$this->info('Executing deploy script for site ' . $this->site . ' in Laravel Forge'); |
|
80
|
|
|
|
|
81
|
|
|
$this->url = $this->obtainAPIURLEndpoint(); |
|
82
|
|
|
|
|
83
|
|
|
$this->http->post($this->url, [ |
|
84
|
|
|
'headers' => [ |
|
85
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
|
86
|
|
|
'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN') |
|
87
|
|
|
] |
|
88
|
|
|
] |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Obtain API URL endpoint. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function obtainAPIURLEndpoint() |
|
98
|
|
|
{ |
|
99
|
|
|
$uri = str_replace('{forgeserver}', $this->server, config('forge-publish.post_deploy_site_uri')); |
|
100
|
|
|
$uri = str_replace('{forgesite}', $this->site, $uri); |
|
101
|
|
|
return config('forge-publish.url') . $uri; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Abort command execution. |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function abortCommandExecution() |
|
108
|
|
|
{ |
|
109
|
|
|
$this->server = $this->checkEnv('server', 'ACACHA_FORGE_SERVER'); |
|
110
|
|
|
$this->site = $this->checkEnv('site', 'ACACHA_FORGE_SITE'); |
|
111
|
|
|
$this->dieIfEnvVarIsNotInstalled('ACACHA_FORGE_ACCESS_TOKEN'); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.