Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.05% covered (warning)
78.05%
32 / 41
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
AssetDeleter
78.05% covered (warning)
78.05%
32 / 41
80.00% covered (warning)
80.00%
4 / 5
14.79
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 deleteByItemUri
50.00% covered (danger)
50.00%
9 / 18
0.00% covered (danger)
0.00%
0 / 1
4.12
 getAssetsToDeleteByItemUri
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 deleteAsset
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 resourceHasNoSiblings
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
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) 2023 (original work) Open Assessment Technologies SA.
19 */
20
21namespace oat\taoMediaManager\model;
22
23use oat\generis\model\data\Ontology;
24use oat\tao\model\resources\Exception\ClassDeletionException;
25use oat\tao\model\resources\Exception\PartialClassDeletionException;
26use oat\tao\model\resources\Service\ClassDeleter;
27use core_kernel_classes_Class;
28use core_kernel_classes_Resource;
29use oat\taoMediaManager\model\relation\repository\rdf\RdfMediaRelationRepository;
30use tao_helpers_Uri;
31use Psr\Log\LoggerInterface;
32use Throwable;
33
34class AssetDeleter
35{
36    private LoggerInterface $logger;
37    private Ontology $ontology;
38    private ClassDeleter $classDeleter;
39    private MediaService $mediaService;
40    private RdfMediaRelationRepository $mediaRelationRepository;
41
42    public function __construct(
43        LoggerInterface $logger,
44        MediaService $mediaService,
45        Ontology $ontology,
46        ClassDeleter $classDeleter,
47        RdfMediaRelationRepository $mediaRelationRepository
48    ) {
49        $this->logger = $logger;
50        $this->mediaService = $mediaService;
51        $this->ontology = $ontology;
52        $this->classDeleter = $classDeleter;
53        $this->mediaRelationRepository = $mediaRelationRepository;
54    }
55
56    public function deleteByItemUri(string $itemUri): void
57    {
58        foreach ($this->getAssetsToDeleteByItemUri($itemUri) as $assetUri) {
59            try {
60                $this->deleteAsset($assetUri);
61
62                $this->logger->info(
63                    sprintf(
64                        'Assets "%s" removed after Item "%s" using them was removed',
65                        $assetUri,
66                        $itemUri
67                    )
68                );
69            } catch (Throwable $e) {
70                $this->logger->error(
71                    sprintf(
72                        'ItemRemovedEventProcessor: CAUGHT %s exception removing assets: %s - trace: %s',
73                        get_class($e),
74                        $e->getMessage(),
75                        $e->getTraceAsString()
76                    )
77                );
78            }
79        }
80    }
81
82    private function getAssetsToDeleteByItemUri(string $itemUri): array
83    {
84        $assetsToDelete = [];
85
86        foreach ($this->mediaRelationRepository->getItemAssetUris($itemUri) as $assetUri) {
87            $relatedItemUris = $this->mediaRelationRepository->getRelatedItemUrisByAssetUri($assetUri);
88
89            if (count($relatedItemUris) == 1) {
90                $assetsToDelete[] = $assetUri;
91            }
92        }
93
94        return $assetsToDelete;
95    }
96
97    /**
98     * @throws PartialClassDeletionException
99     * @throws ClassDeletionException
100     */
101    private function deleteAsset(string $assetId): void
102    {
103        $uri = tao_helpers_Uri::decode($assetId);
104
105        $resource = $this->ontology->getResource($uri);
106        $type = current($resource->getTypes());
107
108        $hasNoSiblings = $this->resourceHasNoSiblings($resource);
109        $this->mediaService->deleteResource($resource);
110
111        if ($hasNoSiblings) {
112            $this->classDeleter->delete($type);
113        }
114    }
115
116    private function resourceHasNoSiblings(core_kernel_classes_Resource $resource): bool
117    {
118        $type = current($resource->getTypes());
119
120        return count($resource->getTypes()) == 1
121            && $type instanceof core_kernel_classes_Class
122            && $type->countInstances() == 1
123            && $type->getUri() !== TaoMediaOntology::CLASS_URI_MEDIA_ROOT;
124    }
125}