SecurityUserDao   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 166
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A search() 0 13 2
A __construct() 0 4 1
A getUserByCredentials() 0 13 3
A getUserByToken() 0 9 2
A discardToken() 0 6 1
A getUserById() 0 4 1
A getUserByLogin() 0 4 1
A findOne() 0 4 1
A setToken() 0 12 2
A setPasswordAndDiscardToken() 0 7 1
1
<?php
2
3
/*
4
 * This file has been automatically generated by TDBM.
5
 * DO NOT edit this file, as it might be overwritten.
6
 * If you need to perform changes, edit the UserDao class instead!
7
 */
8
9
namespace Mouf\Security\DAO;
10
11
use Mouf\Database\TDBM\TDBMService;
12
use Mouf\Security\Password\Api\ForgotYourPasswordDao;
13
use Mouf\Security\Password\Exception\EmailNotFoundException;
14
use Mouf\Security\Password\Exception\TokenNotFoundException;
15
use Mouf\Security\UserManagement\Api\UserListDao;
16
use Mouf\Security\UserService\UserDaoInterface;
17
use Mouf\Security\UserService\UserInterface;
18
use Porpaginas\Result;
19
20
/**
21
 * This class provides a TDBM implementation of the UserDaoInterface.
22
 */
23
class SecurityUserDao implements UserDaoInterface, ForgotYourPasswordDao, UserListDao
24
{
25
    /**
26
     * @var TDBMService
27
     */
28
    protected $tdbmService;
29
30
    /**
31
     * Sets the TDBM service used by this DAO.
32
     *
33
     * @param TDBMService $tdbmService
34
     */
35
    public function __construct(TDBMService $tdbmService)
36
    {
37
        $this->tdbmService = $tdbmService;
38
    }
39
40
    /**
41
     * Returns a user from its login and its password, or null if the login or credentials are false.
42
     *
43
     * @param string $login
44
     * @param string $password
45
     *
46
     * @return UserInterface
47
     */
48
    public function getUserByCredentials($login, $password)
49
    {
50
        $user = $this->findOne(['login' => $login]);
51
        if ($user === null) {
52
            return;
53
        }
54
55
        if (password_verify($password, $user->getPassword())) {
56
            return $user;
57
        } else {
58
            return;
59
        }
60
    }
61
62
    /**
63
     * Returns a user from its token.
64
     *
65
     * @param string $token
66
     *
67
     * @return UserInterface
68
     */
69
    public function getUserByToken($token)
70
    {
71
        $user = $this->findOne(['token' => $token]);
72
        if ($user === null) {
73
            throw TokenNotFoundException::notFound($token);
74
        }
75
76
        return $user;
77
    }
78
79
    /**
80
     * Discards a token.
81
     *
82
     * @param string $token
83
     */
84
    public function discardToken($token)
85
    {
86
        $user = $this->getUserByToken($token);
87
        $user->setToken(null);
0 ignored issues
show
Bug introduced by
The method setToken() does not seem to exist on object<Mouf\Security\UserService\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
        $this->tdbmService->save($user);
0 ignored issues
show
Compatibility introduced by
$user of type object<Mouf\Security\UserService\UserInterface> is not a sub-type of object<Mouf\Database\TDBM\AbstractTDBMObject>. It seems like you assume a concrete implementation of the interface Mouf\Security\UserService\UserInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
89
    }
90
91
    /**
92
     * Returns a user from its ID.
93
     *
94
     * @param string $id
95
     *
96
     * @return UserInterface
97
     */
98
    public function getUserById($id)
99
    {
100
        return $this->tdbmService->findObjectByPk('users', ['id' => $id], [], false);
101
    }
102
103
    /**
104
     * Returns a user from its login.
105
     *
106
     * @param string $login
107
     *
108
     * @return UserInterface
109
     */
110
    public function getUserByLogin($login)
111
    {
112
        return $this->findOne(['login' => $login]);
113
    }
114
115
    /**
116
     * Get a single UserBean specified by its filters.
117
     *
118
     * @param mixed $filter     The filter bag (see TDBMService::findObjects for complete description)
119
     * @param array $parameters The parameters associated with the filter
120
     *
121
     * @return UserInterface|null
122
     */
123
    private function findOne($filter = null, array $parameters = [])
124
    {
125
        return $this->tdbmService->findObject('users', $filter, $parameters);
126
    }
127
128
    /**
129
     * Sets $token for user whose mail is $email, stores the token in database.
130
     * Throws an EmailNotFoundException if the email is not part of the database.
131
     *
132
     * @param string $email
133
     *
134
     * @throws \Mouf\Security\Password\Api\EmailNotFoundException
135
     */
136
    public function setToken(string $email, string $token)
137
    {
138
        $user = $this->findOne(['email' => $email]);
139
140
        if ($user === null) {
141
            throw EmailNotFoundException::notFound($email);
142
        }
143
144
        $user->setToken($token);
145
146
        $this->tdbmService->save($user);
147
    }
148
149
    /**
150
     * Sets the password matching to $token and discards $token.
151
     * Throws an TokenNotFoundException if the token is not part of the database.
152
     *
153
     * @param string $token
154
     * @param string $password
155
     *
156
     * @throws \Mouf\Security\Password\Api\TokenNotFoundException
157
     */
158
    public function setPasswordAndDiscardToken(string $token, string $password)
159
    {
160
        $user = $this->getUserByToken($token);
161
        $user->setPassword($password);
0 ignored issues
show
Bug introduced by
The method setPassword() does not seem to exist on object<Mouf\Security\UserService\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
162
        $user->setToken(null);
0 ignored issues
show
Bug introduced by
The method setToken() does not seem to exist on object<Mouf\Security\UserService\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
163
        $this->tdbmService->save($user);
0 ignored issues
show
Compatibility introduced by
$user of type object<Mouf\Security\UserService\UserInterface> is not a sub-type of object<Mouf\Database\TDBM\AbstractTDBMObject>. It seems like you assume a concrete implementation of the interface Mouf\Security\UserService\UserInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
164
    }
165
166
    /**
167
     * Returns a list of users, as a Porpaginas result.
168
     * This list can be filtered based on the $filters array, that can be really anything based on the filters you implement.
169
     *
170
     * @param array $filters
171
     * @param $orderBy
172
     * @param $direction
173
     * @return Result
174
     */
175
    public function search(array $filters, $orderBy, $direction) : Result
176
    {
177
        $sql = null;
178
        $parameters = [];
179
        if (isset($filters['q'])) {
180
            $sql = 'login LIKE :login';
181
            $parameters = [
182
                'login' => '%'.$filters['q'].'%'
183
            ];
184
        }
185
186
        return $this->tdbmService->findObjects('users', $sql, $parameters);
187
    }
188
}
189