Completed
Push — master ( e45723...8d0f03 )
by Mahmoud
03:14
created

UserTransformer::transformToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
3
namespace App\Containers\User\UI\API\Transformers;
4
5
use App\Containers\Authorization\UI\API\Transformers\RoleTransformer;
6
use App\Containers\User\Models\User;
7
use App\Port\Transformer\Abstracts\Transformer;
8
use Carbon\Carbon;
9
use Config;
10
11
/**
12
 * Class UserTransformer.
13
 *
14
 * @author Mahmoud Zalt <[email protected]>
15
 */
16
class UserTransformer extends Transformer
17
{
18
19
    /**
20
     * @var  array
21
     */
22
    protected $defaultIncludes = [
23
        'roles',
24
    ];
25
26
    /**
27
     * @param \App\Containers\User\Models\User $user
28
     *
29
     * @return array
30
     */
31
    public function transform(User $user)
32
    {
33
        $response = [
34
            'object'               => 'User',
35
            'id'                   => $user->getHashedKey(),
36
            'name'                 => $user->name,
37
            'email'                => $user->email,
38
            'confirmed'            => $user->confirmed,
39
            'nickname'             => $user->nickname,
40
            'gender'               => $user->gender,
41
            'birth'                => $user->birth,
42
            'social_auth_provider' => $user->social_provider,
43
            'social_id'            => $user->social_id,
44
            'social_avatar'        => [
45
                'avatar'   => $user->social_avatar,
46
                'original' => $user->social_avatar_original,
47
            ],
48
            'created_at'           => $user->created_at,
49
            'updated_at'           => $user->updated_at,
50
            'token'                => $this->transformToken($user->token),
51
        ];
52
53
        $response = $this->ifAdmin([
54
            'real_id'    => $user->id,
55
            'deleted_at' => $user->deleted_at,
56
        ], $response);
57
58
        return $response;
59
    }
60
61
    private function transformToken($token)
62
    {
63
        return [
64
            'object'       => 'Token',
65
            'access_token' => [
66
                'token_type'   => 'Bearer',
67
                'time_to_live' => [
68
                    'minutes' => Config::get('jwt.ttl'),
69
                ],
70
                'expires_in'   => Carbon::now()->addMinutes(Config::get('jwt.ttl')),
71
                'token'        => $token,
72
            ],
73
        ];
74
    }
75
76
    public function includeRoles(User $user)
77
    {
78
        return $this->collection($user->roles, new RoleTransformer());
79
    }
80
}
81