Passed
Push — 1.0 ( 6f917f...b8453d )
by David
13:04
created

SecurityUserDao::setPasswordAndDiscardToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
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\UserService\UserDaoInterface;
16
use Mouf\Security\UserService\UserInterface;
17
18
/**
19
 * This class provides a TDBM implementation of the UserDaoInterface.
20
 */
21
class SecurityUserDao implements UserDaoInterface, ForgotYourPasswordDao
22
{
23
    /**
24
     * @var TDBMService
25
     */
26
    protected $tdbmService;
27
28
    /**
29
     * Sets the TDBM service used by this DAO.
30
     *
31
     * @param TDBMService $tdbmService
32
     */
33
    public function __construct(TDBMService $tdbmService)
34
    {
35
        $this->tdbmService = $tdbmService;
36
    }
37
38
    /**
39
     * Returns a user from its login and its password, or null if the login or credentials are false.
40
     *
41
     * @param string $login
42
     * @param string $password
43
     *
44
     * @return UserInterface
45
     */
46
    public function getUserByCredentials($login, $password)
47
    {
48
        $user = $this->findOne(['login' => $login]);
49
        if ($user === null) {
50
            return;
51
        }
52
53
        if (password_verify($password, $user->getPassword())) {
54
            return $user;
55
        } else {
56
            return;
57
        }
58
    }
59
60
    /**
61
     * Returns a user from its token.
62
     *
63
     * @param string $token
64
     *
65
     * @return UserInterface
66
     */
67
    public function getUserByToken($token)
68
    {
69
        $user = $this->findOne(['token' => $token]);
70
        if ($user === null) {
71
            throw TokenNotFoundException::notFound($token);
72
        }
73
74
        return $user;
75
    }
76
77
    /**
78
     * Discards a token.
79
     *
80
     * @param string $token
81
     */
82
    public function discardToken($token)
83
    {
84
        $user = $this->getUserByToken($token);
85
        $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...
86
        $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...
87
    }
88
89
    /**
90
     * Returns a user from its ID.
91
     *
92
     * @param string $id
93
     *
94
     * @return UserInterface
95
     */
96
    public function getUserById($id)
97
    {
98
        return $this->tdbmService->findObjectByPk('users', ['id' => $id], [], false);
99
    }
100
101
    /**
102
     * Returns a user from its login.
103
     *
104
     * @param string $login
105
     *
106
     * @return UserInterface
107
     */
108
    public function getUserByLogin($login)
109
    {
110
        return $this->findOne(['login' => $login]);
111
    }
112
113
    /**
114
     * Get a single UserBean specified by its filters.
115
     *
116
     * @param mixed $filter     The filter bag (see TDBMService::findObjects for complete description)
117
     * @param array $parameters The parameters associated with the filter
118
     *
119
     * @return UserInterface|null
120
     */
121
    private function findOne($filter = null, array $parameters = [])
122
    {
123
        return $this->tdbmService->findObject('users', $filter, $parameters);
124
    }
125
126
    /**
127
     * Sets $token for user whose mail is $email, stores the token in database.
128
     * Throws an EmailNotFoundException if the email is not part of the database.
129
     *
130
     * @param string $email
131
     *
132
     * @throws \Mouf\Security\Password\Api\EmailNotFoundException
133
     */
134
    public function setToken(string $email, string $token)
135
    {
136
        $user = $this->findOne(['email' => $email]);
137
138
        if ($user === null) {
139
            throw EmailNotFoundException::notFound($email);
140
        }
141
142
        $user->setToken($token);
143
144
        $this->tdbmService->save($user);
145
    }
146
147
    /**
148
     * Sets the password matching to $token and discards $token.
149
     * Throws an TokenNotFoundException if the token is not part of the database.
150
     *
151
     * @param string $token
152
     * @param string $password
153
     *
154
     * @throws \Mouf\Security\Password\Api\TokenNotFoundException
155
     */
156
    public function setPasswordAndDiscardToken(string $token, string $password)
157
    {
158
        $user = $this->getUserByToken($token);
159
        $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...
160
        $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...
161
        $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...
162
    }
163
}
164