UserMapper.php 2.73 KB
Newer Older
1
2
3
4
<?php

namespace UnicaenAuth\Service;

5
use Doctrine\ORM\EntityManagerInterface;
6
use UnicaenAuth\Entity\Db\AbstractUser;
7
use UnicaenAuth\Options\ModuleOptions;
8
9
use ZfcUser\Entity\UserInterface as UserEntityInterface;
use ZfcUser\Mapper\UserInterface;
10

11
class UserMapper implements UserInterface
12
{
13
14
15
16
17
18
19
20
21
22
23
    //========== repris du module zf-commons/zfc-user-doctrine-orm abandonné =========
    /**
     * @var \Doctrine\ORM\EntityManagerInterface
     */
    protected $em;

    /**
     * @var ModuleOptions
     */
    protected $options;

24
25
26
27
28
29
    /**
     * UserMapper constructor.
     *
     * @param EntityManagerInterface $em
     * @param ModuleOptions          $options
     */
30
31
32
33
34
35
    public function __construct(EntityManagerInterface $em, ModuleOptions $options)
    {
        $this->em      = $em;
        $this->options = $options;
    }

36
37
38
    /**
     * {@inheritdoc}
     */
39
40
41
42
43
44
45
    public function findByEmail($email)
    {
        $er = $this->em->getRepository($this->options->getUserEntityClass());

        return $er->findOneBy(array('email' => $email));
    }

46
47
48
    /**
     * {@inheritdoc}
     */
49
50
51
52
53
54
55
    public function findByUsername($username)
    {
        $er = $this->em->getRepository($this->options->getUserEntityClass());

        return $er->findOneBy(array('username' => $username));
    }

56
57
58
    /**
     * {@inheritdoc}
     */
59
60
61
62
63
64
65
    public function findById($id)
    {
        $er = $this->em->getRepository($this->options->getUserEntityClass());

        return $er->find($id);
    }

66
67
68
    /**
     * {@inheritdoc}
     */
69
    public function insert(UserEntityInterface $entity)
70
71
72
73
    {
        return $this->persist($entity);
    }

74
75
76
    /**
     * {@inheritdoc}
     */
77
    public function update(UserEntityInterface $entity)
78
79
80
81
    {
        return $this->persist($entity);
    }

82
83
84
85
86
    /**
     * @param UserEntityInterface $entity
     * @return mixed
     */
    protected function persist(UserEntityInterface $entity)
87
88
89
90
91
92
93
94
95
96
    {
        $this->em->persist($entity);
        $this->em->flush();

        return $entity;
    }
    //===================



97
98
99
100
101
102
103
104
105
106
107
108
109
110
    /**
     * Recherche un utilisateur par son username (identifiant de connexion).
     *
     * @param string $username
     * @return AbstractUser|null
     */
    public function findOneByUsername($username)
    {
        /** @var AbstractUser $user */
        $user = $this->em->getRepository($this->options->getUserEntityClass())->findOneBy(['username' => $username]);

        return $user;
    }

111
112
    /**
     * @param string $token
113
     * @return AbstractUser
114
115
116
     */
    public function findOneByPasswordResetToken($token)
    {
117
        /** @var AbstractUser $user */
118
119
120
121
122
        $user = $this->em->getRepository($this->options->getUserEntityClass())->findOneBy(['passwordResetToken' => $token]);

        return $user;
    }
}