UserManager   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 85
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A findUserBy() 0 3 1
A __construct() 0 2 1
A findUserByConfirmationToken() 0 3 1
A findUserByEmail() 0 3 1
A findUserByUsername() 0 3 1
A findUserByUsernameOrEmail() 0 9 3
A getRepository() 0 3 1
A updateUser() 0 4 1
A deleteUser() 0 4 1
1
<?php
2
3
namespace ProjetNormandie\UserBundle\Manager;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\Persistence\ObjectRepository;
7
use ProjetNormandie\UserBundle\Entity\User;
8
9
class UserManager
10
{
11
    /**
12
     * Constructor.
13
     */
14
    public function __construct(private readonly EntityManagerInterface $em)
15
    {
16
    }
17
18
    /**
19
     * @param User $user
20
     */
21
    public function deleteUser(User $user): void
22
    {
23
        $this->em->remove($user);
24
        $this->em->flush();
25
    }
26
27
    /**
28
     * @param User $user
29
     */
30
    public function updateUser(User $user): void
31
    {
32
        $this->em->persist($user);
33
        $this->em->flush();
34
    }
35
36
    /**
37
     * @param string $email
38
     * @return User|null
39
     */
40
    public function findUserByEmail(string $email): ?User
41
    {
42
        return $this->findUserBy(['email' => $email]);
43
    }
44
45
    /**
46
     * @param string $username
47
     * @return User|null
48
     */
49
    public function findUserByUsername(string $username): ?User
50
    {
51
        return $this->findUserBy(['username' => $username]);
52
    }
53
54
    /**
55
     * @param string $usernameOrEmail
56
     * @return User|null
57
     */
58
    public function findUserByUsernameOrEmail(string $usernameOrEmail): ?User
59
    {
60
        if (preg_match('/^.+\@\S+\.\S+$/', $usernameOrEmail)) {
61
            $user = $this->findUserByEmail($usernameOrEmail);
62
            if (null !== $user) {
63
                return $user;
64
            }
65
        }
66
        return $this->findUserByUsername($usernameOrEmail);
67
    }
68
69
    /**
70
     * @param string $token
71
     * @return User|null
72
     */
73
    public function findUserByConfirmationToken(string $token): ?User
74
    {
75
        return $this->findUserBy(['confirmationToken' => $token]);
76
    }
77
78
79
    /**
80
     * @param array $criteria
81
     * @return mixed
82
     */
83
    private function findUserBy(array $criteria): mixed
84
    {
85
        return $this->getRepository()->findOneBy($criteria);
86
    }
87
88
    /**
89
     * @return ObjectRepository
90
     */
91
    protected function getRepository(): ObjectRepository
92
    {
93
        return $this->em->getRepository('ProjetNormandie\UserBundle\Entity\User');
94
    }
95
}
96