1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acacha\ForgePublish\Commands; |
4
|
|
|
|
5
|
|
|
use Acacha\ForgePublish\Commands\Traits\AborstIfEnvVariableIsnotInstalled; |
6
|
|
|
use Acacha\ForgePublish\Commands\Traits\InteractsWithEnvironment; |
7
|
|
|
use Acacha\ForgePublish\Commands\Traits\InteractsWithLocalGithub; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use Illuminate\Console\Command; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class PublishAssignmentUsers. |
13
|
|
|
* |
14
|
|
|
* @package Acacha\ForgePublish\Commands |
15
|
|
|
*/ |
16
|
|
|
class PublishAssignmentUsers extends Command |
17
|
|
|
{ |
18
|
|
|
use InteractsWithLocalGithub, InteractsWithEnvironment; |
19
|
|
|
|
20
|
|
|
use AborstIfEnvVariableIsnotInstalled; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The name and signature of the console command. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $signature = 'publish:assignment_users {--user=* : The user/s assigned to this assignment}'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The console command description. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $description = 'Assing users to current assignment'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Server names. |
38
|
|
|
* |
39
|
|
|
* @var Client |
40
|
|
|
*/ |
41
|
|
|
protected $http; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Users. |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $users; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* SaveEnvVariable constructor. |
52
|
|
|
* |
53
|
|
|
*/ |
54
|
|
|
public function __construct(Client $http) |
55
|
|
|
{ |
56
|
|
|
parent::__construct(); |
57
|
|
|
$this->http = $http; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Execute the console command. |
62
|
|
|
* |
63
|
|
|
*/ |
64
|
|
|
public function handle() |
65
|
|
|
{ |
66
|
|
|
$this->abortCommandExecution(); |
67
|
|
|
|
68
|
|
|
$this->users = $this->option('user') ? $this->argument('user') : $this->askForUsers(); |
69
|
|
|
|
70
|
|
|
if (count($this->users) == 0) { |
71
|
|
|
$this->info('Skipping users...'); |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
$this->assignUsersToAssignment(); |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Assign users to assignment |
80
|
|
|
* |
81
|
|
|
* @return array|mixed |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
protected function assignUsersToAssignment() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$assignment = fp_env('ACACHA_FORGE_ASSIGNMENT'); |
86
|
|
|
foreach ( $this->users as $user) { |
87
|
|
|
$uri = str_replace('{assignment}', $assignment, config('forge-publish.assign_user_to_assignment_uri')); |
88
|
|
|
$uri = str_replace('{user}', $user, $uri); |
89
|
|
|
$url = config('forge-publish.url') . $uri; |
90
|
|
|
try { |
91
|
|
|
$response = $this->http->post($url, [ |
|
|
|
|
92
|
|
|
'headers' => [ |
93
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
94
|
|
|
'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN') |
95
|
|
|
] |
96
|
|
|
]); |
97
|
|
|
} catch (\Exception $e) { |
98
|
|
|
if ($e->getResponse()->getStatusCode() == 422) { |
|
|
|
|
99
|
|
|
$this->error('The user is already assigned'); |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
$this->error('And error occurs connecting to the api url: ' . $url); |
103
|
|
|
$this->error('Status code: ' . $e->getResponse()->getStatusCode() . ' | Reason : ' . $e->getResponse()->getReasonPhrase()); |
|
|
|
|
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get users |
111
|
|
|
*/ |
112
|
|
|
protected function users() |
113
|
|
|
{ |
114
|
|
|
$url = config('forge-publish.url') . config('forge-publish.list_users_uri'); |
115
|
|
|
try { |
116
|
|
|
$response = $this->http->get($url, [ |
117
|
|
|
'headers' => [ |
118
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
119
|
|
|
'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN') |
120
|
|
|
] |
121
|
|
|
]); |
122
|
|
|
} catch (\Exception $e) { |
123
|
|
|
$this->error('And error occurs connecting to the api url: ' . $url); |
124
|
|
|
$this->error('Status code: ' . $e->getResponse()->getStatusCode() . ' | Reason : ' . $e->getResponse()->getReasonPhrase()); |
|
|
|
|
125
|
|
|
return []; |
126
|
|
|
} |
127
|
|
|
return json_decode((string) $response->getBody()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Ask for users. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
protected function askForUsers() |
136
|
|
|
{ |
137
|
|
|
$default = 0; |
138
|
|
|
$users = $this->users(); |
139
|
|
|
$user_names = array_merge( |
140
|
|
|
['Skip'], |
141
|
|
|
collect($users)->pluck('name')->toArray() |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$selected_user_names = $this->choice('Users?', $user_names ,$default, null, true); |
145
|
|
|
|
146
|
|
|
if ($selected_user_names == 0) return null; |
147
|
|
|
$users = collect($users)->filter(function ($user) use ($selected_user_names) { |
148
|
|
|
return in_array($user->name,$selected_user_names); |
149
|
|
|
}); |
150
|
|
|
|
151
|
|
|
return $users->pluck('id'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Abort command execution. |
156
|
|
|
*/ |
157
|
|
|
protected function abortCommandExecution() |
158
|
|
|
{ |
159
|
|
|
$this->abortsIfEnvVarIsNotInstalled('ACACHA_FORGE_ASSIGNMENT'); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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.