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 PublishShowAssignment. |
15
|
|
|
* |
16
|
|
|
* @package Acacha\ForgePublish\Commands |
17
|
|
|
*/ |
18
|
|
|
class PublishShowAssignment extends Command |
19
|
|
|
{ |
20
|
|
|
use InteractsWithEnvironment, |
21
|
|
|
AborstIfEnvVariableIsnotInstalled, |
22
|
|
|
InteractsWithAssignments, |
23
|
|
|
ItFetchesAssignments; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The name and signature of the console command. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $signature = 'publish:show_assignment {assignment? : The assignment to show}' ; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The console command description. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $description = 'Shows an assignment'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Assignment. |
41
|
|
|
* |
42
|
|
|
* @var integer |
43
|
|
|
*/ |
44
|
|
|
protected $assignment; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Server names. |
48
|
|
|
* |
49
|
|
|
* @var Client |
50
|
|
|
*/ |
51
|
|
|
protected $http; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* SaveEnvVariable constructor. |
55
|
|
|
* |
56
|
|
|
*/ |
57
|
|
|
public function __construct(Client $http) |
58
|
|
|
{ |
59
|
|
|
parent::__construct(); |
60
|
|
|
$this->http = $http; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Execute the console command. |
65
|
|
|
* |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function handle() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$this->abortCommandExecution(); |
70
|
|
|
$this->assignment = $this->argument('assignment') ? $this->argument('assignment') : $this->askAssignment(); |
71
|
|
|
|
72
|
|
|
$this->show_assignment(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Show assignment. |
77
|
|
|
*/ |
78
|
|
|
protected function show_assignment() { |
79
|
|
|
$assignment = $this->fetch_assignment(); |
80
|
|
|
|
81
|
|
|
$headers = ['Key', 'Value']; |
82
|
|
|
|
83
|
|
|
$rows = [ |
84
|
|
|
[ 'Id' , $assignment->id], |
85
|
|
|
[ 'Name' , $assignment->name], |
86
|
|
|
[ 'Repository uri' , $assignment->repository_uri], |
87
|
|
|
[ 'Repository type' , $assignment->repository_type], |
88
|
|
|
[ 'Forge Site' , $assignment->forge_site], |
89
|
|
|
[ 'Forge server' , $assignment->forge_server], |
90
|
|
|
[ 'Groups' , join(', ', collect($assignment->groups)->pluck('name')->toArray()) ], |
91
|
|
|
[ 'Users' , join(', ', collect($assignment->users)->pluck('name')->toArray())], |
92
|
|
|
[ 'Teachers' , join(', ', collect($assignment->assignators)->pluck('name')->toArray())], |
93
|
|
|
[ 'Created' , $assignment->created_at], |
94
|
|
|
[ 'Updated' , $assignment->updated_at] |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
$this->table($headers, $rows); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Show assignment. |
102
|
|
|
* |
103
|
|
|
* @return array|mixed |
104
|
|
|
*/ |
105
|
|
|
protected function fetch_assignment() |
106
|
|
|
{ |
107
|
|
|
$uri = str_replace('{assignment}', $this->assignment, config('forge-publish.show_assignment_uri')); |
108
|
|
|
$url = config('forge-publish.url') . $uri; |
109
|
|
|
try { |
110
|
|
|
$response = $this->http->get($url, [ |
111
|
|
|
'headers' => [ |
112
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
113
|
|
|
'Authorization' => 'Bearer ' . fp_env('ACACHA_FORGE_ACCESS_TOKEN') |
114
|
|
|
] |
115
|
|
|
]); |
116
|
|
|
} catch (\Exception $e) { |
117
|
|
|
$this->error('And error occurs connecting to the api url: ' . $url); |
118
|
|
|
$this->error('Status code: ' . $e->getResponse()->getStatusCode() . ' | Reason : ' . $e->getResponse()->getReasonPhrase()); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
return json_decode((string) $response->getBody()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Abort command execution. |
125
|
|
|
*/ |
126
|
|
|
protected function abortCommandExecution() |
127
|
|
|
{ |
128
|
|
|
$this->abortsIfEnvVarIsNotInstalled('ACACHA_FORGE_ACCESS_TOKEN'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.