Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
84.62% |
33 / 39 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| IdentifierGeneratorProxy | |
84.62% |
33 / 39 |
|
83.33% |
5 / 6 |
12.52 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addIdentifierGenerator | |
25.00% |
2 / 8 |
|
0.00% |
0 / 1 |
3.69 | |||
| generate | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| assertRequiredOptionsProvided | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| getResourceType | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| getIdGenerator | |
100.00% |
8 / 8 |
|
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 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\IdentifierGenerator\Generator; |
| 24 | |
| 25 | use core_kernel_classes_Resource; |
| 26 | use InvalidArgumentException; |
| 27 | use oat\generis\model\data\Ontology; |
| 28 | |
| 29 | class IdentifierGeneratorProxy implements IdentifierGeneratorInterface |
| 30 | { |
| 31 | private Ontology $ontology; |
| 32 | private array $idGenerators = []; |
| 33 | |
| 34 | public function __construct(Ontology $ontology) |
| 35 | { |
| 36 | $this->ontology = $ontology; |
| 37 | } |
| 38 | |
| 39 | public function addIdentifierGenerator(IdentifierGeneratorInterface $idGenerator, string $resourceType): void |
| 40 | { |
| 41 | if (isset($this->idGenerators[$resourceType])) { |
| 42 | throw new InvalidArgumentException( |
| 43 | sprintf( |
| 44 | 'Id generator for type %s already defined', |
| 45 | $resourceType |
| 46 | ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | $this->idGenerators[$resourceType] = $idGenerator; |
| 51 | } |
| 52 | |
| 53 | public function generate(array $options = []): string |
| 54 | { |
| 55 | $this->assertRequiredOptionsProvided($options); |
| 56 | $resourceType = $this->getResourceType($options); |
| 57 | |
| 58 | return $this->getIdGenerator($resourceType)->generate($options); |
| 59 | } |
| 60 | |
| 61 | private function assertRequiredOptionsProvided(array $options): void |
| 62 | { |
| 63 | if (!isset($options[self::OPTION_RESOURCE]) && !isset($options[self::OPTION_RESOURCE_ID])) { |
| 64 | throw new InvalidArgumentException( |
| 65 | sprintf( |
| 66 | 'Option "%s" or "%s" is required to generate ID', |
| 67 | self::OPTION_RESOURCE, |
| 68 | self::OPTION_RESOURCE_ID |
| 69 | ) |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private function getResourceType(array $options): string |
| 75 | { |
| 76 | if ( |
| 77 | isset($options[self::OPTION_RESOURCE]) |
| 78 | && !$options[self::OPTION_RESOURCE] instanceof core_kernel_classes_Resource |
| 79 | ) { |
| 80 | throw new InvalidArgumentException( |
| 81 | sprintf( |
| 82 | 'Option "%s" must be an instance of %s', |
| 83 | self::OPTION_RESOURCE, |
| 84 | core_kernel_classes_Resource::class |
| 85 | ) |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | $resource = $options[self::OPTION_RESOURCE] ?? $this->ontology->getResource($options[self::OPTION_RESOURCE_ID]); |
| 90 | |
| 91 | return $resource->getRootId(); |
| 92 | } |
| 93 | |
| 94 | private function getIdGenerator(string $resourceType): IdentifierGeneratorInterface |
| 95 | { |
| 96 | if (!isset($this->idGenerators[$resourceType])) { |
| 97 | throw new InvalidArgumentException( |
| 98 | sprintf( |
| 99 | 'ID generator for resource type %s not defined', |
| 100 | $resourceType |
| 101 | ) |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | return $this->idGenerators[$resourceType]; |
| 106 | } |
| 107 | } |