1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stack Exchange Api Client library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2016 Beñat Espiña <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BenatEspina\StackExchangeApiClient\Infrastructure\Domain\Model; |
13
|
|
|
|
14
|
|
|
use BenatEspina\StackExchangeApiClient\Domain\Model\BadgeCount; |
15
|
|
|
use BenatEspina\StackExchangeApiClient\Domain\Model\NetworkUser; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The anemic implementation of network user domain class. |
19
|
|
|
* |
20
|
|
|
* @author Beñat Espiña <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class AnemicNetworkUser implements NetworkUser |
23
|
|
|
{ |
24
|
|
|
const USER_TYPE_DOES_NOT_EXIST = 'does_not_exist'; |
25
|
|
|
const USER_TYPE_MODERATOR = 'moderator'; |
26
|
|
|
const USER_TYPE_REGISTERED = 'registered'; |
27
|
|
|
const USER_TYPE_UNREGISTERED = 'unregistered'; |
28
|
|
|
|
29
|
|
|
private $id; |
30
|
|
|
private $answerCount; |
31
|
|
|
private $badgeCounts; |
32
|
|
|
private $creationDate; |
33
|
|
|
private $lastAccessDate; |
34
|
|
|
private $questionCount; |
35
|
|
|
private $reputation; |
36
|
|
|
private $siteName; |
37
|
|
|
private $siteUrl; |
38
|
|
|
private $topAnswers; |
39
|
|
|
private $topQuestions; |
40
|
|
|
private $userType; |
41
|
|
|
|
42
|
|
|
public static function fromJson($data) |
43
|
|
|
{ |
44
|
|
|
$topAnswers = []; |
45
|
|
|
$topQuestions = []; |
46
|
|
View Code Duplication |
if (array_key_exists('top_answers', $data) && is_array($data['top_answers'])) { |
|
|
|
|
47
|
|
|
foreach ($data['top_answers'] as $topAnswer) { |
48
|
|
|
$topAnswers[] = AnemicNetworkPost::fromJson($topAnswer); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
View Code Duplication |
if (array_key_exists('top_questions', $data) && is_array($data['top_questions'])) { |
|
|
|
|
52
|
|
|
foreach ($data['top_questions'] as $topQuestion) { |
53
|
|
|
$topQuestions[] = AnemicNetworkPost::fromJson($topQuestion); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return new self( |
58
|
|
|
array_key_exists('user_id', $data) ? $data['user_id'] : null, |
59
|
|
|
array_key_exists('answer_count', $data) ? $data['answer_count'] : null, |
60
|
|
|
array_key_exists('badge_counts', $data) ? AnemicBadgeCount::fromJson($data['badge_counts']) : null, |
61
|
|
|
array_key_exists('creation_date', $data) ? new \DateTimeImmutable('@' . $data['creation_date']) : null, |
62
|
|
|
array_key_exists('last_access_date', $data) ? new \DateTimeImmutable('@' . $data['last_access_date']) : null, |
|
|
|
|
63
|
|
|
array_key_exists('question_count', $data) ? $data['question_count'] : null, |
64
|
|
|
array_key_exists('reputation', $data) ? $data['reputation'] : null, |
65
|
|
|
array_key_exists('site_name', $data) ? $data['site_name'] : null, |
66
|
|
|
array_key_exists('site_url', $data) ? $data['site_url'] : null, |
67
|
|
|
$topAnswers, |
68
|
|
|
$topQuestions, |
69
|
|
|
array_key_exists('user_type', $data) ? $data['user_type'] : null |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function fromProperties( |
74
|
|
|
$id, |
|
|
|
|
75
|
|
|
$answerCount, |
|
|
|
|
76
|
|
|
BadgeCount $badgeCounts, |
|
|
|
|
77
|
|
|
\DateTimeInterface $creationDate, |
|
|
|
|
78
|
|
|
\DateTimeInterface $lastAccessDate, |
|
|
|
|
79
|
|
|
$questionCount, |
|
|
|
|
80
|
|
|
$reputation, |
|
|
|
|
81
|
|
|
$siteName, |
|
|
|
|
82
|
|
|
$siteUrl, |
|
|
|
|
83
|
|
|
array $topAnswers, |
|
|
|
|
84
|
|
|
array $topQuestions, |
|
|
|
|
85
|
|
|
$userType |
|
|
|
|
86
|
|
|
) { |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function __construct( |
90
|
|
|
$id = null, |
91
|
|
|
$answerCount = null, |
92
|
|
|
BadgeCount $badgeCounts = null, |
93
|
|
|
\DateTimeInterface $creationDate = null, |
94
|
|
|
\DateTimeInterface $lastAccessDate = null, |
95
|
|
|
$questionCount = null, |
96
|
|
|
$reputation = null, |
97
|
|
|
$siteName = null, |
98
|
|
|
$siteUrl = null, |
99
|
|
|
array $topAnswers = [], |
100
|
|
|
array $topQuestions = [], |
101
|
|
|
$userType = null |
102
|
|
|
) { |
103
|
|
|
$this->id = $id; |
104
|
|
|
$this->answerCount = $answerCount; |
105
|
|
|
$this->badgeCounts = $badgeCounts; |
106
|
|
|
$this->creationDate = $creationDate; |
107
|
|
|
$this->lastAccessDate = $lastAccessDate; |
108
|
|
|
$this->questionCount = $questionCount; |
109
|
|
|
$this->reputation = $reputation; |
110
|
|
|
$this->siteName = $siteName; |
111
|
|
|
$this->siteUrl = $siteUrl; |
112
|
|
|
$this->topAnswers = $topAnswers; |
113
|
|
|
$this->topQuestions = $topQuestions; |
114
|
|
|
$this->userType = $userType; |
115
|
|
|
$this->setUserType($userType); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getId() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
return $this->id; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function setId($id) |
124
|
|
|
{ |
125
|
|
|
$this->id = $id; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getAnswerCount() |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
return $this->answerCount; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function setAnswerCount($answerCount) |
136
|
|
|
{ |
137
|
|
|
$this->answerCount = $answerCount; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getBadgeCounts() |
143
|
|
|
{ |
144
|
|
|
return $this->badgeCounts; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function setBadgeCounts(BadgeCount $badgeCounts) |
148
|
|
|
{ |
149
|
|
|
$this->badgeCounts = $badgeCounts; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function getCreationDate() |
155
|
|
|
{ |
156
|
|
|
return $this->creationDate; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function setCreationDate(\DateTimeInterface $creationDate) |
160
|
|
|
{ |
161
|
|
|
$this->creationDate = $creationDate; |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function getLastAccessDate() |
167
|
|
|
{ |
168
|
|
|
return $this->lastAccessDate; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function setLastAccessDate(\DateTimeInterface $lastAccessDate) |
172
|
|
|
{ |
173
|
|
|
$this->lastAccessDate = $lastAccessDate; |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function getQuestionCount() |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
return $this->questionCount; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function setQuestionCount($questionCount) |
184
|
|
|
{ |
185
|
|
|
$this->questionCount = $questionCount; |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function getReputation() |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
return $this->reputation; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function setReputation($reputation) |
196
|
|
|
{ |
197
|
|
|
$this->reputation = $reputation; |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function getSiteName() |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
return $this->siteName; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function setSiteName($siteName) |
208
|
|
|
{ |
209
|
|
|
$this->siteName = $siteName; |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function getSiteUrl() |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
return $this->siteUrl; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function setSiteUrl($siteUrl) |
220
|
|
|
{ |
221
|
|
|
$this->siteUrl = $siteUrl; |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function getTopAnswers() |
227
|
|
|
{ |
228
|
|
|
return $this->topAnswers; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function setTopAnswers($topAnswers) |
232
|
|
|
{ |
233
|
|
|
$this->topAnswers = $topAnswers; |
234
|
|
|
|
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function getTopQuestions() |
239
|
|
|
{ |
240
|
|
|
return $this->topQuestions; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function setTopQuestions($topQuestions) |
244
|
|
|
{ |
245
|
|
|
$this->topQuestions = $topQuestions; |
246
|
|
|
|
247
|
|
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function getUserType() |
|
|
|
|
251
|
|
|
{ |
252
|
|
|
return $this->userType; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
View Code Duplication |
public function setUserType($userType) |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
if (in_array($userType, [ |
258
|
|
|
self::USER_TYPE_DOES_NOT_EXIST, |
259
|
|
|
self::USER_TYPE_MODERATOR, |
260
|
|
|
self::USER_TYPE_REGISTERED, |
261
|
|
|
self::USER_TYPE_UNREGISTERED, |
262
|
|
|
], true)) { |
263
|
|
|
$this->userType = $userType; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $this; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.