Passed
Pull Request — 1.11.x (#4616)
by Angel Fernando Quiroz
09:25 queued 48s
created

RequestTrait   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 26
eloc 88
c 2
b 0
f 0
dl 0
loc 175
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B doEditRequest() 0 37 6
B doCreateRequest() 0 46 8
A doDeleteRequest() 0 35 6
B doVisibilityRequest() 0 37 6
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 doVisibilityRequest(array $data)
114
    {
115
        try {
116
            $token = $this->plugin->getAccessToken();
117
        } catch (OptimisticLockException|ORMException|Exception $e) {
118
            throw new Exception($e->getMessage());
119
        }
120
121
        $options = [
122
            'headers' => [
123
                'Authorization' => "Bearer $token",
124
            ],
125
            'json' => $data,
126
        ];
127
128
        $client = new Client();
129
130
        try {
131
            $response = $client->post(
132
                $this->plugin->get(ExternalNotificationConnectPlugin::SETTING_NOTIFICATION_URL).'/visibility',
133
                $options
134
            );
135
        } catch (ClientException|ServerException $e) {
136
            if (!$e->hasResponse()) {
137
                throw new Exception($e->getMessage());
138
            }
139
140
            $response = $e->getResponse();
141
        }
142
143
        $json = json_decode((string) $response->getBody(), true);
144
145
        if (isset($json['status']) && 500 === $json['status']) {
146
            throw new Exception($json['message']);
147
        }
148
149
        return $json;
150
    }
151
152
    /**
153
     * @throws Exception
154
     */
155
    protected function doDeleteRequest(int $contentId, string $contentType): array
156
    {
157
        try {
158
            $token = $this->plugin->getAccessToken();
159
        } catch (OptimisticLockException|ORMException|Exception $e) {
160
            throw new Exception($e->getMessage());
161
        }
162
163
        $url = $this->plugin->get(ExternalNotificationConnectPlugin::SETTING_NOTIFICATION_URL)."/$contentId/$contentType";
164
165
        $options = [
166
            'headers' => [
167
                'Authorization' => "Bearer $token",
168
            ],
169
        ];
170
171
        $client = new Client();
172
173
        try {
174
            $response = $client->delete($url, $options);
175
        } catch (ClientException|ServerException $e) {
176
            if (!$e->hasResponse()) {
177
                throw new Exception($e->getMessage());
178
            }
179
180
            $response = $e->getResponse();
181
        }
182
183
        $json = json_decode((string) $response->getBody(), true);
184
185
        if (isset($json['status']) && 500 === $json['status']) {
186
            throw new Exception($json['message']);
187
        }
188
189
        return $json;
190
    }
191
}
192