Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ResourceRelationServiceProxy
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
4 / 4
10
100.00% covered (success)
100.00%
1 / 1
 addService
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 removeService
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 findRelations
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
4
 getServices
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
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) 2020-2025 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\tao\model\resources\relation\service;
24
25use oat\oatbox\service\ConfigurableService;
26use oat\tao\model\resources\relation\FindAllQuery;
27use oat\tao\model\resources\relation\ResourceRelationCollection;
28
29class ResourceRelationServiceProxy extends ConfigurableService implements ResourceRelationServiceInterface
30{
31    public const SERVICE_ID = 'tao/ResourceRelationServiceProxy';
32    public const OPTION_SERVICES = 'services';
33
34    public function addService(string $type, string $serviceId): void
35    {
36        $services = $this->getServices($type);
37
38        if (!in_array($serviceId, $services[$type], true)) {
39            $services[$type][] = $serviceId;
40        }
41
42        $this->setOption(self::OPTION_SERVICES, $services);
43    }
44
45    public function removeService(string $type, string $serviceId): void
46    {
47        $services = $this->getServices($type);
48
49        $key = array_search($serviceId, $services[$type]);
50
51        if ($key !== false) {
52            unset($services[$type][$key]);
53        }
54
55        $this->setOption(self::OPTION_SERVICES, $services);
56    }
57
58    public function findRelations(FindAllQuery $query): ResourceRelationCollection
59    {
60        $relations = [];
61
62        foreach ($this->getOption(self::OPTION_SERVICES, []) as $type => $services) {
63            if ($query->getType() !== $type) {
64                continue;
65            }
66
67            $container = $this->getServiceManager()->getContainer();
68
69            foreach ($services as $serviceId) {
70                $relations = array_merge(
71                    $relations,
72                    $container->get($serviceId)
73                        ->findRelations($query)
74                        ->getIterator()
75                        ->getArrayCopy()
76                );
77            }
78        }
79
80        return new ResourceRelationCollection(...$relations);
81    }
82
83    private function getServices(string $type): array
84    {
85        $services = (array)$this->getOption(self::OPTION_SERVICES, []);
86
87        if (!isset($services[$type])) {
88            $services[$type] = [];
89        }
90
91        return $services;
92    }
93}