Completed
Branch v2.0.0 (887ffb)
by Alexander
05:43
created

UserFactory::retrieveById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Domain\User\Factories;
4
5
use Domain\User\Exceptions\UserException;
6
use Domain\User\Specifications\UserSpecification;
7
use Domain\User\User;
8
use Domain\User\UserRepository;
9
use stdClass;
10
11
class UserFactory
12
{
13
    public static function create(stdClass $data): User
14
    {
15
        $filteredObject = UserFilterFactory::sanitizeCreationData($data);
16
17
        $user = new User();
18
        $user
19
            ->setEmail($filteredObject->email)
20
            ->setName($filteredObject->name)
21
            ->setPassword($filteredObject->password)
22
        ;
23
24
        $userSpecification = new UserSpecification($user);
25
        $userSpecification->validateNew();
26
27
        return $user;
28
    }
29
30
    public static function retrieveById(int $id): User
31
    {
32
        $repository = new UserRepository();
33
34
        $user = $repository->fetchUserModelById($id);
35
        if (!$user) {
36
            throw new UserException("User {$id} not found.");
37
        }
38
39
        return $user;
40
    }
41
42
    public static function retrieveByEmail(string $email): User
43
    {
44
        $repository = new UserRepository();
45
46
        $user = $repository->fetchUserModelByEmail($email);
47
        if (!$user) {
48
            throw new UserException("User with email = {$email} not found.");
49
        }
50
51
        return $user;
52
    }
53
}
54