1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acacha\ForgePublish\Commands; |
4
|
|
|
|
5
|
|
|
use Acacha\ForgePublish\Commands\Traits\AborstIfEnvVariableIsnotInstalled; |
6
|
|
|
use Acacha\ForgePublish\Commands\Traits\InteractsWithAssignments; |
7
|
|
|
use Acacha\ForgePublish\Commands\Traits\InteractsWithEnvironment; |
8
|
|
|
use Acacha\ForgePublish\Commands\Traits\InteractsWithLocalGithub; |
9
|
|
|
use Acacha\ForgePublish\Commands\Traits\ItFetchesAssignments; |
10
|
|
|
use GuzzleHttp\Client; |
11
|
|
|
use Illuminate\Console\Command; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PublishUpdateAssignment. |
15
|
|
|
* |
16
|
|
|
* @package Acacha\ForgePublish\Commands |
17
|
|
|
*/ |
18
|
|
|
class PublishUpdateAssignment extends Command |
19
|
|
|
{ |
20
|
|
|
use InteractsWithEnvironment, AborstIfEnvVariableIsnotInstalled, InteractsWithAssignments, ItFetchesAssignments; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The name and signature of the console command. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $signature = 'publish:update_assignment {assignment? : The assignment to update} |
28
|
|
|
{name? : The name description} |
29
|
|
|
{repository_uri? : The repository URI} |
30
|
|
|
{repository_type? : The repository type} |
31
|
|
|
{forge_site? : The Laravel Forge site id} |
32
|
|
|
{forge_server? : The Laravel Forge Server}'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The console command description. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $description = 'Updates an assignment'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Server names. |
43
|
|
|
* |
44
|
|
|
* @var Client |
45
|
|
|
*/ |
46
|
|
|
protected $http; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Assignments. |
50
|
|
|
* |
51
|
|
|
* @var [] |
52
|
|
|
*/ |
53
|
|
|
protected $assignments; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Assignment. |
57
|
|
|
* |
58
|
|
|
* @var integer |
59
|
|
|
*/ |
60
|
|
|
protected $assignment; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Assignment name |
64
|
|
|
* |
65
|
|
|
* @var String |
66
|
|
|
*/ |
67
|
|
|
protected $assignmentName; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Repository uri. |
71
|
|
|
* |
72
|
|
|
* @var String |
73
|
|
|
*/ |
74
|
|
|
protected $repository_uri; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Repository type. |
78
|
|
|
* |
79
|
|
|
* @var String |
80
|
|
|
*/ |
81
|
|
|
protected $repository_type; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Laravel Forge site id. |
85
|
|
|
* |
86
|
|
|
* @var String |
87
|
|
|
*/ |
88
|
|
|
protected $forge_site; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Laravel Forge server id. |
92
|
|
|
* |
93
|
|
|
* @var String |
94
|
|
|
*/ |
95
|
|
|
protected $forge_server; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* SaveEnvVariable constructor. |
99
|
|
|
* |
100
|
|
|
*/ |
101
|
|
|
public function __construct(Client $http) |
102
|
|
|
{ |
103
|
|
|
parent::__construct(); |
104
|
|
|
$this->http = $http; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Execute the console command. |
109
|
|
|
* |
110
|
|
|
*/ |
111
|
|
|
public function handle() |
112
|
|
|
{ |
113
|
|
|
$this->abortCommandExecution(); |
114
|
|
|
|
115
|
|
|
$this->assignment = $this->argument('assignment') ? $this->argument('assignment') : $this->askAssignment(); |
116
|
|
|
$this->assignmentName = $this->argument('name') ? $this->argument('name') : $this->askName(); |
117
|
|
|
$this->repository_uri = $this->argument('repository_uri') ? $this->argument('repository_uri') : $this->askRepositoryUri(); |
118
|
|
|
$this->repository_type = $this->argument('repository_type') ? $this->argument('repository_type') : $this->askRepositoryType(); |
119
|
|
|
$this->forge_site = $this->argument('forge_site') ? $this->argument('forge_site') : $this->askForgeSite(); |
120
|
|
|
$this->forge_server = $this->argument('forge_server') ? $this->argument('forge_server') : $this->askForgeServer(); |
121
|
|
|
|
122
|
|
|
$this->updateAssignment(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Update assignment. |
127
|
|
|
* |
128
|
|
|
* @return array|mixed |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
protected function updateAssignment() |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$uri = str_replace('{assignment}', $this->assignment, config('forge-publish.update_assignment_uri')); |
133
|
|
|
$url = config('forge-publish.url') . $uri; |
134
|
|
|
try { |
135
|
|
|
$response = $this->http->put($url, [ |
136
|
|
|
'form_params' => [ |
137
|
|
|
'name' => $this->assignmentName, |
138
|
|
|
'repository_uri' => $this->repository_uri, |
139
|
|
|
'repository_type' => $this->repository_type, |
140
|
|
|
'forge_site' => $this->forge_site, |
141
|
|
|
'forge_server' => $this->forge_server |
142
|
|
|
], |
143
|
|
|
'headers' => [ |
144
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
145
|
|
|
'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN') |
146
|
|
|
] |
147
|
|
|
]); |
148
|
|
|
} catch (\Exception $e) { |
149
|
|
|
$this->error('And error occurs connecting to the api url: ' . $url); |
150
|
|
|
$this->error('Status code: ' . $e->getResponse()->getStatusCode() . ' | Reason : ' . $e->getResponse()->getReasonPhrase()); |
|
|
|
|
151
|
|
|
return []; |
152
|
|
|
} |
153
|
|
|
return json_decode((string) $response->getBody()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Abort command execution. |
158
|
|
|
*/ |
159
|
|
|
protected function abortCommandExecution() |
160
|
|
|
{ |
161
|
|
|
$this->abortsIfEnvVarIsNotInstalled('ACACHA_FORGE_ACCESS_TOKEN'); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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.