Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
ResourceDeleter | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
delete | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
getParentClass | |
100.00% |
4 / 4 |
|
100.00% |
1 / 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) 2021 (original work) Open Assessment Technologies SA. |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\generis\model\resource\Service; |
24 | |
25 | use core_kernel_classes_Class; |
26 | use core_kernel_classes_Resource; |
27 | use oat\generis\model\OntologyRdf; |
28 | use oat\generis\model\resource\Context\ResourceRepositoryContext; |
29 | use oat\generis\model\resource\Contract\ResourceDeleterInterface; |
30 | use oat\generis\model\resource\Contract\ResourceRepositoryInterface; |
31 | use oat\generis\model\resource\exception\ResourceDeletionException; |
32 | use Throwable; |
33 | |
34 | class ResourceDeleter implements ResourceDeleterInterface |
35 | { |
36 | /** @var ResourceRepositoryInterface */ |
37 | private $resourceRepository; |
38 | |
39 | public function __construct(ResourceRepositoryInterface $resourceRepository) |
40 | { |
41 | $this->resourceRepository = $resourceRepository; |
42 | } |
43 | |
44 | public function delete(core_kernel_classes_Resource $resource): void |
45 | { |
46 | try { |
47 | $context = new ResourceRepositoryContext([ResourceRepositoryContext::PARAM_RESOURCE => $resource]); |
48 | $parentClass = $this->getParentClass($resource); |
49 | |
50 | if ($parentClass !== null) { |
51 | $context->setParameter(ResourceRepositoryContext::PARAM_PARENT_CLASS, $parentClass); |
52 | } |
53 | |
54 | $this->resourceRepository->delete($context); |
55 | } catch (Throwable $exception) { |
56 | throw new ResourceDeletionException( |
57 | sprintf( |
58 | 'Unable to delete resource "%s::%s" (%s).', |
59 | $resource->getLabel(), |
60 | $resource->getUri(), |
61 | $exception->getMessage() |
62 | ), |
63 | __('Unable to delete the selected resource') |
64 | ); |
65 | } |
66 | } |
67 | |
68 | private function getParentClass(core_kernel_classes_Resource $resource): ?core_kernel_classes_Class |
69 | { |
70 | $parentClassUri = $resource->getOnePropertyValue($resource->getProperty(OntologyRdf::RDF_TYPE)); |
71 | |
72 | return $parentClassUri !== null |
73 | ? $resource->getClass($parentClassUri) |
74 | : null; |
75 | } |
76 | } |