Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
66 / 77 |
|
50.00% |
6 / 12 |
CRAP | |
0.00% |
0 / 1 |
| WebHookClassService | |
85.71% |
66 / 77 |
|
50.00% |
6 / 12 |
25.68 | |
0.00% |
0 / 1 |
| getRootClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getWebhookByUri | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| findWebhookByEventClass | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| getWebhooks | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| saveWebhook | |
85.00% |
17 / 20 |
|
0.00% |
0 / 1 |
6.12 | |||
| saveWebhookInstance | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getCurrentClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDataBinder | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAuth | |
66.67% |
8 / 12 |
|
0.00% |
0 / 1 |
2.15 | |||
| rdfInstanceToEntity | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
2.00 | |||
| getWebhookEntryFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getWebhookAuthService | |
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) 2023 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\webhooks; |
| 24 | |
| 25 | use common_Exception; |
| 26 | use common_exception_Error; |
| 27 | use common_exception_InvalidArgumentType; |
| 28 | use core_kernel_classes_Class; |
| 29 | use core_kernel_classes_Resource; |
| 30 | use core_kernel_persistence_Exception; |
| 31 | use Laminas\ServiceManager\ServiceLocatorAwareInterface; |
| 32 | use Laminas\ServiceManager\ServiceLocatorAwareTrait; |
| 33 | use oat\tao\model\auth\AuthUriClassMapper; |
| 34 | use oat\tao\model\auth\BasicAuth; |
| 35 | use oat\tao\model\auth\BasicAuthCredentials; |
| 36 | use oat\tao\model\OntologyClassService; |
| 37 | use oat\tao\model\webhooks\configEntity\WebhookAuth; |
| 38 | use oat\tao\model\webhooks\configEntity\WebhookEntryFactory; |
| 39 | use oat\tao\model\webhooks\configEntity\WebhookInterface; |
| 40 | use tao_models_classes_dataBinding_GenerisFormDataBinder as DataBinder; |
| 41 | |
| 42 | class WebHookClassService extends OntologyClassService implements ServiceLocatorAwareInterface |
| 43 | { |
| 44 | use ServiceLocatorAwareTrait; |
| 45 | |
| 46 | public const CLASS_URI = 'http://www.tao.lu/Ontologies/TAO.rdf#WebHook'; |
| 47 | public const PROPERTY_AUTH_TYPE = 'http://www.tao.lu/Ontologies/TAO.rdf#WebHookAuthType'; |
| 48 | public const PROPERTY_WEB_HOOK_URL = 'http://www.tao.lu/Ontologies/TAO.rdf#WebHookUrl'; |
| 49 | public const PROPERTY_WEB_HOOK_METHOD = 'http://www.tao.lu/Ontologies/TAO.rdf#WebHookMethod'; |
| 50 | public const PROPERTY_WEB_HOOK_RETRY = 'http://www.tao.lu/Ontologies/TAO.rdf#WebHookRetry'; |
| 51 | public const PROPERTY_RESPONSE_VALIDATION = 'http://www.tao.lu/Ontologies/TAO.rdf#WebHookResponseValidation'; |
| 52 | public const PROPERTY_WEBHOOK_EVENT = 'http://www.tao.lu/Ontologies/TAO.rdf#WebhookEvent'; |
| 53 | public const RDF_HTTP_METHOD_POST = 'http://www.tao.lu/Ontologies/TAO.rdf#HTTPMethodPOST'; |
| 54 | public const RDF_HTTP_METHOD_GET = 'http://www.tao.lu/Ontologies/TAO.rdf#HTTPMethodGET'; |
| 55 | public const RDF_COMPLY_ENABLED = 'http://www.tao.lu/Ontologies/TAO.rdf#ComplyEnabled'; |
| 56 | |
| 57 | |
| 58 | public function getRootClass(): core_kernel_classes_Class |
| 59 | { |
| 60 | return new core_kernel_classes_Class(self::CLASS_URI); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @throws core_kernel_persistence_Exception |
| 65 | * @throws common_Exception |
| 66 | */ |
| 67 | public function getWebhookByUri(string $uri): ?WebhookInterface |
| 68 | { |
| 69 | $webhookClass = $this->getModel()->getResource($uri); |
| 70 | |
| 71 | if ($webhookClass === null) { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | return $this->rdfInstanceToEntity($webhookClass); |
| 76 | } |
| 77 | |
| 78 | public function findWebhookByEventClass(string $eventClassName): array |
| 79 | { |
| 80 | $rootClass = $this->getRootClass(); |
| 81 | $webhookInstances = $rootClass->getInstances(false); |
| 82 | |
| 83 | $result = []; |
| 84 | |
| 85 | foreach ($webhookInstances as $webhookInstance) { |
| 86 | $events = $webhookInstance->getPropertyValues($this->getProperty(self::PROPERTY_WEBHOOK_EVENT)); |
| 87 | if (in_array($eventClassName, $events, true)) { |
| 88 | $result[] = $webhookInstance; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return $result; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @throws core_kernel_persistence_Exception |
| 97 | * @throws common_exception_InvalidArgumentType |
| 98 | * @throws common_Exception |
| 99 | */ |
| 100 | public function getWebhooks(): array |
| 101 | { |
| 102 | $rootClass = $this->getRootClass(); |
| 103 | $webhookInstances = $rootClass->getInstances(false); |
| 104 | |
| 105 | $result = []; |
| 106 | foreach ($webhookInstances as $webhookInstance) { |
| 107 | $result[] = $this->rdfInstanceToEntity($webhookInstance); |
| 108 | } |
| 109 | |
| 110 | return $result; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @throws common_exception_Error |
| 115 | * @throws common_Exception |
| 116 | */ |
| 117 | public function saveWebhook(WebhookInterface $webhook, array $events): void |
| 118 | { |
| 119 | $values = [ |
| 120 | self::PROPERTY_WEB_HOOK_URL => $webhook->getUrl(), |
| 121 | self::PROPERTY_WEB_HOOK_RETRY => (string)$webhook->getMaxRetries(), |
| 122 | self::PROPERTY_WEB_HOOK_METHOD => $webhook->getHttpMethod() === 'POST' |
| 123 | ? self::RDF_HTTP_METHOD_POST |
| 124 | : self::RDF_HTTP_METHOD_GET |
| 125 | ]; |
| 126 | |
| 127 | if ($webhook->getResponseValidationEnable()) { |
| 128 | $values[self::PROPERTY_RESPONSE_VALIDATION] = |
| 129 | self::RDF_COMPLY_ENABLED; |
| 130 | } |
| 131 | |
| 132 | $uriClassMapper = new AuthUriClassMapper(); |
| 133 | $authTypeUri = $uriClassMapper->getUriByClass($webhook->getAuth()->getAuthClass()); |
| 134 | $values[self::PROPERTY_AUTH_TYPE] = $authTypeUri; |
| 135 | |
| 136 | if ($authTypeUri === BasicAuth::CLASS_BASIC_AUTH) { |
| 137 | $credentials = $webhook->getAuth()->getCredentials(); |
| 138 | $values[BasicAuth::PROPERTY_LOGIN] = $credentials[BasicAuthCredentials::LOGIN]; |
| 139 | $values[BasicAuth::PROPERTY_PASSWORD] = $credentials[BasicAuthCredentials::PASSWORD]; |
| 140 | } |
| 141 | |
| 142 | if (!empty($events)) { |
| 143 | $values[self::PROPERTY_WEBHOOK_EVENT] = []; |
| 144 | foreach ($events as $eventClass) { |
| 145 | $values[self::PROPERTY_WEBHOOK_EVENT][] = $eventClass; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | $this->saveWebhookInstance($values); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @throws common_exception_Error |
| 154 | * @throws common_Exception |
| 155 | */ |
| 156 | public function saveWebhookInstance(array $parameters, ?core_kernel_classes_Resource $instance = null): void |
| 157 | { |
| 158 | if (!$instance) { |
| 159 | $currentClass = $this->getCurrentClass(); |
| 160 | |
| 161 | $instance = $this->createInstance($currentClass); |
| 162 | } |
| 163 | |
| 164 | // save properties |
| 165 | $this->getDataBinder($instance)->bind($parameters); |
| 166 | } |
| 167 | |
| 168 | protected function getCurrentClass(): ?core_kernel_classes_Class |
| 169 | { |
| 170 | return $this->getModel()->getClass(self::CLASS_URI); |
| 171 | } |
| 172 | |
| 173 | protected function getDataBinder(core_kernel_classes_Resource $targetInstance): DataBinder |
| 174 | { |
| 175 | return new DataBinder($targetInstance); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @throws core_kernel_persistence_Exception |
| 180 | * @throws common_Exception |
| 181 | */ |
| 182 | private function getAuth(core_kernel_classes_Resource $webhookClass): WebhookAuth |
| 183 | { |
| 184 | /** @var WebhookAuthService $webhookAuthService */ |
| 185 | $webhookAuthService = $this->getServiceLocator()->getContainer()->get(WebhookAuthService::class); |
| 186 | $authType = $webhookAuthService->getAuthType( |
| 187 | $webhookClass->getOnePropertyValue($this->getProperty(self::PROPERTY_AUTH_TYPE)) |
| 188 | ); |
| 189 | |
| 190 | $authProperties = $authType->getAuthProperties(); |
| 191 | $extractedAuthProperties = []; |
| 192 | foreach ($authProperties as $authProperty) { |
| 193 | $propertyLabel = strtolower($authProperty->getLabel()); |
| 194 | $extractedAuthProperties[$propertyLabel] = (string)$webhookClass->getOnePropertyValue( |
| 195 | $this->getProperty($authProperty->getUri()) |
| 196 | ); |
| 197 | } |
| 198 | |
| 199 | return $this->getWebhookEntryFactory()->createAuthEntry(get_class($authType), $extractedAuthProperties); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @param core_kernel_classes_Resource $webhookResource |
| 204 | * |
| 205 | * @return WebhookInterface |
| 206 | * |
| 207 | * @throws common_Exception |
| 208 | * @throws common_exception_InvalidArgumentType |
| 209 | * @throws core_kernel_persistence_Exception |
| 210 | */ |
| 211 | private function rdfInstanceToEntity(core_kernel_classes_Resource $webhookResource): WebhookInterface |
| 212 | { |
| 213 | $properties = $webhookResource->getPropertiesValues([ |
| 214 | self::PROPERTY_WEB_HOOK_URL, |
| 215 | self::PROPERTY_WEB_HOOK_METHOD, |
| 216 | self::PROPERTY_WEB_HOOK_RETRY, |
| 217 | self::PROPERTY_AUTH_TYPE, |
| 218 | self::PROPERTY_RESPONSE_VALIDATION |
| 219 | ]); |
| 220 | |
| 221 | $responseValidation = empty($properties[self::PROPERTY_RESPONSE_VALIDATION]) |
| 222 | ? '' |
| 223 | : $properties[self::PROPERTY_RESPONSE_VALIDATION][0]->getLabel(); |
| 224 | |
| 225 | return $this->getWebhookEntryFactory()->createEntry( |
| 226 | $webhookResource->getUri(), |
| 227 | $properties[self::PROPERTY_WEB_HOOK_URL][0]->__toString(), |
| 228 | $properties[self::PROPERTY_WEB_HOOK_METHOD][0]->getLabel(), |
| 229 | (int)$properties[self::PROPERTY_WEB_HOOK_RETRY][0]->__toString(), |
| 230 | $this->getAuth($webhookResource), |
| 231 | $responseValidation === 'Enable', |
| 232 | ); |
| 233 | } |
| 234 | |
| 235 | private function getWebhookEntryFactory(): WebhookEntryFactory |
| 236 | { |
| 237 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 238 | return $this->getServiceLocator()->get(WebhookEntryFactory::class); |
| 239 | } |
| 240 | |
| 241 | private function getWebhookAuthService(): WebhookAuthService |
| 242 | { |
| 243 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 244 | return $this->getServiceLocator()->get(WebhookAuthService::class); |
| 245 | } |
| 246 | } |