Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ResourceRelationServiceProxy
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
5 / 5
11
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%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
 getResourceRelationService
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 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 (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            foreach ($services as $serviceId) {
68                $relations = array_merge(
69                    $relations,
70                    $this->getResourceRelationService($serviceId)
71                        ->findRelations($query)
72                        ->getIterator()
73                        ->getArrayCopy()
74                );
75            }
76        }
77
78        return new ResourceRelationCollection(...$relations);
79    }
80
81    private function getResourceRelationService(string $serviceId): ResourceRelationServiceInterface
82    {
83        return $this->getServiceLocator()->get($serviceId);
84    }
85
86    private function getServices(string $type): array
87    {
88        $services = (array)$this->getOption(self::OPTION_SERVICES, []);
89
90        if (!isset($services[$type])) {
91            $services[$type] = [];
92        }
93
94        return $services;
95    }
96}