Lines 100.00% 25 / 25
Functions and Methods 100.00% 5 / 5
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
UserRepository 100.00% 25 / 25 100.00% 5 / 5 5 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 findOneByAgentName 100.00% 6 / 6 100.00% 1 / 1 1
 findUsers 100.00% 4 / 4 100.00% 1 / 1 1
 findVisibleAgents 100.00% 6 / 6 100.00% 1 / 1 1
 findAllByAgentName 100.00% 8 / 8 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Created by PhpStorm.
7 * User: elkuku
8 * Date: 19.03.17
9 * Time: 12:40.
10 */
11
12namespace App\Repository;
13
14use App\Entity\User;
15use App\Enum\ProfilePart;
16use App\Enum\UserRole;
17use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
18use Doctrine\Persistence\ManagerRegistry;
19
20/**
21 * @extends ServiceEntityRepository<User>
22 */
23class UserRepository extends ServiceEntityRepository
24{
25    public function __construct(ManagerRegistry $registry)
26    {
27        parent::__construct($registry, User::class);
28    }
29
30    /** The user with this agent name, ignoring case. */
31    public function findOneByAgentName(string $agentName): ?User
32    {
33        /** @var User|null $user */
34        $user = $this->createQueryBuilder('u')
35            ->where('LOWER(u.agentName) = LOWER(:agentName)')
36            ->setParameter('agentName', $agentName)
37            ->getQuery()
38            ->getOneOrNullResult();
39
40        return $user;
41    }
42
43    /**
44     * @return User[]
45     */
46    public function findUsers(): array
47    {
48        return $this->findBy(
49            ['role' => UserRole::USER],
50            ['id' => 'ASC']
51        );
52    }
53
54    /**
55     * The users who show their agent part to other users, by agent name.
56     *
57     * @return User[]
58     */
59    public function findVisibleAgents(): array
60    {
61        /** @var User[] $users */
62        $users = $this->createQueryBuilder('u')
63            ->where('u.agentName IS NOT NULL')
64            ->orderBy('LOWER(u.agentName)')
65            ->getQuery()
66            ->getResult();
67
68        // The parts are JSON: few users, filtered here
69        return \array_values(\array_filter($users, static fn (User $user): bool => $user->isVisible(ProfilePart::AGENT)));
70    }
71
72    /**
73     * All users by agent name, those without one last (by identifier).
74     *
75     * @return User[]
76     */
77    public function findAllByAgentName(): array
78    {
79        /** @var User[] $users */
80        $users = $this->createQueryBuilder('u')
81            ->addSelect('CASE WHEN u.agentName IS NULL THEN 1 ELSE 0 END AS HIDDEN noAgentName')
82            ->addOrderBy('noAgentName')
83            ->addOrderBy('LOWER(u.agentName)')
84            ->addOrderBy('u.identifier')
85            ->getQuery()
86            ->getResult();
87
88        return $users;
89    }
90}