Completed
Push — master ( 361e7c...d7e260 )
by Artem
03:39
created

User::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 8
dl 0
loc 19
ccs 0
cts 9
cp 0
crap 2
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Slides\Connector\Auth\Sync;
4
5
use Carbon\Carbon;
6
7
/**
8
 * Class User describes a remote user
9
 *
10
 * @package Slides\Connector\Auth\Sync
11
 */
12
final class User
13
{
14
    /**
15
     * User's remote ID
16
     *
17
     * @var int
18
     */
19
    protected $remoteId;
20
21
    /**
22
     * User's name
23
     *
24
     * @var string
25
     */
26
    protected $name;
27
28
    /**
29
     * User's email
30
     *
31
     * @var string
32
     */
33
    protected $email;
34
35
    /**
36
     * User's country code
37
     *
38
     * @var string
39
     */
40
    protected $country;
41
42
    /**
43
     * User's encrypted password
44
     *
45
     * @var string
46
     */
47
    protected $password;
48
49
    /**
50
     * @var \Carbon\Carbon
51
     */
52
    protected $updated;
53
54
    /**
55
     * @var \Carbon\Carbon
56
     */
57
    protected $created;
58
59
    /**
60
     * The instance of the user locally
61
     *
62
     * @var \Illuminate\Contracts\Auth\Authenticatable
63
     */
64
    protected $localUser;
65
66
    /**
67
     * Whether a user exist only locally
68
     *
69
     * @var bool
70
     */
71
    protected $isLocal = false;
72
73
    /**
74
     * The action for a remote sync client
75
     *
76
     * @var string
77
     */
78
    protected $remoteAction;
79
80
    /**
81
     * User constructor.
82
     *
83
     * @param int $remoteId
84
     * @param string|null $name
85
     * @param string $email
86
     * @param string|null $password
87
     * @param string|null $updated
88
     * @param string $created
89
     * @param string|null $country
90
     * @param string $action
91
     */
92
    public function __construct(
93
        int $remoteId,
94
        ?string $name,
95
        string $email,
96
        ?string $password,
97
        ?string $updated,
98
        string $created,
99
        ?string $country,
100
        string $action = null
101
    )
102
    {
103
        $this->remoteId = $remoteId;
104
        $this->name = $name;
105
        $this->email = $email;
106
        $this->password = $password;
107
        $this->updated = new Carbon($updated);
108
        $this->created = new Carbon($created);
109
        $this->country = $country;
110
        $this->remoteAction = $action;
111
    }
112
113
    /**
114
     * Get user's name
115
     *
116
     * @return string|null
117
     */
118
    public function getName()
119
    {
120
        return $this->name;
121
    }
122
123
    /**
124
     * Get user's email
125
     *
126
     * @return string
127
     */
128
    public function getEmail(): string
129
    {
130
        return $this->email;
131
    }
132
133
    /**
134
     * Get user's password
135
     *
136
     * @return string|null
137
     */
138
    public function getPassword()
139
    {
140
        return $this->password;
141
    }
142
143
    /**
144
     * Get created_at date
145
     *
146
     * @return Carbon
147
     */
148
    public function getCreated(): Carbon
149
    {
150
        return $this->created;
151
    }
152
153
    /**
154
     * Get updated_at date
155
     *
156
     * @return Carbon|null
157
     */
158
    public function getUpdated()
159
    {
160
        return $this->updated;
161
    }
162
163
    /**
164
     * Get remote action
165
     *
166
     * @return string|null
167
     */
168
    public function getRemoteAction(): ?string
169
    {
170
        return $this->remoteAction;
171
    }
172
173
    /**
174
     * Get remote user ID
175
     *
176
     * @return int
177
     */
178
    public function getRemoteId(): int
179
    {
180
        return $this->remoteId;
181
    }
182
183
    /**
184
     * Get user's country
185
     *
186
     * @return string|null
187
     */
188
    public function getCountry()
189
    {
190
        return $this->country;
191
    }
192
193
    /**
194
     * Reset the password.
195
     *
196
     * @return void
197
     */
198
    public function resetPassword()
199
    {
200
        $this->password = null;
201
    }
202
203
    /**
204
     * Create a user from the response
205
     *
206
     * @param array $response
207
     *
208
     * @return static
209
     */
210
    public static function createFromResponse(array $response)
211
    {
212
        $user = array_get($response, 'user');
213
214
        return new static(
215
            array_get($user, 'id'),
216
            array_get($user, 'name'),
217
            array_get($user, 'email'),
218
            array_get($user, 'password'),
219
            array_get($user, 'updated_at'),
220
            array_get($user, 'created_at'),
221
            array_get($user, 'country')
222
        );
223
    }
224
}