Completed
Push — master ( 719cb2...3075dc )
by Beñat
03:12
created

UserDTODataTransformer::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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(User $aUser)
36
    {
37
        $this->user = $aUser;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function read()
44
    {
45
        if (null === $this->user) {
46
            return [];
47
        }
48
49
        $roles = array_map(function (UserRole $role) {
50
            return $role->role();
51
        }, $this->user->roles());
52
53
        $confirmationToken = !$this->user->confirmationToken()
54
            ? null
55
            : $this->user->confirmationToken()->token();
56
        $rememberPasswordToken = !$this->user->rememberPasswordToken()
57
            ? null
58
            : $this->user->rememberPasswordToken()->token();
59
60
        return [
61
            'id'                      => $this->user->id()->id(),
62
            'confirmation_token'      => $confirmationToken,
63
            'created_on'              => $this->user->createdOn(),
64
            'email'                   => $this->user->email()->email(),
65
            'last_login'              => $this->user->lastLogin(),
66
            'encoded_password'        => $this->user->password()->encodedPassword(),
67
            'salt'                    => $this->user->password()->salt(),
68
            'remember_password_token' => $rememberPasswordToken,
69
            'roles'                   => $roles,
70
            'updated_on'              => $this->user->updatedOn(),
71
        ];
72
    }
73
}
74