Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 47 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| WebhookRdsEventLogService | |
0.00% |
0 / 47 |
|
0.00% |
0 / 8 |
110 | |
0.00% |
0 / 1 |
| storeNetworkErrorLog | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| storeInvalidHttpStatusLog | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| storeInvalidBodyFormat | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| storeInvalidAcknowledgementLog | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| storeSuccessfulLog | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| storeInternalErrorLog | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| applyContext | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| getRepository | |
0.00% |
0 / 1 |
|
0.00% |
0 / 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\log; |
| 22 | |
| 23 | use oat\oatbox\service\ConfigurableService; |
| 24 | use oat\tao\model\webhooks\task\WebhookTaskContext; |
| 25 | use Slim\Http\StatusCode; |
| 26 | |
| 27 | class WebhookRdsEventLogService extends ConfigurableService implements WebhookEventLogInterface |
| 28 | { |
| 29 | /** |
| 30 | * @inheritDoc |
| 31 | * @throws \Exception |
| 32 | */ |
| 33 | public function storeNetworkErrorLog(WebhookTaskContext $webhookTaskContext, $networkError = null) |
| 34 | { |
| 35 | $record = $this->applyContext($webhookTaskContext) |
| 36 | ->setResultMessage(sprintf('Network error: %s', $networkError)) |
| 37 | ->setResult(WebhookEventLogRecord::RESULT_NETWORK_ERROR); |
| 38 | |
| 39 | $this->getRepository()->storeLog($record); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @inheritDoc |
| 44 | * @throws \Exception |
| 45 | */ |
| 46 | public function storeInvalidHttpStatusLog( |
| 47 | WebhookTaskContext $webhookTaskContext, |
| 48 | $actualHttpStatusCode, |
| 49 | $responseBody = null |
| 50 | ) { |
| 51 | $record = $this->applyContext($webhookTaskContext) |
| 52 | ->setHttpStatusCode($actualHttpStatusCode) |
| 53 | ->setResponseBody($responseBody) |
| 54 | ->setResultMessage(sprintf('HTTP status code %d unexpected', $actualHttpStatusCode)) |
| 55 | ->setResult(WebhookEventLogRecord::RESULT_INVALID_HTTP_STATUS); |
| 56 | |
| 57 | $this->getRepository()->storeLog($record); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @inheritDoc |
| 62 | * @throws \Exception |
| 63 | */ |
| 64 | public function storeInvalidBodyFormat(WebhookTaskContext $webhookTaskContext, $responseBody = null) |
| 65 | { |
| 66 | $record = $this->applyContext($webhookTaskContext) |
| 67 | ->setHttpStatusCode(StatusCode::HTTP_OK) |
| 68 | ->setResultMessage(sprintf('Invalid body format')) |
| 69 | ->setResponseBody($responseBody) |
| 70 | ->setResult(WebhookEventLogRecord::RESULT_INVALID_BODY_FORMAT); |
| 71 | |
| 72 | $this->getRepository()->storeLog($record); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @inheritDoc |
| 77 | * @throws \Exception |
| 78 | */ |
| 79 | public function storeInvalidAcknowledgementLog( |
| 80 | WebhookTaskContext $webhookTaskContext, |
| 81 | $responseBody, |
| 82 | $actualAcknowledgement = null |
| 83 | ) { |
| 84 | $record = $this->applyContext($webhookTaskContext) |
| 85 | ->setHttpStatusCode(StatusCode::HTTP_OK) |
| 86 | ->setResponseBody($responseBody) |
| 87 | ->setAcknowledgementStatus($actualAcknowledgement) |
| 88 | ->setResultMessage(sprintf('Acknowledgement "%s" unexpected', $actualAcknowledgement)) |
| 89 | ->setResult(WebhookEventLogRecord::RESULT_INVALID_HTTP_STATUS); |
| 90 | |
| 91 | $this->getRepository()->storeLog($record); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @inheritDoc |
| 96 | * @throws \Exception |
| 97 | */ |
| 98 | public function storeSuccessfulLog(WebhookTaskContext $webhookTaskContext, $responseBody, $acknowledgement) |
| 99 | { |
| 100 | $record = $this->applyContext($webhookTaskContext) |
| 101 | ->setHttpStatusCode(StatusCode::HTTP_OK) |
| 102 | ->setResponseBody($responseBody) |
| 103 | ->setAcknowledgementStatus($acknowledgement) |
| 104 | ->setResultMessage('OK') |
| 105 | ->setResult(WebhookEventLogRecord::RESULT_OK); |
| 106 | |
| 107 | $this->getRepository()->storeLog($record); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @inheritDoc |
| 112 | * @throws \Exception |
| 113 | */ |
| 114 | public function storeInternalErrorLog(WebhookTaskContext $webhookTaskContext, $internalError = null) |
| 115 | { |
| 116 | $record = $this->applyContext($webhookTaskContext) |
| 117 | ->setResultMessage(sprintf('Internal error: %s', $internalError)) |
| 118 | ->setResult(WebhookEventLogRecord::RESULT_INTERNAL_ERROR); |
| 119 | |
| 120 | $this->getRepository()->storeLog($record); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @param WebhookTaskContext $webhookTaskContext |
| 125 | * @return WebhookEventLogRecord |
| 126 | * @throws \Exception |
| 127 | */ |
| 128 | private function applyContext(WebhookTaskContext $webhookTaskContext) |
| 129 | { |
| 130 | $record = new WebhookEventLogRecord(); |
| 131 | |
| 132 | $createdAt = new \DateTimeImmutable(); |
| 133 | |
| 134 | $record |
| 135 | ->setTaskId($webhookTaskContext->getTaskId()) |
| 136 | ->setCreatedAt($createdAt->getTimestamp()); |
| 137 | |
| 138 | if ($taskParams = $webhookTaskContext->getWebhookTaskParams()) { |
| 139 | $record->setEventId($taskParams->getEventId()); |
| 140 | $record->setEventName($taskParams->getEventName()); |
| 141 | } |
| 142 | |
| 143 | if ($webhookConfig = $webhookTaskContext->getWebhookConfig()) { |
| 144 | $record->setHttpMethod($webhookConfig->getHttpMethod()); |
| 145 | $record->setEndpointUrl($webhookConfig->getUrl()); |
| 146 | } |
| 147 | |
| 148 | return $record; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @return WebhookLogRepository |
| 153 | */ |
| 154 | private function getRepository() |
| 155 | { |
| 156 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 157 | return $this->getServiceLocator()->get(WebhookLogRepository::SERVICE_ID); |
| 158 | } |
| 159 | } |