Completed
Push — master ( 38e0ea...bff432 )
by Beñat
02:33
created

UserDTODataTransformer::read()   C

Complexity

Conditions 7
Paths 33

Size

Total Lines 41
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 41
rs 6.7272
cc 7
eloc 33
nc 33
nop 0
1
<?php
2
3
/*
4
 * This file is part of the BenGorUser package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorUser\User\Application\DataTransformer;
14
15
use BenGorUser\User\Domain\Model\User;
16
use BenGorUser\User\Domain\Model\UserRole;
17
18
/**
19
 * User DTO data transformer.
20
 *
21
 * @author Beñat Espiña <[email protected]>
22
 */
23
class UserDTODataTransformer implements UserDataTransformer
24
{
25
    /**
26
     * The domain user.
27
     *
28
     * @var User
29
     */
30
    private $user;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function write($aUser)
36
    {
37
        if (!$aUser instanceof User) {
38
            throw new \InvalidArgumentException(sprintf('Expected instance of %s', User::class));
39
        }
40
        $this->user = $aUser;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function read()
47
    {
48
        if (null === $this->user) {
49
            return [];
50
        }
51
52
        $roles = array_map(function (UserRole $role) {
53
            return $role->role();
54
        }, $this->user->roles());
55
56
        $encodedPassword = !$this->user->password()
57
            ? null
58
            : $this->user->password()->encodedPassword();
59
        $salt = !$this->user->password()
60
            ? null
61
            : $this->user->password()->salt();
62
63
        $confirmationToken = !$this->user->confirmationToken()
64
            ? null
65
            : $this->user->confirmationToken()->token();
66
        $invitationToken = !$this->user->invitationToken()
67
            ? null
68
            : $this->user->invitationToken()->token();
69
        $rememberPasswordToken = !$this->user->rememberPasswordToken()
70
            ? null
71
            : $this->user->rememberPasswordToken()->token();
72
73
        return [
74
            'id'                      => $this->user->id()->id(),
75
            'confirmation_token'      => $confirmationToken,
76
            'created_on'              => $this->user->createdOn(),
77
            'email'                   => $this->user->email()->email(),
78
            'invitation_token'        => $invitationToken,
79
            'last_login'              => $this->user->lastLogin(),
80
            'encoded_password'        => $encodedPassword,
81
            'salt'                    => $salt,
82
            'remember_password_token' => $rememberPasswordToken,
83
            'roles'                   => $roles,
84
            'updated_on'              => $this->user->updatedOn(),
85
        ];
86
    }
87
}
88