Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.00% |
19 / 20 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ResourceExtractor | |
95.00% |
19 / 20 |
|
75.00% |
3 / 4 |
7 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| extract | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| extractResourceHeader | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getMappedRootClass | |
100.00% |
6 / 6 |
|
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 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\StatisticalMetadata\Import\Extractor; |
| 24 | |
| 25 | use core_kernel_classes_Class; |
| 26 | use oat\tao\model\TaoOntology; |
| 27 | use core_kernel_classes_Resource; |
| 28 | use oat\generis\model\data\Ontology; |
| 29 | use oat\tao\model\StatisticalMetadata\Contract\Header; |
| 30 | use oat\tao\model\StatisticalMetadata\Import\Validator\RecordResourceValidator; |
| 31 | use oat\tao\model\StatisticalMetadata\Import\Exception\ErrorValidationException; |
| 32 | |
| 33 | class ResourceExtractor |
| 34 | { |
| 35 | /** @var RecordResourceValidator */ |
| 36 | private $recordResourceValidator; |
| 37 | |
| 38 | /** @var Ontology */ |
| 39 | private $ontology; |
| 40 | |
| 41 | /** @var array<string, core_kernel_classes_Class> */ |
| 42 | private $rootClassMap; |
| 43 | |
| 44 | public function __construct(RecordResourceValidator $recordResourceValidator, Ontology $ontology) |
| 45 | { |
| 46 | $this->recordResourceValidator = $recordResourceValidator; |
| 47 | $this->ontology = $ontology; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @throws ErrorValidationException |
| 52 | */ |
| 53 | public function extract(array $record): core_kernel_classes_Resource |
| 54 | { |
| 55 | $this->recordResourceValidator->validateResourceId($record); |
| 56 | |
| 57 | $resourceHeader = $this->extractResourceHeader($record); |
| 58 | $resource = $this->ontology->getResource($record[$resourceHeader]); |
| 59 | |
| 60 | try { |
| 61 | $this->recordResourceValidator->validateResourceAvailability($resource); |
| 62 | $this->recordResourceValidator->validateResourceType($resource, $this->getMappedRootClass($resourceHeader)); |
| 63 | } catch (ErrorValidationException $exception) { |
| 64 | $exception->setColumn($resourceHeader); |
| 65 | |
| 66 | throw $exception; |
| 67 | } |
| 68 | |
| 69 | return $resource; |
| 70 | } |
| 71 | |
| 72 | private function extractResourceHeader(array $record): string |
| 73 | { |
| 74 | return empty($record[Header::ITEM_ID]) |
| 75 | ? Header::TEST_ID |
| 76 | : Header::ITEM_ID; |
| 77 | } |
| 78 | |
| 79 | private function getMappedRootClass(string $resourceHeader): core_kernel_classes_Class |
| 80 | { |
| 81 | if (!isset($this->rootClassMap)) { |
| 82 | $this->rootClassMap = [ |
| 83 | Header::ITEM_ID => $this->ontology->getClass(TaoOntology::CLASS_URI_ITEM), |
| 84 | Header::TEST_ID => $this->ontology->getClass(TaoOntology::CLASS_URI_TEST), |
| 85 | ]; |
| 86 | } |
| 87 | |
| 88 | return $this->rootClassMap[$resourceHeader]; |
| 89 | } |
| 90 | } |