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

UserFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 16 1
A retrieveById() 0 11 2
A retrieveByEmail() 0 11 2
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