Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 51 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| DeliveryExecutionDeleteService | |
0.00% |
0 / 51 |
|
0.00% |
0 / 5 |
182 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| execute | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
12 | |||
| getReport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| deleteDeliveryExecutionData | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| getDeliveryExecutionDeleteService | |
0.00% |
0 / 8 |
|
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) 2018 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoDelivery\model\execution\Delete; |
| 23 | |
| 24 | use common_report_Report; |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use oat\taoDelivery\model\execution\ServiceProxy; |
| 27 | |
| 28 | class DeliveryExecutionDeleteService extends ConfigurableService |
| 29 | { |
| 30 | public const SERVICE_ID = 'taoDelivery/DeliveryExecutionDelete'; |
| 31 | |
| 32 | public const OPTION_DELETE_DELIVERY_EXECUTION_DATA_SERVICES = 'deleteDeliveryExecutionDataServices'; |
| 33 | |
| 34 | /** @var common_report_Report */ |
| 35 | private $report; |
| 36 | |
| 37 | /** |
| 38 | * DeliveryExecutionDeleteService constructor. |
| 39 | * @param array $options |
| 40 | * @throws \common_exception_Error |
| 41 | */ |
| 42 | public function __construct(array $options = []) |
| 43 | { |
| 44 | parent::__construct($options); |
| 45 | |
| 46 | if (!$this->hasOption(static::OPTION_DELETE_DELIVERY_EXECUTION_DATA_SERVICES)) { |
| 47 | throw new \common_exception_Error( |
| 48 | 'Invalid Option provided: ' . static::OPTION_DELETE_DELIVERY_EXECUTION_DATA_SERVICES |
| 49 | ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param DeliveryExecutionDeleteRequest $request |
| 55 | * @return bool |
| 56 | * @throws \Exception |
| 57 | */ |
| 58 | public function execute(DeliveryExecutionDeleteRequest $request) |
| 59 | { |
| 60 | $this->report = common_report_Report::createInfo( |
| 61 | 'Deleting Delivery Execution: ' . $request->getDeliveryExecution()->getIdentifier() |
| 62 | ); |
| 63 | |
| 64 | $shouldDelete = $this->deleteDeliveryExecutionData($request); |
| 65 | |
| 66 | if ($shouldDelete) { |
| 67 | /** @var Service $executionService */ |
| 68 | $executionService = $this->getServiceLocator()->get(ServiceProxy::SERVICE_ID); |
| 69 | // at the end delete the delivery execution itself. |
| 70 | $deleted = $executionService->deleteDeliveryExecutionData($request); |
| 71 | |
| 72 | if ($deleted) { |
| 73 | $this->report->add( |
| 74 | common_report_Report::createSuccess( |
| 75 | 'Delivery Execution has been deleted.', |
| 76 | $request->getDeliveryExecution()->getIdentifier() |
| 77 | ) |
| 78 | ); |
| 79 | } else { |
| 80 | $this->report->add( |
| 81 | common_report_Report::createInfo( |
| 82 | 'Delivery Execution has NOT been deleted. DE id: ' |
| 83 | . $request->getDeliveryExecution()->getIdentifier() |
| 84 | ) |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | return $deleted; |
| 89 | } |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @return common_report_Report |
| 96 | */ |
| 97 | public function getReport() |
| 98 | { |
| 99 | return $this->report; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @param DeliveryExecutionDeleteRequest $request |
| 104 | * @return bool |
| 105 | * @throws \Exception |
| 106 | */ |
| 107 | protected function deleteDeliveryExecutionData(DeliveryExecutionDeleteRequest $request) |
| 108 | { |
| 109 | $services = $this->getDeliveryExecutionDeleteService(); |
| 110 | |
| 111 | foreach ($services as $service) { |
| 112 | try { |
| 113 | $deleted = $service->deleteDeliveryExecutionData($request); |
| 114 | if ($deleted) { |
| 115 | $this->report->add(common_report_Report::createSuccess( |
| 116 | 'Delete execution Service: ' . get_class($service) . ' data has been deleted.', |
| 117 | $request->getDeliveryExecution()->getIdentifier() |
| 118 | )); |
| 119 | } else { |
| 120 | $this->report->add(common_report_Report::createInfo( |
| 121 | 'Delete execution Service: ' . get_class($service) . ' data has nothing to delete' |
| 122 | )); |
| 123 | } |
| 124 | } catch (\Exception $exception) { |
| 125 | $this->report->add(common_report_Report::createFailure($exception->getMessage())); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @return DeliveryExecutionDelete[] |
| 134 | * @throws \common_exception_Error |
| 135 | */ |
| 136 | private function getDeliveryExecutionDeleteService() |
| 137 | { |
| 138 | $services = []; |
| 139 | $servicesStrings = $this->getOption(static::OPTION_DELETE_DELIVERY_EXECUTION_DATA_SERVICES); |
| 140 | foreach ($servicesStrings as $serviceString) { |
| 141 | $deleteService = $this->getServiceLocator()->get($serviceString); |
| 142 | if (!$deleteService instanceof DeliveryExecutionDelete) { |
| 143 | throw new \common_exception_Error('Invalid Delete Service provided: ' . $serviceString); |
| 144 | } |
| 145 | |
| 146 | $services[] = $deleteService; |
| 147 | } |
| 148 | |
| 149 | return $services; |
| 150 | } |
| 151 | } |