Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractRelationUpdateService
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 updateByTargetId
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 getRelationType
n/a
0 / 0
n/a
0 / 0
0
 createMediaRelation
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getMediaRelationRepository
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) 2020 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoMediaManager\model\relation\service\update;
24
25use oat\oatbox\service\ConfigurableService;
26use oat\taoMediaManager\model\relation\MediaRelation;
27use oat\taoMediaManager\model\relation\repository\MediaRelationRepositoryInterface;
28use oat\taoMediaManager\model\relation\repository\query\FindAllByTargetQuery;
29
30abstract class AbstractRelationUpdateService extends ConfigurableService
31{
32    public function updateByTargetId(string $targetId, array $currentMediaIds = []): void
33    {
34        $repository = $this->getMediaRelationRepository();
35
36        $collection = $repository->findAllByTarget(
37            new FindAllByTargetQuery($targetId, $this->getRelationType())
38        );
39
40        foreach ($collection->filterNewMediaIds($currentMediaIds) as $mediaId) {
41            $repository->save($this->createMediaRelation($targetId, $mediaId));
42        }
43
44        foreach ($collection->filterRemovedMediaIds($currentMediaIds) as $mediaId) {
45            $repository->remove($this->createMediaRelation($targetId, $mediaId));
46        }
47    }
48
49    abstract protected function getRelationType(): string;
50
51    private function createMediaRelation(string $targetId, string $mediaId): MediaRelation
52    {
53        return (new MediaRelation($this->getRelationType(), $targetId))
54            ->withSourceId($mediaId);
55    }
56
57    private function getMediaRelationRepository(): MediaRelationRepositoryInterface
58    {
59        return $this->getServiceLocator()->get(MediaRelationRepositoryInterface::SERVICE_ID);
60    }
61}