Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
MetadataPropertiesExtractor
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
3 / 3
4
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%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 findByAliases
100.00% covered (success)
100.00%
14 / 14
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) 2022 (original work) Open Assessment Technologies SA.
19 */
20
21declare(strict_types=1);
22
23namespace oat\tao\model\StatisticalMetadata\Import\Extractor;
24
25use common_exception_Error;
26use core_kernel_classes_Property;
27use oat\search\base\exception\SearchGateWayExeption;
28use oat\tao\model\StatisticalMetadata\Contract\Header;
29use oat\generis\model\resource\Context\PropertyRepositoryContext;
30use oat\generis\model\resource\Contract\ResourceRepositoryInterface;
31use oat\tao\model\StatisticalMetadata\Import\Exception\HeaderValidationException;
32use oat\tao\model\StatisticalMetadata\Import\Validator\MetadataPropertiesValidator;
33use oat\tao\model\StatisticalMetadata\Import\Exception\AggregatedValidationException;
34
35class MetadataPropertiesExtractor
36{
37    /** @var MetadataAliasesExtractor */
38    private $metadataAliasesExtractor;
39
40    /** @var ResourceRepositoryInterface */
41    private $propertyRepository;
42
43    /** @var MetadataPropertiesValidator */
44    private $metadataPropertiesValidator;
45
46    public function __construct(
47        MetadataAliasesExtractor $metadataAliasesExtractor,
48        ResourceRepositoryInterface $propertyRepository,
49        MetadataPropertiesValidator $metadataPropertiesValidator
50    ) {
51        $this->metadataAliasesExtractor = $metadataAliasesExtractor;
52        $this->propertyRepository = $propertyRepository;
53        $this->metadataPropertiesValidator = $metadataPropertiesValidator;
54    }
55
56    /**
57     * @throws common_exception_Error
58     * @throws SearchGateWayExeption
59     * @throws AggregatedValidationException
60     * @throws HeaderValidationException
61     *
62     * @return core_kernel_classes_Property[]
63     */
64    public function extract(array $header): array
65    {
66        $aliases = $this->metadataAliasesExtractor->extract($header);
67        [$metadataProperties, $uniqueMetadataProperties] = $this->findByAliases($aliases);
68
69        $this->metadataPropertiesValidator->validateMetadataExistence($aliases, $metadataProperties);
70        $this->metadataPropertiesValidator->validateMetadataUniqueness($metadataProperties);
71        $this->metadataPropertiesValidator->validateMetadataTypes($metadataProperties);
72        $this->metadataPropertiesValidator->validateMetadataIsStatistical($metadataProperties);
73
74        return $uniqueMetadataProperties;
75    }
76
77    /**
78     * @throws common_exception_Error
79     * @throws SearchGateWayExeption
80     *
81     * @return core_kernel_classes_Property[][]
82     */
83    private function findByAliases(array $aliases): array
84    {
85        $metadataResources = $this->propertyRepository->findBy(
86            new PropertyRepositoryContext(
87                [
88                    PropertyRepositoryContext::PARAM_ALIASES => $aliases,
89                ]
90            )
91        );
92
93        $metadataProperties = [];
94        $uniqueMetadataProperties = [];
95
96        foreach ($metadataResources as $resource) {
97            $property = $resource->getProperty($resource->getUri());
98
99            $metadataProperties[] = $property;
100            $uniqueMetadataProperties[Header::METADATA_PREFIX . $property->getAlias()] = $property;
101        }
102
103        return [$metadataProperties, $uniqueMetadataProperties];
104    }
105}