Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
GenericLomOntologyExtractor
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 extract
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 mappingRequired
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) 2024 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoQtiTest\models\classes\metadata;
24
25use core_kernel_classes_Resource as Resource;
26use core_kernel_classes_Triple as Triple;
27use DOMDocument;
28use oat\generis\model\data\Ontology;
29use oat\taoQtiItem\model\qti\metadata\MetadataExtractionException;
30use oat\taoQtiTest\models\classes\metadata\metaMetadata\PropertyMapper;
31
32class GenericLomOntologyExtractor
33{
34    private Ontology $ontology;
35    private PropertyMapper $propertyMapper;
36    private MetadataLomService $metadataLomService;
37
38    public function __construct(
39        Ontology $ontology,
40        PropertyMapper $propertyMapper,
41        MetadataLomService $metadataLomService
42    ) {
43        $this->ontology = $ontology;
44        $this->propertyMapper = $propertyMapper;
45        $this->metadataLomService = $metadataLomService;
46    }
47
48    /**
49     * @param Resource[] $resourceCollection
50     * @throws MetadataExtractionException
51     */
52    public function extract(array $resourceCollection, DOMDocument $manifest): void
53    {
54        $properties = [];
55
56        foreach ($resourceCollection as $resource) {
57            if (!$resource instanceof Resource) {
58                throw new MetadataExtractionException(
59                    __('The given target is not an instance of core_kernel_classes_Resource')
60                );
61            }
62
63            foreach ($resource->getRdfTriples() as $triple) {
64                if ($this->mappingRequired($properties, $triple)) {
65                    $properties[] = $this->propertyMapper
66                        ->getMetadataProperties(
67                            $this->ontology->getProperty($triple->predicate)
68                        );
69                }
70            }
71        }
72
73        $this->metadataLomService->addPropertiesToMetadataBlock($properties, $manifest);
74    }
75
76    /**
77     * Mapping action only applies for confirmed properties that are not already mapped
78     */
79    private function mappingRequired(array $properties, Triple $triple): bool
80    {
81        return $this->ontology->getProperty($triple->predicate)->isProperty() &&
82            array_filter($properties, function ($property) use ($triple) {
83                return $property['uri'] === $triple->predicate;
84            }) === [];
85    }
86}