Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.30% covered (success)
96.30%
26 / 27
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ResourceRepository
96.30% covered (success)
96.30%
26 / 27
75.00% covered (warning)
75.00%
3 / 4
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 findBy
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 delete
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
3
 getImplementation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
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
21declare(strict_types=1);
22
23namespace oat\generis\model\resource\Repository;
24
25use oat\generis\model\data\event\BeforeResourceDeleted;
26use RuntimeException;
27use BadMethodCallException;
28use InvalidArgumentException;
29use core_kernel_classes_Class;
30use core_kernel_classes_Resource;
31use oat\oatbox\event\EventManager;
32use oat\generis\model\data\Ontology;
33use core_kernel_persistence_ResourceInterface;
34use oat\generis\model\Context\ContextInterface;
35use oat\generis\model\data\event\ResourceDeleted;
36use oat\generis\model\resource\Context\ResourceRepositoryContext;
37use oat\generis\model\resource\Contract\ResourceRepositoryInterface;
38
39class 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
75        if (!$this->getImplementation()->delete($resource, $deleteReference)) {
76            throw new RuntimeException(
77                sprintf(
78                    'Resource "%s" ("%s") was not deleted.',
79                    $resource->getLabel(),
80                    $resource->getUri()
81                )
82            );
83        }
84
85        /** @var core_kernel_classes_Class|null $selectedClass */
86        $selectedClass = $context->getParameter(ResourceRepositoryContext::PARAM_SELECTED_CLASS);
87        /** @var core_kernel_classes_Class|null $selectedClass */
88        $parentClass = $context->getParameter(ResourceRepositoryContext::PARAM_PARENT_CLASS);
89
90        $resourceDeletedEvent = (new ResourceDeleted($resource->getUri()))
91            ->setSelectedClass($selectedClass)
92            ->setParentClass($parentClass);
93        $this->eventManager->trigger($resourceDeletedEvent);
94    }
95
96    private function getImplementation(): core_kernel_persistence_ResourceInterface
97    {
98        return $this->ontology->getRdfsInterface()->getResourceImplementation();
99    }
100}