Completed
Pull Request — master (#329)
by Beñat
03:55
created

UserPublicDTODataTransformer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 58
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A write() 0 4 1
A read() 0 20 2
A firstName() 0 4 2
A lastName() 0 4 2
A fullName() 0 4 2
A image() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the Kreta 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
declare(strict_types=1);
14
15
namespace Kreta\IdentityAccess\Application\DataTransformer;
16
17
use BenGorUser\User\Application\DataTransformer\UserDataTransformer;
18
use Kreta\IdentityAccess\Domain\Model\User\FullName;
19
use Kreta\IdentityAccess\Domain\Model\User\Image;
20
21
class UserPublicDTODataTransformer implements UserDataTransformer
22
{
23
    private $uploadDestination;
24
    private $user;
25
26
    public function __construct(string $uploadDestination = '')
27
    {
28
        $this->uploadDestination = $uploadDestination;
29
    }
30
31
    public function write($aUser) : void
32
    {
33
        $this->user = $aUser;
34
    }
35
36
    public function read() : array
37
    {
38
        if (null === $this->user) {
39
            return [];
40
        }
41
42
        return [
43
            'id'         => $this->user->id()->id(),
44
            'user_id'    => $this->user->id()->id(),
45
            'created_on' => $this->user->createdOn(),
46
            'email'      => $this->user->email()->email(),
47
            'first_name' => $this->firstName($this->user->fullName()),
48
            'full_name'  => $this->fullName($this->user->fullName()),
49
            'last_login' => $this->user->lastLogin(),
50
            'last_name'  => $this->lastName($this->user->fullName()),
51
            'image'      => $this->image($this->user->image()),
52
            'updated_on' => $this->user->updatedOn(),
53
            'user_name'  => $this->user->username()->username(),
54
        ];
55
    }
56
57
    private function firstName(FullName $fullName = null) : ?string
58
    {
59
        return $fullName instanceof FullName ? $fullName->firstName() : null;
60
    }
61
62
    private function lastName(FullName $fullName = null) : ?string
63
    {
64
        return $fullName instanceof FullName ? $fullName->lastName() : null;
65
    }
66
67
    private function fullName(FullName $fullName = null) : ?string
68
    {
69
        return $fullName instanceof FullName ? $fullName->fullName() : null;
70
    }
71
72
    private function image(Image $image = null) : ?string
73
    {
74
        return $image instanceof Image
75
            ? sprintf('%s/%s', $this->uploadDestination, $image->name()->filename())
76
            : null;
77
    }
78
}
79