Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.44% |
85 / 90 |
|
86.67% |
13 / 15 |
CRAP | |
0.00% |
0 / 1 |
| WebhookTask | |
94.44% |
85 / 90 |
|
86.67% |
13 / 15 |
31.16 | |
0.00% |
0 / 1 |
| __invoke | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| prepareRequest | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
| performRequest | |
85.19% |
23 / 27 |
|
0.00% |
0 / 1 |
6.12 | |||
| handleResponse | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
6 | |||
| isAcceptableResponseStatusCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getWebhookConfig | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getWebhookRegistry | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getWebhookPayloadFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getWebhookTaskParamsFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getWebhookResponseFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getWebhookSender | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getWebhookTaskService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getWebhookTaskReports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTaskContext | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| retryTask | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2019 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\webhooks\task; |
| 22 | |
| 23 | use GuzzleHttp\Exception\BadResponseException; |
| 24 | use GuzzleHttp\Exception\ConnectException; |
| 25 | use GuzzleHttp\Exception\GuzzleException; |
| 26 | use GuzzleHttp\Exception\RequestException; |
| 27 | use GuzzleHttp\Exception\ServerException; |
| 28 | use GuzzleHttp\Psr7\Request; |
| 29 | use oat\oatbox\extension\AbstractAction; |
| 30 | use oat\tao\model\taskQueue\Task\TaskAwareInterface; |
| 31 | use oat\tao\model\taskQueue\Task\TaskAwareTrait; |
| 32 | use oat\tao\model\webhooks\configEntity\WebhookAuthInterface; |
| 33 | use oat\tao\model\webhooks\configEntity\WebhookInterface; |
| 34 | use oat\tao\model\webhooks\WebhookRegistryInterface; |
| 35 | use oat\tao\model\webhooks\WebhookTaskServiceInterface; |
| 36 | use Psr\Http\Message\RequestInterface; |
| 37 | use Psr\Http\Message\ResponseInterface; |
| 38 | |
| 39 | class WebhookTask extends AbstractAction implements TaskAwareInterface |
| 40 | { |
| 41 | use TaskAwareTrait; |
| 42 | |
| 43 | /** |
| 44 | * @var WebhookTaskParams |
| 45 | */ |
| 46 | private $params; |
| 47 | |
| 48 | /** |
| 49 | * @param array $paramsArray |
| 50 | * |
| 51 | * @return \common_report_Report |
| 52 | * @throws GuzzleException |
| 53 | */ |
| 54 | public function __invoke($paramsArray) |
| 55 | { |
| 56 | try { |
| 57 | $this->params = $this->getWebhookTaskParamsFactory()->createFromArray($paramsArray); |
| 58 | $webhookConfig = $this->getWebhookConfig(); |
| 59 | $request = $this->prepareRequest($webhookConfig); |
| 60 | return $this->performRequest($request, $webhookConfig->getAuth()); |
| 61 | } catch (\Exception $exception) { |
| 62 | return $this->getWebhookTaskReports()->reportInternalException($this->getTaskContext(), $exception); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @param WebhookInterface $webhookConfig |
| 68 | * |
| 69 | * @return RequestInterface |
| 70 | * @throws \common_Exception |
| 71 | */ |
| 72 | private function prepareRequest(WebhookInterface $webhookConfig) |
| 73 | { |
| 74 | $payloadFactory = $this->getWebhookPayloadFactory(); |
| 75 | |
| 76 | $body = $payloadFactory->createPayload( |
| 77 | $this->params->getEventName(), |
| 78 | $this->params->getEventId(), |
| 79 | $this->params->getTriggeredTimestamp(), |
| 80 | $this->params->getEventData() |
| 81 | ); |
| 82 | |
| 83 | return new Request( |
| 84 | $webhookConfig->getHttpMethod(), |
| 85 | $webhookConfig->getUrl(), |
| 86 | [ |
| 87 | 'Content-Type' => $payloadFactory->getContentType(), |
| 88 | 'Accept' => $this->getWebhookResponseFactory()->getAcceptedContentType(), |
| 89 | ], |
| 90 | $body |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param RequestInterface $request |
| 96 | * @param WebhookAuthInterface|null $authConfig |
| 97 | * |
| 98 | * @return \common_report_Report |
| 99 | * @throws GuzzleException |
| 100 | * @throws \common_exception_InvalidArgumentType |
| 101 | */ |
| 102 | private function performRequest(RequestInterface $request, WebhookAuthInterface $authConfig = null) |
| 103 | { |
| 104 | $errorReport = $response = null; |
| 105 | try { |
| 106 | $response = $this->getWebhookSender()->performRequest($request, $authConfig); |
| 107 | } catch (ServerException $connectException) { |
| 108 | $this->retryTask(); |
| 109 | $errorReport = $this->getWebhookTaskReports()->reportBadResponseException( |
| 110 | $this->getTaskContext(), |
| 111 | $connectException |
| 112 | ); |
| 113 | } catch (BadResponseException $badResponseException) { |
| 114 | $errorReport = $this->getWebhookTaskReports()->reportBadResponseException( |
| 115 | $this->getTaskContext(), |
| 116 | $badResponseException |
| 117 | ); |
| 118 | } catch (ConnectException $connectException) { |
| 119 | $this->retryTask(); |
| 120 | $errorReport = $this->getWebhookTaskReports()->reportConnectException( |
| 121 | $this->getTaskContext(), |
| 122 | $connectException |
| 123 | ); |
| 124 | } catch (RequestException $requestException) { |
| 125 | $errorReport = $this->getWebhookTaskReports()->reportRequestException( |
| 126 | $this->getTaskContext(), |
| 127 | $requestException |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | return $response |
| 132 | ? $this->handleResponse($response) |
| 133 | : $errorReport; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param ResponseInterface $response |
| 138 | * |
| 139 | * @return \common_report_Report |
| 140 | */ |
| 141 | private function handleResponse(ResponseInterface $response) |
| 142 | { |
| 143 | if (!$this->isAcceptableResponseStatusCode($response->getStatusCode())) { |
| 144 | $this->retryTask(); |
| 145 | return $this->getWebhookTaskReports()->reportInvalidStatusCode($this->getTaskContext(), $response); |
| 146 | } |
| 147 | |
| 148 | $parsedResponse = $this->getWebhookResponseFactory()->create($response); |
| 149 | $eventId = $this->params->getEventId(); |
| 150 | |
| 151 | if ($this->params->responseValidation()) { |
| 152 | if ($this->params->getRetryMax() && $parsedResponse->getParseError()) { |
| 153 | return $this->getWebhookTaskReports()->reportInvalidBodyFormat($this->getTaskContext(), $response); |
| 154 | } |
| 155 | |
| 156 | if (!$parsedResponse->isDelivered($eventId)) { |
| 157 | return $this->getWebhookTaskReports()->reportInvalidAcknowledgement( |
| 158 | $this->getTaskContext(), |
| 159 | $response, |
| 160 | $parsedResponse |
| 161 | ); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return $this->getWebhookTaskReports()->reportSuccess( |
| 166 | $this->getTaskContext(), |
| 167 | $response, |
| 168 | $parsedResponse->getStatus($eventId) |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @param int $httpStatusCode |
| 174 | * |
| 175 | * @return bool |
| 176 | */ |
| 177 | private function isAcceptableResponseStatusCode($httpStatusCode) |
| 178 | { |
| 179 | return $httpStatusCode >= 200 && $httpStatusCode < 300; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @return WebhookInterface |
| 184 | * @throws \common_exception_NotFound |
| 185 | */ |
| 186 | private function getWebhookConfig() |
| 187 | { |
| 188 | $webhookConfig = $this->getWebhookRegistry()->getWebhookConfig($this->params->getWebhookConfigId()); |
| 189 | if ($webhookConfig === null) { |
| 190 | throw new \common_exception_NotFound("Webhook config '$webhookConfig' not found"); |
| 191 | } |
| 192 | return $webhookConfig; |
| 193 | } |
| 194 | |
| 195 | private function getWebhookRegistry(): WebhookRegistryInterface |
| 196 | { |
| 197 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 198 | return $this->getServiceLocator()->getContainer()->get(WebhookRegistryInterface::class); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @return WebhookPayloadFactoryInterface |
| 203 | */ |
| 204 | private function getWebhookPayloadFactory() |
| 205 | { |
| 206 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 207 | return $this->getServiceLocator()->get(WebhookPayloadFactoryInterface::SERVICE_ID); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * @return WebhookTaskParamsFactory |
| 212 | */ |
| 213 | private function getWebhookTaskParamsFactory() |
| 214 | { |
| 215 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 216 | return $this->getServiceLocator()->get(WebhookTaskParamsFactory::class); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @return WebhookResponseFactoryInterface |
| 221 | */ |
| 222 | private function getWebhookResponseFactory() |
| 223 | { |
| 224 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 225 | return $this->getServiceLocator()->get(WebhookResponseFactoryInterface::SERVICE_ID); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * @return WebhookSender |
| 230 | */ |
| 231 | private function getWebhookSender() |
| 232 | { |
| 233 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 234 | return $this->getServiceLocator()->get(WebhookSender::class); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @return WebhookTaskServiceInterface |
| 239 | */ |
| 240 | private function getWebhookTaskService() |
| 241 | { |
| 242 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 243 | return $this->getServiceLocator()->get(WebhookTaskServiceInterface::SERVICE_ID); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @return WebhookTaskReports |
| 248 | */ |
| 249 | private function getWebhookTaskReports() |
| 250 | { |
| 251 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 252 | return $this->getServiceLocator()->get(WebhookTaskReports::class); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * @return WebhookTaskContext |
| 257 | */ |
| 258 | private function getTaskContext() |
| 259 | { |
| 260 | $context = new WebhookTaskContext(); |
| 261 | $context->setTaskId($this->getTask()->getId()); |
| 262 | try { |
| 263 | $context->setWebhookConfig($this->getWebhookConfig()); |
| 264 | } catch (\Exception $e) { |
| 265 | } |
| 266 | try { |
| 267 | $context->setWebhookTaskParams($this->params); |
| 268 | } catch (\Exception $e) { |
| 269 | } |
| 270 | return $context; |
| 271 | } |
| 272 | |
| 273 | private function retryTask() |
| 274 | { |
| 275 | if (!$this->params->isMaxRetryCountReached()) { |
| 276 | $this->params->increaseRetryCount(); |
| 277 | $this->getWebhookTaskService()->createTask($this->params); |
| 278 | } |
| 279 | } |
| 280 | } |