Completed
Branch v2.0.0 (e47d62)
by Alexander
03:40
created

UserRepository::fetchUserModelById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * @author Alexander Torosh <[email protected]>
4
 */
5
6
namespace Domain\User;
7
8
use Core\Domain\DomainException;
9
use Core\Domain\DomainRepository;
10
use DbModel\User as UserModel;
11
12
class UserRepository extends DomainRepository
13
{
14
    public function insertUserIntoDb(User $user): int
15
    {
16
        $userModel = new UserModel();
17
18
        $userModel->email = $user->getEmail();
19
        $userModel->name = $user->getName();
20
21
        if ($user->passwordDefined()) {
22
            $userModel->password_hash = $user->buildPasswordHash();
23
        }
24
25
        $userModel->create();
26
27
        return (int) $userModel->id;
28
    }
29
30
    public function fetchUserModelById(int $userID): UserModel
31
    {
32
        $result = UserModel::findFirst($userID);
33
        if (!$result) {
34
            throw new DomainException("User {$userID} not found.");
35
        }
36
37
        return $result;
38
    }
39
40
    public function fetchUserModelByEmail(string $email): UserModel
41
    {
42
        $result = UserModel::findFirstByEmail($email);
43
        if (!$result) {
44
            throw new DomainException("User with email = {$email} not found.");
45
        }
46
47
        return $result;
48
    }
49
}
50