Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
65.52% |
19 / 29 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ClassMetadataService | |
65.52% |
19 / 29 |
|
50.00% |
2 / 4 |
12.32 | |
0.00% |
0 / 1 |
| findAll | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| fillData | |
50.00% |
9 / 18 |
|
0.00% |
0 / 1 |
8.12 | |||
| getClassMetadata | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getClassMetadataValuesService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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) 2020-2021 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\tao\model\Lists\Business\Service; |
| 25 | |
| 26 | use core_kernel_classes_Class; |
| 27 | use oat\generis\model\OntologyAwareTrait; |
| 28 | use oat\tao\model\Lists\Business\Contract\ClassMetadataSearcherInterface; |
| 29 | use oat\tao\model\Lists\Business\Domain\ClassCollection; |
| 30 | use oat\tao\model\Lists\Business\Domain\ClassMetadata; |
| 31 | use oat\tao\model\Lists\Business\Domain\MetadataCollection; |
| 32 | use oat\tao\model\Lists\Business\Input\ClassMetadataSearchInput; |
| 33 | use oat\tao\model\service\InjectionAwareService; |
| 34 | |
| 35 | class ClassMetadataService extends InjectionAwareService implements ClassMetadataSearcherInterface |
| 36 | { |
| 37 | use OntologyAwareTrait; |
| 38 | |
| 39 | /** @var int */ |
| 40 | private $maxListSize; |
| 41 | |
| 42 | public const SERVICE_ID = 'tao/ClassMetadataService'; |
| 43 | |
| 44 | public function findAll(ClassMetadataSearchInput $input): ClassCollection |
| 45 | { |
| 46 | $searchRequest = $input->getSearchRequest(); |
| 47 | |
| 48 | $this->maxListSize = $searchRequest->getMaxListSize(); |
| 49 | |
| 50 | $class = $this->getClass($searchRequest->getClassUri()); |
| 51 | $collection = new ClassCollection(); |
| 52 | |
| 53 | if (!$class->isClass()) { |
| 54 | return $collection; |
| 55 | } |
| 56 | |
| 57 | return $this->fillData($searchRequest->getIgnoredWidgets(), $collection, $class); |
| 58 | } |
| 59 | |
| 60 | private function fillData( |
| 61 | array $ignoredWidgets, |
| 62 | ClassCollection $collection, |
| 63 | core_kernel_classes_Class $currentClass, |
| 64 | core_kernel_classes_Class $parentClass = null |
| 65 | ): ClassCollection { |
| 66 | $subClasses = $currentClass->getSubClasses(); |
| 67 | |
| 68 | if (count($subClasses)) { |
| 69 | $classMetadata = (new ClassMetadata()) |
| 70 | ->setClass($currentClass->getUri()) |
| 71 | ->setLabel($currentClass->getLabel()) |
| 72 | ->setParentClass($parentClass !== null ? $parentClass->getUri() : null) |
| 73 | ->setMetaData($this->getClassMetadata($ignoredWidgets, $currentClass)); |
| 74 | |
| 75 | $collection->addClassMetadata($classMetadata); |
| 76 | |
| 77 | foreach ($subClasses as $subClass) { |
| 78 | $collection = $this->fillData($ignoredWidgets, $collection, $subClass, $currentClass); |
| 79 | } |
| 80 | |
| 81 | return $collection; |
| 82 | } |
| 83 | |
| 84 | $classMetadata = (new ClassMetadata()) |
| 85 | ->setClass($currentClass->getUri()) |
| 86 | ->setLabel($currentClass->getLabel()) |
| 87 | ->setParentClass($parentClass !== null ? $parentClass->getUri() : null) |
| 88 | ->setMetaData($this->getClassMetadata($ignoredWidgets, $currentClass)); |
| 89 | |
| 90 | $collection->addClassMetadata($classMetadata); |
| 91 | |
| 92 | return $collection; |
| 93 | } |
| 94 | |
| 95 | private function getClassMetadata(array $ignoredWidgets, core_kernel_classes_Class $class): MetadataCollection |
| 96 | { |
| 97 | return $this->getClassMetadataValuesService() |
| 98 | ->ignoreWidgets($ignoredWidgets) |
| 99 | ->getByClassRecursive($class, $this->maxListSize); |
| 100 | } |
| 101 | |
| 102 | private function getClassMetadataValuesService(): GetClassMetadataValuesService |
| 103 | { |
| 104 | return $this->getServiceLocator()->get(GetClassMetadataValuesService::class); |
| 105 | } |
| 106 | } |