Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| RoleRepository | |
0.00% |
0 / 30 |
|
0.00% |
0 / 5 |
56 | |
0.00% |
0 / 1 |
| getPredefinedRole | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| findRoleFuzzilyByUsername | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| findRoleFuzzilyByOldUsername | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getRolesWithParent | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| getAllActive | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Repository\Roles; |
| 4 | |
| 5 | use Doctrine\Common\Collections\Collection; |
| 6 | use Doctrine\Common\Collections\Criteria; |
| 7 | use Olz\Entity\Roles\Role; |
| 8 | use Olz\Repository\Common\OlzRepository; |
| 9 | |
| 10 | /** |
| 11 | * @extends OlzRepository<Role> |
| 12 | */ |
| 13 | class RoleRepository extends OlzRepository { |
| 14 | protected string $entityClass = Role::class; |
| 15 | |
| 16 | public function getPredefinedRole(PredefinedRole $predefined_role): ?Role { |
| 17 | $role = $this->findOneBy(['username' => $predefined_role->value]); |
| 18 | if (!$role) { |
| 19 | $this->log()->warning("Predefined role does not exist: {$predefined_role->value}"); |
| 20 | } |
| 21 | return $role; |
| 22 | } |
| 23 | |
| 24 | public function findRoleFuzzilyByUsername(string $username): ?Role { |
| 25 | $dql = "SELECT r FROM {$this->entityClass} r WHERE r.username LIKE ?1"; |
| 26 | $query = $this->getEntityManager()->createQuery($dql)->setParameter(1, $username); |
| 27 | return $query->getOneOrNullResult(); |
| 28 | } |
| 29 | |
| 30 | public function findRoleFuzzilyByOldUsername(string $old_username): ?Role { |
| 31 | $dql = "SELECT r FROM {$this->entityClass} r WHERE r.old_username LIKE ?1"; |
| 32 | $query = $this->getEntityManager()->createQuery($dql)->setParameter(1, $old_username); |
| 33 | return $query->getOneOrNullResult(); |
| 34 | } |
| 35 | |
| 36 | /** @return array<Role> */ |
| 37 | public function getRolesWithParent(?int $roleId, int $limit = 100): array { |
| 38 | if ($roleId === null) { |
| 39 | $dql = " |
| 40 | SELECT r |
| 41 | FROM {$this->entityClass} r |
| 42 | WHERE |
| 43 | r.parent_role IS NULL |
| 44 | AND r.position_within_parent IS NOT NULL |
| 45 | AND r.position_within_parent >= 0 |
| 46 | AND r.on_off = 1 |
| 47 | ORDER BY r.position_within_parent ASC"; |
| 48 | $query = $this->getEntityManager()->createQuery($dql); |
| 49 | } else { |
| 50 | $dql = " |
| 51 | SELECT r |
| 52 | FROM {$this->entityClass} r |
| 53 | WHERE |
| 54 | r.parent_role = ?1 |
| 55 | AND r.position_within_parent IS NOT NULL |
| 56 | AND r.on_off = 1 |
| 57 | ORDER BY r.position_within_parent ASC"; |
| 58 | $query = $this->getEntityManager()->createQuery($dql)->setParameter(1, $roleId); |
| 59 | } |
| 60 | $query->setMaxResults($limit); |
| 61 | return $query->getResult(); |
| 62 | } |
| 63 | |
| 64 | /** @return Collection<int, Role>&iterable<Role> */ |
| 65 | public function getAllActive(): Collection { |
| 66 | // TODO: Remove guide != '' condition again, after all ressort |
| 67 | // descriptions have been updated. This is just temporary logic! |
| 68 | $criteria = Criteria::create() |
| 69 | ->where(Criteria::expr()->andX( |
| 70 | Criteria::expr()->gte('position_within_parent', 0), // Negative = hidden |
| 71 | Criteria::expr()->neq('guide', ''), |
| 72 | )) |
| 73 | ->setFirstResult(0) |
| 74 | ->setMaxResults(1000000) |
| 75 | ; |
| 76 | return $this->matching($criteria); |
| 77 | } |
| 78 | } |