Completed
Push — master ( 633b6b...4d2598 )
by Beñat
10s
created

UserPublicDTODataTransformer::lastName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
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
            'email'      => $this->user->email()->email(),
46
            'first_name' => $this->firstName($this->user->fullName()),
47
            'full_name'  => $this->fullName($this->user->fullName()),
48
            'last_name'  => $this->lastName($this->user->fullName()),
49
            'image'      => $this->image($this->user->image()),
50
            'user_name'  => $this->user->username()->username(),
51
        ];
52
    }
53
54
    private function firstName(FullName $fullName = null) : ?string
55
    {
56
        return $fullName instanceof FullName ? $fullName->firstName() : null;
57
    }
58
59
    private function lastName(FullName $fullName = null) : ?string
60
    {
61
        return $fullName instanceof FullName ? $fullName->lastName() : null;
62
    }
63
64
    private function fullName(FullName $fullName = null) : ?string
65
    {
66
        return $fullName instanceof FullName ? $fullName->fullName() : null;
67
    }
68
69
    private function image(Image $image = null) : ?string
70
    {
71
        return $image instanceof Image
72
            ? sprintf('%s/%s', $this->uploadDestination, $image->name()->filename())
73
            : null;
74
    }
75
}
76