Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| RegisterSkillCategoriesEndpoint | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
90 | |
0.00% |
0 / 1 |
| handle | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Apps\Quiz\Endpoints; |
| 4 | |
| 5 | use Olz\Api\OlzTypedEndpoint; |
| 6 | use Olz\Entity\Quiz\SkillCategory; |
| 7 | |
| 8 | /** |
| 9 | * @phpstan-type OlzSkillCategoryData array{ |
| 10 | * name: non-empty-string, |
| 11 | * parentCategoryName?: ?non-empty-string, |
| 12 | * } |
| 13 | * |
| 14 | * @extends OlzTypedEndpoint< |
| 15 | * array{skillCategories: array<OlzSkillCategoryData>}, |
| 16 | * array{idByName: array<non-empty-string, non-empty-string>} |
| 17 | * > |
| 18 | */ |
| 19 | class RegisterSkillCategoriesEndpoint extends OlzTypedEndpoint { |
| 20 | protected function handle(mixed $input): mixed { |
| 21 | $skill_category_repo = $this->entityManager()->getRepository(SkillCategory::class); |
| 22 | $category_by_name = []; |
| 23 | foreach ($input['skillCategories'] as $input_category) { |
| 24 | $category_name = $input_category['name']; |
| 25 | $existing_category = $skill_category_repo->findOneBy(['name' => $category_name]); |
| 26 | if ($existing_category) { |
| 27 | $category = $existing_category; |
| 28 | } else { |
| 29 | $category = new SkillCategory(); |
| 30 | $this->entityUtils()->createOlzEntity($category, ['onOff' => true]); |
| 31 | } |
| 32 | $category->setName($category_name); |
| 33 | $category_by_name[$category_name] = $category; |
| 34 | } |
| 35 | foreach ($input['skillCategories'] as $input_category) { |
| 36 | $category_name = $input_category['name']; |
| 37 | $parent_name = $input_category['parentCategoryName'] ?? null; |
| 38 | $category = $category_by_name[$category_name]; |
| 39 | if ($parent_name === null) { |
| 40 | $category->setParentCategory(null); |
| 41 | } else { |
| 42 | $parent_category = $category_by_name[$parent_name] ?? null; |
| 43 | if ($parent_category === null) { |
| 44 | throw new \Exception("No such parent category: {$parent_name}"); |
| 45 | } |
| 46 | $category->setParentCategory($parent_category); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | foreach ($category_by_name as $category_name => $category) { |
| 51 | $this->entityManager()->persist($category); |
| 52 | } |
| 53 | $this->entityManager()->flush(); |
| 54 | |
| 55 | $id_by_name = []; |
| 56 | foreach ($category_by_name as $category_name => $category) { |
| 57 | $id_by_name[$category_name] = $this->idUtils()->toExternalId($category->getId() ?? 0, 'SkillCategory') ?: '-'; |
| 58 | } |
| 59 | return ['idByName' => $id_by_name]; |
| 60 | } |
| 61 | } |