Completed
Push — master ( 2e9ff7...454a32 )
by Alexis
07:25
created

UserManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace AWurth\SilexUser\Model;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder;
8
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
9
10
class UserManager implements UserManagerInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $class;
16
17
    /**
18
     * @var EncoderFactoryInterface
19
     */
20
    protected $encoderFactory;
21
22
    /**
23
     * @var ObjectManager
24
     */
25
    protected $objectManager;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param ObjectManager           $objectManager
31
     * @param EncoderFactoryInterface $encoderFactory
32
     * @param string                  $class
33
     */
34
    public function __construct(ObjectManager $objectManager, EncoderFactoryInterface $encoderFactory, $class)
35
    {
36
        $this->objectManager = $objectManager;
37
        $this->encoderFactory = $encoderFactory;
38
        $this->class = $class;
39
    }
40
41
    /**
42
     * Gets the Doctrine repository for the User class.
43
     *
44
     * @return ObjectRepository
45
     */
46
    protected function getRepository()
47
    {
48
        return $this->objectManager->getRepository($this->getClass());
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function createUser()
55
    {
56
        $class = $this->getClass();
57
58
        return new $class();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function deleteUser(UserInterface $user)
65
    {
66
        $this->objectManager->remove($user);
67
        $this->objectManager->flush();
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function findUserBy(array $criteria)
74
    {
75
        return $this->getRepository()->findOneBy($criteria);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function findUserByEmail($email)
82
    {
83
        return $this->findUserBy(array('email' => $email));
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function findUserByUsername($username)
90
    {
91
        return $this->findUserBy(array('username' => $username));
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function findUserByUsernameOrEmail($usernameOrEmail)
98
    {
99
        if (preg_match('/^.+\@\S+\.\S+$/', $usernameOrEmail)) {
100
            return $this->findUserByEmail($usernameOrEmail);
101
        }
102
103
        return $this->findUserByUsername($usernameOrEmail);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function findUserByConfirmationToken($token)
110
    {
111
        return $this->findUserBy(['confirmationToken' => $token]);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function findUsers()
118
    {
119
        return $this->getRepository()->findAll();
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getClass()
126
    {
127
        return $this->class;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function updatePassword(UserInterface $user)
134
    {
135
        $plainPassword = $user->getPlainPassword();
136
137
        if (0 === strlen($plainPassword)) {
138
            return;
139
        }
140
141
        $encoder = $this->encoderFactory->getEncoder($user);
142
        if ($encoder instanceof BCryptPasswordEncoder) {
143
            $user->setSalt(null);
144
        } else {
145
            $user->setSalt(rtrim(str_replace('+', '.', base64_encode(random_bytes(32))), '='));
146
        }
147
148
        $user->setPassword($encoder->encodePassword($plainPassword, $user->getSalt()));
149
        $user->eraseCredentials();
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function updateUser(UserInterface $user, $flush = true)
156
    {
157
        $this->updatePassword($user);
158
159
        $this->objectManager->persist($user);
160
        if ($flush) {
161
            $this->objectManager->flush();
162
        }
163
    }
164
}
165