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