Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| DeliveryService | |
0.00% |
0 / 34 |
|
0.00% |
0 / 6 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| update | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| setProperties | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| assignPropertyValue | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| assignValueToFormElement | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| handleValidationErrors | |
0.00% |
0 / 6 |
|
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) 2022 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * @author Sergei Mikhailov <sergei.mikhailov@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\taoDeliveryRdf\model\Delivery\Business\Service; |
| 26 | |
| 27 | use common_exception_ResourceNotFound as ResourceNotFoundException; |
| 28 | use core_kernel_classes_Resource as KernelResource; |
| 29 | use DomainException; |
| 30 | use oat\oatbox\event\EventManager; |
| 31 | use oat\taoDeliveryRdf\model\Delivery\Business\Input\DeliveryUpdateInput; |
| 32 | use oat\taoDeliveryRdf\model\Delivery\DataAccess\DeliveryRepository; |
| 33 | use oat\taoDeliveryRdf\model\Delivery\Presentation\Web\Form\DeliveryFormFactory; |
| 34 | use oat\taoDeliveryRdf\model\event\DeliveryUpdatedEvent; |
| 35 | use RuntimeException; |
| 36 | use tao_helpers_form_elements_MultipleElement as MultipleValueElement; |
| 37 | use tao_helpers_form_Form as Form; |
| 38 | use tao_helpers_Uri as UriHelper; |
| 39 | use tao_models_classes_dataBinding_GenerisFormDataBinder as FormDataBinder; |
| 40 | use tao_models_classes_dataBinding_GenerisFormDataBindingException as FormDataBindingException; |
| 41 | |
| 42 | class DeliveryService |
| 43 | { |
| 44 | /** @var DeliveryRepository */ |
| 45 | private $repository; |
| 46 | |
| 47 | /** @var DeliveryFormFactory */ |
| 48 | private $formFactory; |
| 49 | |
| 50 | /** @var EventManager */ |
| 51 | private $eventManager; |
| 52 | |
| 53 | public function __construct( |
| 54 | DeliveryRepository $repository, |
| 55 | DeliveryFormFactory $formFactory, |
| 56 | EventManager $eventManager |
| 57 | ) { |
| 58 | $this->repository = $repository; |
| 59 | $this->formFactory = $formFactory; |
| 60 | $this->eventManager = $eventManager; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @throws ResourceNotFoundException |
| 65 | * @throws DomainException |
| 66 | * @throws FormDataBindingException |
| 67 | */ |
| 68 | public function update(DeliveryUpdateInput $input): KernelResource |
| 69 | { |
| 70 | $delivery = $this->repository->findOrFail( |
| 71 | $input->getSearchRequest() |
| 72 | ); |
| 73 | |
| 74 | /** @var Form $form */ |
| 75 | $form = $this->formFactory->create($delivery)->getForm(); |
| 76 | $this->setProperties($input, $form); |
| 77 | |
| 78 | $propertyValues = $form->getValues(); |
| 79 | (new FormDataBinder($delivery))->bind($propertyValues); |
| 80 | $this->eventManager->trigger(new DeliveryUpdatedEvent($delivery->getUri(), $propertyValues)); |
| 81 | |
| 82 | return $delivery; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @throws RuntimeException |
| 87 | */ |
| 88 | private function setProperties(DeliveryUpdateInput $input, Form $form): void |
| 89 | { |
| 90 | $errors = []; |
| 91 | |
| 92 | foreach ($input->getProperties() as $property => $value) { |
| 93 | $errors[$this->assignPropertyValue($form, $property, $value)] = $property; |
| 94 | } |
| 95 | |
| 96 | $this->handleValidationErrors($errors); |
| 97 | } |
| 98 | |
| 99 | private function assignPropertyValue(Form $form, string $property, $value): string |
| 100 | { |
| 101 | $formElement = $form->getElement(UriHelper::encode($property)); |
| 102 | |
| 103 | if (null === $formElement) { |
| 104 | return ''; |
| 105 | } |
| 106 | |
| 107 | $this->assignValueToFormElement($formElement, $value); |
| 108 | $formElement->validate(); |
| 109 | |
| 110 | return $formElement->getError(); |
| 111 | } |
| 112 | |
| 113 | private function assignValueToFormElement(\tao_helpers_form_FormElement $formElement, $value): void |
| 114 | { |
| 115 | if (!$formElement instanceof MultipleValueElement) { |
| 116 | $formElement->setValue($value); |
| 117 | |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | $formElement->setValues([]); |
| 122 | if ($value) { |
| 123 | $formElement->setValue($value); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @throws RuntimeException |
| 129 | */ |
| 130 | private function handleValidationErrors(array $errors): void |
| 131 | { |
| 132 | $validationErrors = []; |
| 133 | |
| 134 | unset($errors['']); |
| 135 | foreach ($errors as $error => $property) { |
| 136 | $validationErrors[] = "[$property] $error"; |
| 137 | } |
| 138 | |
| 139 | if ($validationErrors) { |
| 140 | throw new RuntimeException(implode('; ', $validationErrors)); |
| 141 | } |
| 142 | } |
| 143 | } |