Completed
Push — master ( 02f92d...9c9507 )
by Sebastian
15:38
created

User::activate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace App\Services\Auth\Front;
4
5
use App\Services\Auth\Front\Enums\UserRole;
6
use App\Services\Auth\Front\Enums\UserStatus;
7
use App\Services\Auth\Front\Events\UserWasRegistered;
8
use App\Services\Auth\Front\Exceptions\UserIsAlreadyActivated;
9
use App\Services\Auth\User as BaseUser;
10
11
/**
12
 * @property string $address
13
 * @property string $postal
14
 * @property string $city
15
 * @property string $country
16
 * @property string $telephone
17
 * @property \App\Services\Auth\Front\Enums\UserRole $role
18
 * @property \App\Services\Auth\Front\Enums\UserStatus $status
19
 */
20
class User extends BaseUser
21
{
22
    protected $table = 'users_front';
23
    protected $presenter = UserPresenter::class;
24
25
    public static function register(array $input) : User
26
    {
27
        $defaults = [
28
            'role' => UserRole::MEMBER(),
29
            'status' => UserStatus::ACTIVE(),
30
        ];
31
32
        $user = static::create($defaults + array_only($input, [
33
            'first_name',
34
            'last_name',
35
            'address',
36
            'postal',
37
            'city',
38
            'country',
39
            'telephone',
40
            'email',
41
            'password',
42
        ]));
43
44
        event(new UserWasRegistered($user));
45
46
        return $user;
47
    }
48
49
    public function guardDriver() : string
50
    {
51
        return 'front';
52
    }
53
54
    public function getHomeUrl() : string
55
    {
56
        return url('/');
57
    }
58
59
    public function getProfileUrl() : string
60
    {
61
        return url('/');
62
    }
63
64
    public function getStatusAttribute() : UserStatus
65
    {
66
        return new UserStatus($this->attributes['status']);
67
    }
68
69
    public function setStatusAttribute(UserStatus $status)
70
    {
71
        $this->attributes['status'] = $status->getValue();
72
    }
73
74
    public function hasStatus(UserStatus $status) : bool
75
    {
76
        return $this->status->equals($status);
77
    }
78
79
    public function isActive() : bool
80
    {
81
        return $this->hasStatus(UserStatus::ACTIVE());
82
    }
83
84
    public function activate() : User
85
    {
86
        if ($this->status->doesntEqual(UserStatus::WAITING_FOR_APPROVAL())) {
87
            throw new UserIsAlreadyActivated();
88
        }
89
90
        $this->status = UserStatus::ACTIVE();
91
92
        return $this;
93
    }
94
95
    public function getRoleAttribute() : UserRole
96
    {
97
        return new UserRole($this->attributes['role']);
98
    }
99
100
    public function setRoleAttribute(UserRole $role)
101
    {
102
        $this->attributes['role'] = $role->getValue();
103
    }
104
105
    public function hasRole(UserRole $role) : bool
106
    {
107
        return $this->role->equals($role);
108
    }
109
}
110