Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.83% |
23 / 24 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| UserRepository | |
95.83% |
23 / 24 |
|
66.67% |
2 / 3 |
6 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSortedByStore | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
4.01 | |||
| findActiveUsers | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Repository; |
| 6 | |
| 7 | use App\Entity\User; |
| 8 | use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
| 9 | use Doctrine\Persistence\ManagerRegistry; |
| 10 | |
| 11 | /** |
| 12 | * @method User|null find($id, $lockMode = null, $lockVersion = null) |
| 13 | * @method User|null findOneBy(array<string, mixed> $criteria, ?array<string, string> $orderBy = null) |
| 14 | * @method User[] findAll() |
| 15 | * @method User[] findBy(array<string, mixed> $criteria, ?array<string, string> $orderBy = null, $limit = null, $offset = null) |
| 16 | * |
| 17 | * @extends ServiceEntityRepository<User> |
| 18 | */ |
| 19 | class UserRepository extends ServiceEntityRepository |
| 20 | { |
| 21 | public function __construct(ManagerRegistry $registry) |
| 22 | { |
| 23 | parent::__construct($registry, User::class); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @return User[] |
| 28 | */ |
| 29 | public function getSortedByStore(): array |
| 30 | { |
| 31 | $users = $this->findActiveUsers(); |
| 32 | |
| 33 | usort( |
| 34 | $users, |
| 35 | static function (User $a, User $b): int { |
| 36 | $aId = 0; |
| 37 | $bId = 0; |
| 38 | |
| 39 | foreach ($a->getStores() as $store) { |
| 40 | $aId = $store->getId(); |
| 41 | } |
| 42 | |
| 43 | foreach ($b->getStores() as $store) { |
| 44 | $bId = $store->getId(); |
| 45 | } |
| 46 | |
| 47 | return ($aId < $bId) ? -1 : 1; |
| 48 | } |
| 49 | ); |
| 50 | |
| 51 | return $users; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Find all active users. |
| 56 | * |
| 57 | * @return User[] |
| 58 | */ |
| 59 | public function findActiveUsers(): array |
| 60 | { |
| 61 | return $this->findBy( |
| 62 | [ |
| 63 | 'role' => 'ROLE_USER', |
| 64 | 'isActive' => 1, |
| 65 | ], |
| 66 | [ |
| 67 | 'name' => 'ASC', |
| 68 | ] |
| 69 | ); |
| 70 | } |
| 71 | } |