Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
32.14% |
9 / 28 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| WebhookFileRegistry | |
32.14% |
9 / 28 |
|
40.00% |
2 / 5 |
56.99 | |
0.00% |
0 / 1 |
| getWebhookConfig | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
| getWebhookConfigIds | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getWebhookEntryFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getWebhooks | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| addWebhook | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| 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; |
| 22 | |
| 23 | use oat\oatbox\service\ConfigurableService; |
| 24 | use oat\tao\model\webhooks\configEntity\Webhook; |
| 25 | use oat\tao\model\webhooks\configEntity\WebhookEntryFactory; |
| 26 | use oat\tao\model\webhooks\configEntity\WebhookInterface; |
| 27 | use oat\tao\model\webhooks\task\WebhookTaskParams; |
| 28 | |
| 29 | /** |
| 30 | * Implementation which uses own (service) configuration to store webhooks configuration |
| 31 | * |
| 32 | * Options: |
| 33 | * `events` is array with: |
| 34 | * key: name of event |
| 35 | * value: array of string ids of connected webhooks |
| 36 | * |
| 37 | * `webhooks` is array with: |
| 38 | * key: webhook unique id |
| 39 | * value: array representation of ConfigEntry\Webhook |
| 40 | * |
| 41 | * See tao/test/unit/webhooks/WebhookConfigFileRepositoryTest.php |
| 42 | * for example of configuration |
| 43 | */ |
| 44 | class WebhookFileRegistry extends ConfigurableService implements WebhookRegistryInterface |
| 45 | { |
| 46 | public const OPTION_WEBHOOKS = 'webhooks'; |
| 47 | public const OPTION_EVENTS = 'events'; |
| 48 | |
| 49 | /** |
| 50 | * @param string $id |
| 51 | * @return Webhook|null |
| 52 | */ |
| 53 | public function getWebhookConfig($id) |
| 54 | { |
| 55 | $webhooks = $this->getOption(self::OPTION_WEBHOOKS); |
| 56 | if (!isset($webhooks[$id])) { |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | try { |
| 61 | return $this->getWebhookEntryFactory()->createEntryFromArray($webhooks[$id]); |
| 62 | } catch (\InvalidArgumentException $exception) { |
| 63 | throw new \InvalidArgumentException("Invalid '$id' webhook config. " . $exception->getMessage()); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param string $eventName |
| 69 | * @return string[] |
| 70 | */ |
| 71 | public function getWebhookConfigIds($eventName) |
| 72 | { |
| 73 | $events = $this->getOption(self::OPTION_EVENTS); |
| 74 | |
| 75 | return isset($events[$eventName]) |
| 76 | ? $events[$eventName] |
| 77 | : []; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @return WebhookEntryFactory |
| 82 | */ |
| 83 | private function getWebhookEntryFactory() |
| 84 | { |
| 85 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 86 | return $this->getServiceLocator()->get(WebhookEntryFactory::class); |
| 87 | } |
| 88 | |
| 89 | public function getWebhooks(): array |
| 90 | { |
| 91 | $config = $this->getOption(self::OPTION_WEBHOOKS); |
| 92 | if (!is_array($config)) { |
| 93 | return []; |
| 94 | } |
| 95 | |
| 96 | $entityFactory = $this->getWebhookEntryFactory(); |
| 97 | $result = []; |
| 98 | foreach ($config as $value) { |
| 99 | $result[] = $entityFactory->createEntryFromArray($value); |
| 100 | } |
| 101 | |
| 102 | return $result; |
| 103 | } |
| 104 | |
| 105 | public function addWebhook(WebhookInterface $webhook, array $events = []): void |
| 106 | { |
| 107 | $configWebhooks = $this->getOption(self::OPTION_WEBHOOKS, []); |
| 108 | |
| 109 | $configWebhooks[$webhook->getId()] = $webhook->toArray(); |
| 110 | |
| 111 | $this->setOption(self::OPTION_WEBHOOKS, $configWebhooks); |
| 112 | |
| 113 | if (!empty($events)) { |
| 114 | $configEvents = $this->getOption(self::OPTION_EVENTS, []); |
| 115 | foreach ($events as $event) { |
| 116 | $configEvents[$event] = $webhook->getId(); |
| 117 | $this->setOption(self::OPTION_EVENTS, $configEvents); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | $this->registerService(WebhookRegistryInterface::SERVICE_ID, $this); |
| 122 | } |
| 123 | } |