Completed
Branch v2.0.0 (addc15)
by Alexander
03:27
created

UserRepository::updateUserPassword()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.7
cc 4
nc 4
nop 2
1
<?php
2
/**
3
 * @author Alexander Torosh <[email protected]>
4
 */
5
6
namespace Domain\User\Repository;
7
8
use Domain\Core\DomainException;
9
use Domain\User\UseCase\FetchUserCase;
10
use Model\User;
11
use Phalcon\Mvc\ModelInterface;
12
13
class UserRepository
14
{
15
    /**
16
     * @throws DomainException
17
     *
18
     * @return User
19
     */
20
    public function fetchUser(int $userID): ModelInterface
21
    {
22
        $user = User::findFirst($userID);
23
        if (!$user) {
24
            throw new DomainException("User {$userID} not found.");
25
        }
26
27
        return $user;
28
    }
29
30
    /**
31
     * @throws DomainException
32
     */
33
    public function updateUserPassword(int $userID, string $passwordHash)
34
    {
35
        $fetchUserCase = new FetchUserCase();
36
        $user = $fetchUserCase->getUser($userID);
37
        if (!$user) {
38
            throw new DomainException("User {$userID} not found.");
39
        }
40
41
        $user->setPasswordHash($passwordHash);
42
43
        if (!$user->update()) {
44
            $messages = $user->getMessages();
45
            foreach ($messages as $message) {
46
                throw new DomainException($message);
47
            }
48
        }
49
    }
50
}
51