1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
namespace Chamilo\PluginBundle\ExternalNotificationConnect\Traits\RequestTrait; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\OptimisticLockException; |
8
|
|
|
use Doctrine\ORM\ORMException; |
9
|
|
|
use Exception; |
10
|
|
|
use ExternalNotificationConnectPlugin; |
11
|
|
|
use GuzzleHttp\Client; |
12
|
|
|
use GuzzleHttp\Exception\ClientException; |
13
|
|
|
use GuzzleHttp\Exception\ServerException; |
14
|
|
|
|
15
|
|
|
trait RequestTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @throws Exception |
19
|
|
|
*/ |
20
|
|
|
protected function doCreateRequest($json): ?array |
21
|
|
|
{ |
22
|
|
|
try { |
23
|
|
|
$token = $this->plugin->getAccessToken(); |
24
|
|
|
} catch (OptimisticLockException|ORMException|Exception $e) { |
25
|
|
|
throw new Exception($e->getMessage()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$options = [ |
29
|
|
|
'headers' => [ |
30
|
|
|
'Authorization' => "Bearer $token", |
31
|
|
|
], |
32
|
|
|
'json' => $json, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
$client = new Client(); |
36
|
|
|
|
37
|
|
|
try { |
38
|
|
|
$response = $client->post( |
39
|
|
|
$this->plugin->get(ExternalNotificationConnectPlugin::SETTING_NOTIFICATION_URL), |
40
|
|
|
$options |
41
|
|
|
); |
42
|
|
|
} catch (ClientException|ServerException $e) { |
43
|
|
|
if (!$e->hasResponse()) { |
44
|
|
|
throw new Exception($e->getMessage()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$response = $e->getResponse(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$json = json_decode((string) $response->getBody(), true); |
51
|
|
|
|
52
|
|
|
if (isset($json['status']) && 500 === $json['status']) { |
53
|
|
|
throw new Exception($json['message']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (isset($json['validation_errors']) && $json['validation_errors']) { |
57
|
|
|
$messageError = implode( |
58
|
|
|
'<br>', |
59
|
|
|
array_column($json['errors'], 'message') |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
throw new Exception($messageError); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $json; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @throws Exception |
70
|
|
|
*/ |
71
|
|
|
protected function doEditRequest(array $json): array |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
$token = $this->plugin->getAccessToken(); |
75
|
|
|
} catch (OptimisticLockException|ORMException|Exception $e) { |
76
|
|
|
throw new Exception($e->getMessage()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$url = $this->plugin->get(ExternalNotificationConnectPlugin::SETTING_NOTIFICATION_URL) |
80
|
|
|
.'/'.$json['content_id'].'/'.$json['content_type']; |
81
|
|
|
|
82
|
|
|
$options = [ |
83
|
|
|
'headers' => [ |
84
|
|
|
'Authorization' => "Bearer $token", |
85
|
|
|
], |
86
|
|
|
'json' => $json, |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
$client = new Client(); |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
$response = $client->post($url, $options); |
93
|
|
|
} catch (ClientException|ServerException $e) { |
94
|
|
|
if (!$e->hasResponse()) { |
95
|
|
|
throw new Exception($e->getMessage()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$response = $e->getResponse(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$json = json_decode((string) $response->getBody(), true); |
102
|
|
|
|
103
|
|
|
if (isset($json['status']) && 500 === $json['status']) { |
104
|
|
|
throw new Exception($json['message']); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $json; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @throws Exception |
112
|
|
|
*/ |
113
|
|
|
protected function doDeleteRequest(int $contentId, string $contentType): array |
114
|
|
|
{ |
115
|
|
|
try { |
116
|
|
|
$token = $this->plugin->getAccessToken(); |
117
|
|
|
} catch (OptimisticLockException|ORMException|Exception $e) { |
118
|
|
|
throw new Exception($e->getMessage()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$url = $this->plugin->get(ExternalNotificationConnectPlugin::SETTING_NOTIFICATION_URL)."/$contentId/$contentType"; |
122
|
|
|
|
123
|
|
|
$options = [ |
124
|
|
|
'headers' => [ |
125
|
|
|
'Authorization' => "Bearer $token", |
126
|
|
|
], |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
$client = new Client(); |
130
|
|
|
|
131
|
|
|
try { |
132
|
|
|
$response = $client->delete($url, $options); |
133
|
|
|
} catch (ClientException|ServerException $e) { |
134
|
|
|
if (!$e->hasResponse()) { |
135
|
|
|
throw new Exception($e->getMessage()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$response = $e->getResponse(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$json = json_decode((string) $response->getBody(), true); |
142
|
|
|
|
143
|
|
|
if (isset($json['status']) && 500 === $json['status']) { |
144
|
|
|
throw new Exception($json['message']); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $json; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|