Completed
Branch v2.0.0 (f654ea)
by Alexander
01:15
created

UserFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 16 1
B retrieve() 0 28 6
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 retrieve(array $params = []): User
31
    {
32
        $filteredParams = UserFilterFactory::sanitizeRetrievingParams($params);
33
        $repository = new UserRepository();
34
35
        if ($filteredParams->userID) {
36
            if ($filteredParams->email) {
37
                throw new UserException('Only one retrieving param should be defined: userID or email.');
38
            }
39
40
            $user = $repository->fetchUserModelById($filteredParams->userID);
41
            if (!$user) {
42
                throw new UserException("User {$filteredParams->userID} not found.");
43
            }
44
        } elseif ($filteredParams->email) {
45
            $user = $repository->fetchUserModelByEmail($filteredParams->email);
46
            if (!$user) {
47
                throw new UserException("User with email = {$filteredParams->email} not found.");
48
            }
49
        } else {
50
            throw new UserException('Bad params for retrieving User');
51
        }
52
53
        $userSpecification = new UserSpecification($user);
54
        $userSpecification->validate();
55
56
        return $user;
57
    }
58
}
59