|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
namespace Acacha\ForgePublish\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Acacha\ForgePublish\Commands\Traits\ItFetchesServers; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class PublishServer. |
|
10
|
|
|
* |
|
11
|
|
|
* @package Acacha\ForgePublish\Commands |
|
12
|
|
|
*/ |
|
13
|
|
|
class PublishServer extends SaveEnvVariable |
|
14
|
|
|
{ |
|
15
|
|
|
use ItFetchesServers; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The name and signature of the console command. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $signature = 'publish:server {server?}'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The console command description. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $description = 'Save acacha forge server'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Servers. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $servers; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Server. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $server; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Server names. |
|
47
|
|
|
* |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $server_names; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Server names. |
|
54
|
|
|
* |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $server_ids; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Server names. |
|
61
|
|
|
* |
|
62
|
|
|
* @var Client |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $http; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* SaveEnvVariable constructor. |
|
68
|
|
|
* |
|
69
|
|
|
*/ |
|
70
|
|
|
public function __construct(Client $http) |
|
71
|
|
|
{ |
|
72
|
|
|
parent::__construct(); |
|
73
|
|
|
$this->http = $http; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Env var to set. |
|
78
|
|
|
* |
|
79
|
|
|
* @return mixed |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function envVar() |
|
82
|
|
|
{ |
|
83
|
|
|
return 'ACACHA_FORGE_SERVER'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Argument key. |
|
88
|
|
|
* |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function argKey() |
|
92
|
|
|
{ |
|
93
|
|
|
return 'server'; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Question text. |
|
98
|
|
|
* |
|
99
|
|
|
* @return mixed |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function questionText() |
|
102
|
|
|
{ |
|
103
|
|
|
return 'Acacha forge server (forge id)?'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Before hook. |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function before() |
|
110
|
|
|
{ |
|
111
|
|
|
while (! $this->confirm('Do you have a validated server assigned at http:://forge.acacha.com?')) { |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
$this->servers = $this->fetchServers(); |
|
|
|
|
|
|
114
|
|
|
if (empty($this->servers)) { |
|
115
|
|
|
$this->error('No valid servers assigned to user!'); |
|
116
|
|
|
die(); |
|
117
|
|
|
} |
|
118
|
|
|
$this->server_names = collect($this->servers)->pluck('name')->toArray(); |
|
119
|
|
|
$this->server_ids = collect($this->servers)->pluck('forge_id')->toArray(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* After hook. |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function after() |
|
126
|
|
|
{ |
|
127
|
|
|
$ip_address = $this->serverIpAddress($this->servers, $this->server); |
|
128
|
|
|
$server_name = $this->getForgeName($this->servers, $this->server); |
|
129
|
|
|
$this->call("publish:ip", [ |
|
130
|
|
|
'ip' => $ip_address |
|
131
|
|
|
]); |
|
132
|
|
|
$this->call("publish:server_name", [ |
|
133
|
|
|
'server_name' => $server_name |
|
134
|
|
|
]); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Default proposed value when asking. |
|
139
|
|
|
* |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function default() |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
|
|
return array_search(fp_env('ACACHA_FORGE_SERVER'), $this->server_ids); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Value. |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function value() |
|
150
|
|
|
{ |
|
151
|
|
|
$server_name = $this->choice($this->questionText(), $this->server_names, $this->default()); |
|
152
|
|
|
return $this->server = $this->getForgeIdServer($this->servers, $server_name); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.