Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
77.27% |
34 / 44 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| WebhookEntryFactory | |
77.27% |
34 / 44 |
|
66.67% |
4 / 6 |
11.17 | |
0.00% |
0 / 1 |
| createEntry | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| createAuthEntry | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createEntryFromArray | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
4 | |||
| createAuthEntryFromArray | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| getWebhookEntryValidator | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getWebhookAuthValidator | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 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\configEntity; |
| 22 | |
| 23 | use oat\oatbox\service\ConfigurableService; |
| 24 | use oat\tao\helpers\ArrayValidator; |
| 25 | |
| 26 | class WebhookEntryFactory extends ConfigurableService |
| 27 | { |
| 28 | public function createEntry( |
| 29 | string $id, |
| 30 | string $url, |
| 31 | string $httpMethod, |
| 32 | int $retryMax, |
| 33 | WebhookAuth $auth = null, |
| 34 | bool $responseValidation = true, |
| 35 | array $extraPayload = [] |
| 36 | ): WebhookInterface { |
| 37 | return new Webhook( |
| 38 | $id, |
| 39 | $url, |
| 40 | $httpMethod, |
| 41 | $retryMax, |
| 42 | $auth, |
| 43 | $responseValidation, |
| 44 | $extraPayload |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | public function createAuthEntry(string $authClass, array $properties): WebhookAuth |
| 49 | { |
| 50 | return new WebhookAuth($authClass, $properties); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param array $data |
| 55 | * |
| 56 | * @return Webhook |
| 57 | */ |
| 58 | public function createEntryFromArray(array $data) |
| 59 | { |
| 60 | $validator = $this->getWebhookEntryValidator(); |
| 61 | if (!$validator->validate($data)) { |
| 62 | throw new \InvalidArgumentException($validator->getErrorMessage()); |
| 63 | } |
| 64 | |
| 65 | $auth = isset($data[Webhook::AUTH]) |
| 66 | ? $this->createAuthEntryFromArray($data[Webhook::AUTH]) |
| 67 | : null; |
| 68 | |
| 69 | /* |
| 70 | * default value for validation for back compatibility, |
| 71 | * because old webhooks are nod updated and may not contain this new parameter |
| 72 | */ |
| 73 | $responseValidation = true; |
| 74 | if (array_key_exists(Webhook::RESPONSE_VALIDATION, $data)) { |
| 75 | $responseValidation = $data[Webhook::RESPONSE_VALIDATION]; |
| 76 | } |
| 77 | |
| 78 | return new Webhook( |
| 79 | $data[Webhook::ID], |
| 80 | $data[Webhook::URL], |
| 81 | $data[Webhook::HTTP_METHOD], |
| 82 | $data[Webhook::RETRY_MAX], |
| 83 | $auth, |
| 84 | $responseValidation, |
| 85 | $data[Webhook::EXTRA_PAYLOAD] ?? [] |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @param array $data |
| 91 | * |
| 92 | * @return WebhookAuth |
| 93 | */ |
| 94 | protected function createAuthEntryFromArray(array $data) |
| 95 | { |
| 96 | $validator = $this->getWebhookAuthValidator(); |
| 97 | if (!$validator->validate($data)) { |
| 98 | throw new \InvalidArgumentException( |
| 99 | 'Invalid ' . Webhook::AUTH . ' config: ' . $validator->getErrorMessage() |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | return new WebhookAuth( |
| 104 | $data[WebhookAuth::AUTH_CLASS], |
| 105 | $data[WebhookAuth::CREDENTIALS] |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return ArrayValidator |
| 111 | */ |
| 112 | private function getWebhookEntryValidator() |
| 113 | { |
| 114 | return (new ArrayValidator()) |
| 115 | ->assertString([Webhook::ID, Webhook::URL, Webhook::HTTP_METHOD]) |
| 116 | ->assertInt([Webhook::RETRY_MAX]) |
| 117 | ->assertArray(Webhook::AUTH, false, true); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @return ArrayValidator |
| 122 | */ |
| 123 | private function getWebhookAuthValidator() |
| 124 | { |
| 125 | return (new ArrayValidator()) |
| 126 | ->assertString(WebhookAuth::AUTH_CLASS) |
| 127 | ->assertArray(WebhookAuth::CREDENTIALS); |
| 128 | } |
| 129 | } |