Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| SkillRepository | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 1 |
| getSkillsInCategories | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Repository\Quiz; |
| 4 | |
| 5 | use Olz\Entity\Quiz\Skill; |
| 6 | use Olz\Entity\Quiz\SkillCategory; |
| 7 | use Olz\Repository\Common\OlzRepository; |
| 8 | |
| 9 | /** |
| 10 | * @extends OlzRepository<Skill> |
| 11 | */ |
| 12 | class SkillRepository extends OlzRepository { |
| 13 | protected string $entityClass = Skill::class; |
| 14 | |
| 15 | public const ITERATION_LIMIT = 1000; |
| 16 | |
| 17 | /** |
| 18 | * @param array<int> $category_ids |
| 19 | * |
| 20 | * @return array<Skill> |
| 21 | */ |
| 22 | public function getSkillsInCategories(array $category_ids): array { |
| 23 | $skill_category_class = SkillCategory::class; |
| 24 | $transitive_category_ids = array_map( |
| 25 | function ($id) { |
| 26 | return intval($id); |
| 27 | }, |
| 28 | $category_ids, |
| 29 | ); |
| 30 | $skills = []; |
| 31 | for ($i = 0; $i < count($transitive_category_ids); $i++) { |
| 32 | if ($i > self::ITERATION_LIMIT) { |
| 33 | throw new \Exception("Too many transitive child categories. Is there a loop?"); |
| 34 | } |
| 35 | $sane_category_id = intval($transitive_category_ids[$i]); |
| 36 | $category = $this->findOneBy(['id' => $sane_category_id]); |
| 37 | $dql = " |
| 38 | SELECT sc |
| 39 | FROM {$skill_category_class} sc |
| 40 | WHERE sc.id = '{$sane_category_id}'"; |
| 41 | $query = $this->getEntityManager()->createQuery($dql); |
| 42 | foreach ($query->getResult() as $category) { |
| 43 | foreach ($category->getSkills() as $skill) { |
| 44 | $skills[] = $skill; |
| 45 | } |
| 46 | } |
| 47 | $dql = " |
| 48 | SELECT sc |
| 49 | FROM {$skill_category_class} sc |
| 50 | WHERE sc.parent_category = '{$sane_category_id}'"; |
| 51 | $query = $this->getEntityManager()->createQuery($dql); |
| 52 | $categories = $query->getResult(); |
| 53 | foreach ($categories as $category) { |
| 54 | $transitive_category_ids[] = intval($category->getId()); |
| 55 | } |
| 56 | } |
| 57 | return $skills; |
| 58 | } |
| 59 | } |