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 PublishAssignmentGroups. |
13
|
|
|
* |
14
|
|
|
* @package Acacha\ForgePublish\Commands |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
class PublishAssignmentGroups 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_groups {--group=* : The group/s assigned to this assignment}'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The console command description. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $description = 'Assign a group of users to the current assignment'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Server names. |
38
|
|
|
* |
39
|
|
|
* @var Client |
40
|
|
|
*/ |
41
|
|
|
protected $http; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Groups |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $groups; |
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->groups = $this->option('group') ? $this->argument('group') : $this->askForGroups(); |
69
|
|
|
$this->assignGroupsToAssignment(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Assign groups to assignment |
74
|
|
|
* |
75
|
|
|
* @return array|mixed |
76
|
|
|
*/ |
77
|
|
|
protected function assignGroupsToAssignment() |
78
|
|
|
{ |
79
|
|
|
$assignment = fp_env('ACACHA_FORGE_ASSIGNMENT'); |
80
|
|
|
foreach ( $this->groups as $group) { |
81
|
|
|
$uri = str_replace('{assignment}', $assignment, config('forge-publish.assign_group_to_assignment_uri')); |
82
|
|
|
$uri = str_replace('{group}', $group, $uri); |
83
|
|
|
$url = config('forge-publish.url') . $uri; |
84
|
|
|
try { |
85
|
|
|
$response = $this->http->post($url, [ |
|
|
|
|
86
|
|
|
'headers' => [ |
87
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
88
|
|
|
'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN') |
89
|
|
|
] |
90
|
|
|
]); |
91
|
|
|
} catch (\Exception $e) { |
92
|
|
|
if ($e->getResponse()->getStatusCode() == 422) { |
|
|
|
|
93
|
|
|
$this->error('The group is already assigned'); |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
$this->error('And error occurs connecting to the api url: ' . $url); |
97
|
|
|
$this->error('Status code: ' . $e->getResponse()->getStatusCode() . ' | Reason : ' . $e->getResponse()->getReasonPhrase()); |
|
|
|
|
98
|
|
|
return []; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Ask for groups. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function askForGroups() |
109
|
|
|
{ |
110
|
|
|
$default = 0; |
111
|
|
|
$groups = $this->groups(); |
112
|
|
|
$group_names = collect($groups)->pluck('name')->toArray(); |
113
|
|
|
$selected_group_names = $this->choice('Groups?', $group_names ,$default, null, true); |
114
|
|
|
|
115
|
|
|
$groups = collect($groups)->filter(function ($group) use ($selected_group_names) { |
116
|
|
|
return in_array($group->name,$selected_group_names); |
117
|
|
|
}); |
118
|
|
|
|
119
|
|
|
return $groups->pluck('id'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get groups |
124
|
|
|
*/ |
125
|
|
|
protected function groups() |
126
|
|
|
{ |
127
|
|
|
$url = config('forge-publish.url') . config('forge-publish.list_groups_uri'); |
128
|
|
|
try { |
129
|
|
|
$response = $this->http->get($url, [ |
130
|
|
|
'headers' => [ |
131
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
132
|
|
|
'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN') |
133
|
|
|
] |
134
|
|
|
]); |
135
|
|
|
} catch (\Exception $e) { |
136
|
|
|
$this->error('And error occurs connecting to the api url: ' . $url); |
137
|
|
|
$this->error('Status code: ' . $e->getResponse()->getStatusCode() . ' | Reason : ' . $e->getResponse()->getReasonPhrase()); |
|
|
|
|
138
|
|
|
return []; |
139
|
|
|
} |
140
|
|
|
return json_decode((string) $response->getBody()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Abort command execution. |
145
|
|
|
*/ |
146
|
|
|
protected function abortCommandExecution() |
147
|
|
|
{ |
148
|
|
|
$this->abortsIfEnvVarIsNotInstalled('ACACHA_FORGE_ASSIGNMENT'); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
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.