Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.67% |
29 / 30 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ResourceRepository | |
96.67% |
29 / 30 |
|
75.00% |
3 / 4 |
6 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| findBy | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| delete | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
3 | |||
| getImplementation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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\Repository; |
| 24 | |
| 25 | use oat\generis\model\data\event\BeforeResourceDeleted; |
| 26 | use RuntimeException; |
| 27 | use BadMethodCallException; |
| 28 | use InvalidArgumentException; |
| 29 | use core_kernel_classes_Class; |
| 30 | use core_kernel_classes_Resource; |
| 31 | use oat\oatbox\event\EventManager; |
| 32 | use oat\generis\model\data\Ontology; |
| 33 | use core_kernel_persistence_ResourceInterface; |
| 34 | use oat\generis\model\Context\ContextInterface; |
| 35 | use oat\generis\model\data\event\ResourceDeleted; |
| 36 | use oat\generis\model\resource\Context\ResourceRepositoryContext; |
| 37 | use oat\generis\model\resource\Contract\ResourceRepositoryInterface; |
| 38 | |
| 39 | class ResourceRepository implements ResourceRepositoryInterface |
| 40 | { |
| 41 | /** @var Ontology */ |
| 42 | private $ontology; |
| 43 | |
| 44 | /** @var EventManager */ |
| 45 | private $eventManager; |
| 46 | |
| 47 | public function __construct(Ontology $ontology, EventManager $eventManager) |
| 48 | { |
| 49 | $this->ontology = $ontology; |
| 50 | $this->eventManager = $eventManager; |
| 51 | } |
| 52 | |
| 53 | public function findBy(ContextInterface $context): array |
| 54 | { |
| 55 | throw new BadMethodCallException(sprintf('Method %s not implemented.', __METHOD__)); |
| 56 | } |
| 57 | |
| 58 | public function delete(ContextInterface $context): void |
| 59 | { |
| 60 | /** @var core_kernel_classes_Resource|null $resource */ |
| 61 | $resource = $context->getParameter(ResourceRepositoryContext::PARAM_RESOURCE); |
| 62 | |
| 63 | if ($resource === null) { |
| 64 | throw new InvalidArgumentException('Resource was not provided for deletion.'); |
| 65 | } |
| 66 | |
| 67 | $deleteReference = $context->getParameter( |
| 68 | ResourceRepositoryContext::PARAM_DELETE_REFERENCE, |
| 69 | false |
| 70 | ); |
| 71 | |
| 72 | $beforeResourceDeletedEvent = new BeforeResourceDeleted($resource->getUri()); |
| 73 | $this->eventManager->trigger($beforeResourceDeletedEvent); |
| 74 | $resourceType = $resource->getTypes() ?? []; |
| 75 | $resourceType = reset($resourceType); |
| 76 | |
| 77 | if (!$this->getImplementation()->delete($resource, $deleteReference)) { |
| 78 | throw new RuntimeException( |
| 79 | sprintf( |
| 80 | 'Resource "%s" ("%s") was not deleted.', |
| 81 | $resource->getLabel(), |
| 82 | $resource->getUri() |
| 83 | ) |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | /** @var core_kernel_classes_Class|null $selectedClass */ |
| 88 | $selectedClass = $context->getParameter(ResourceRepositoryContext::PARAM_SELECTED_CLASS); |
| 89 | /** @var core_kernel_classes_Class|null $selectedClass */ |
| 90 | $parentClass = $context->getParameter(ResourceRepositoryContext::PARAM_PARENT_CLASS); |
| 91 | $resourceDeletedEvent = (new ResourceDeleted($resource->getUri())) |
| 92 | ->setSelectedClass($selectedClass) |
| 93 | ->setParentClass($parentClass) |
| 94 | ->setResourceType($resourceType->getUri()); |
| 95 | |
| 96 | $this->eventManager->trigger($resourceDeletedEvent); |
| 97 | } |
| 98 | |
| 99 | private function getImplementation(): core_kernel_persistence_ResourceInterface |
| 100 | { |
| 101 | return $this->ontology->getRdfsInterface()->getResourceImplementation(); |
| 102 | } |
| 103 | } |