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 PublishMySQLUsers. |
12
|
|
|
* |
13
|
|
|
* @package Acacha\ForgePublish\Commands |
14
|
|
|
*/ |
15
|
|
|
class PublishMySQLUsers 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:mysql-users {name?} {user?} {databases?} {--server=} {--dump}'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The console command description. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Manage MySQL user databases'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Server forge id. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $server; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Guzzle http client. |
42
|
|
|
* |
43
|
|
|
* @var Client |
44
|
|
|
*/ |
45
|
|
|
protected $http; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create a new command instance. |
49
|
|
|
* |
50
|
|
|
*/ |
51
|
|
|
public function __construct(Client $http) |
52
|
|
|
{ |
53
|
|
|
parent::__construct(); |
54
|
|
|
$this->http = $http; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Execute the console command. |
59
|
|
|
* |
60
|
|
|
*/ |
61
|
|
|
public function handle() |
62
|
|
|
{ |
63
|
|
|
$this->abortCommandExecution(); |
64
|
|
|
|
65
|
|
|
if ($this->argument('name')) { |
66
|
|
|
$this->createMySQLUser(); |
67
|
|
|
} else { |
68
|
|
|
$this->listMySQLUsers(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create MySQL user. |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
protected function createMySQLUser() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$this->checkParameters(); |
78
|
|
|
$this->url = $this->obtainAPIURLEndpoint(); |
79
|
|
|
$this->http->post($this->url, [ |
80
|
|
|
'form_params' => $this->getData(), |
81
|
|
|
'headers' => [ |
82
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
83
|
|
|
'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN') |
84
|
|
|
] |
85
|
|
|
] |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* List MySQL users. |
91
|
|
|
*/ |
92
|
|
|
protected function listMySQLUsers() |
93
|
|
|
{ |
94
|
|
|
$this->url = $this->obtainAPIURLEndpointForList(); |
95
|
|
|
$response = $this->http->get($this->url, [ |
96
|
|
|
'headers' => [ |
97
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
98
|
|
|
'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN') |
99
|
|
|
] |
100
|
|
|
] |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$users = json_decode($response->getBody(), true) ; |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
if ($this->option('dump')) { |
107
|
|
|
dump($users); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (empty($users)) { |
111
|
|
|
$this->error('No users found.'); |
112
|
|
|
die(); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$headers = ['Id', 'Server Id','Name','Status','Created at']; |
116
|
|
|
|
117
|
|
|
$rows = []; |
118
|
|
View Code Duplication |
foreach ($users as $user) { |
|
|
|
|
119
|
|
|
$rows[] = [ |
120
|
|
|
$user['id'], |
121
|
|
|
$user['serverId'], |
122
|
|
|
$user['name'], |
123
|
|
|
$user['status'], |
124
|
|
|
$user['createdAt'] |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->table($headers, $rows); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Check parameters. |
133
|
|
|
*/ |
134
|
|
|
protected function checkParameters() |
135
|
|
|
{ |
136
|
|
|
if ($this->argument('user')) { |
137
|
|
|
if (! $this->argument('password')) { |
138
|
|
|
$this->error('Password argument is required if user argument is provided!'); |
139
|
|
|
die(); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get data. |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
protected function getData() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
if ($this->argument('user')) { |
152
|
|
|
return [ |
153
|
|
|
'name' => $this->argument('name'), |
154
|
|
|
'user' => $this->argument('user'), |
155
|
|
|
'password' => $this->argument('password') |
156
|
|
|
]; |
157
|
|
|
} else { |
158
|
|
|
return [ |
159
|
|
|
'name' => $this->argument('name') |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Obtain API URL endpoint. |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
protected function obtainAPIURLEndpoint() |
170
|
|
|
{ |
171
|
|
|
$uri = str_replace('{forgeserver}', $this->server, config('forge-publish.post_mysql_users_uri')); |
172
|
|
|
return config('forge-publish.url') . $uri; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Obtain API URL endpoint. |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
protected function obtainAPIURLEndpointForList() |
181
|
|
|
{ |
182
|
|
|
$uri = str_replace('{forgeserver}', $this->server, config('forge-publish.get_mysql_users_uri')); |
183
|
|
|
return config('forge-publish.url') . $uri; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Abort command execution. |
188
|
|
|
*/ |
189
|
|
|
protected function abortCommandExecution() |
190
|
|
|
{ |
191
|
|
|
$this->server = $this->checkEnv('server', 'ACACHA_FORGE_SERVER'); |
192
|
|
|
$this->dieIfEnvVarIsNotInstalled('ACACHA_FORGE_ACCESS_TOKEN'); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
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.