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