Character::createFromArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Igorsgm\TibiaDataApi\Models;
4
5
use Carbon\Carbon;
6
use Igorsgm\TibiaDataApi\Models\Character\AccountInformation;
7
use Igorsgm\TibiaDataApi\Models\Character\Achievement;
8
use Igorsgm\TibiaDataApi\Models\Character\Death;
9
use Igorsgm\TibiaDataApi\Models\Character\Guild;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Igorsgm\TibiaDataApi\Models\Guild.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use Igorsgm\TibiaDataApi\Models\Character\OtherCharacter;
11
use Igorsgm\TibiaDataApi\Traits\ImmutableTrait;
12
use Igorsgm\TibiaDataApi\Traits\SerializableTrait;
13
use Illuminate\Support\Collection;
14
15
class Character
16
{
17
    use ImmutableTrait, SerializableTrait;
18
19
    /**
20
     * @var string
21
     */
22
    private $name;
23
24
    /**
25
     * @var Collection
26
     */
27
    private $formerNames;
28
29
    /**
30
     * @var string
31
     */
32
    private $sex;
33
34
    /**
35
     * @var string
36
     */
37
    private $vocation;
38
39
    /**
40
     * @var int
41
     */
42
    private $level;
43
44
    /**
45
     * @var string
46
     */
47
    private $achievementPoints;
48
49
    /**
50
     * @var string
51
     */
52
    private $world;
53
54
    /**
55
     * @var string
56
     */
57
    private $formerWorld;
58
59
    /**
60
     * @var string
61
     */
62
    private $residence;
63
64
    /**
65
     * @var Guild
66
     */
67
    private $guild;
68
69
    /**
70
     * @var Carbon
71
     */
72
    private $lastLogin;
73
74
    /**
75
     * @var string
76
     */
77
    private $comment = '';
78
79
    /**
80
     * @var string
81
     */
82
    private $accountStatus;
83
84
    /**
85
     * @var string
86
     */
87
    private $status;
88
89
    /**
90
     * @var Collection
91
     */
92
    private $achievements;
93
94
    /**
95
     * @var Collection
96
     */
97
    private $deaths;
98
99
    /**
100
     * @var AccountInformation
101
     */
102
    private $accountInformation;
103
104
    /**
105
     * @var Collection
106
     */
107
    private $otherCharacters;
108
109
    /**
110
     * Character constructor.
111
     * @param  string  $name
112
     * @param  string  $vocation
113
     * @param  int  $level
114
     * @throws \Igorsgm\TibiaDataApi\Exceptions\ImmutableException
115
     */
116
    public function __construct(string $name, string $vocation, int $level)
117
    {
118
        $this->handleImmutableConstructor();
119
120
        $this->name = $name;
121
        $this->vocation = $vocation;
122
        $this->level = $level;
123
    }
124
125
    /**
126
     * @param  array  $response
127
     * @return Character
128
     * @throws \Igorsgm\TibiaDataApi\Exceptions\ImmutableException
129
     */
130
    public static function createFromArray(array $response): Character
131
    {
132
        $character = new Character($response['name'], $response['vocation'], $response['level']);
133
134
        $character->formerNames = $response['former_names'];
135
        $character->sex = $response['sex'];
136
        $character->achievementPoints = $response['achievement_points'];
137
        $character->world = $response['world'];
138
        $character->formerWorld = $response['former_world'];
139
        $character->residence = $response['residence'];
140
        $character->guild = $response['guild'];
141
        $character->lastLogin = $response['last_login'];
142
        $character->comment = $response['comment'];
143
        $character->accountStatus = $response['account_status'];
144
        $character->status = $response['status'];
145
        $character->achievements = $response['achievements'];
146
        $character->accountInformation = $response['account_information'];
147
        $character->deaths = $response['deaths'];
148
        $character->otherCharacters = $response['other_characters'];
149
150
        return $character;
151
    }
152
153
    /**
154
     * Returns true when characters has a premium account.
155
     * @return bool
156
     */
157
    public function isPremium()
158
    {
159
        return $this->accountStatus === 'Premium Account';
160
    }
161
162
    /**
163
     * @return bool Returns true when character is online.
164
     */
165
    public function isOnline()
166
    {
167
        return $this->status === 'online';
168
    }
169
170
    /**
171
     * @return string
172
     */
173
    public function getName(): string
174
    {
175
        return $this->name;
176
    }
177
178
    /**
179
     * @return array
180
     */
181
    public function getFormerNames(): Collection
182
    {
183
        return $this->formerNames;
184
    }
185
186
    /**
187
     * @return string
188
     */
189
    public function getSex(): string
190
    {
191
        return $this->sex;
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    public function getVocation(): string
198
    {
199
        return $this->vocation;
200
    }
201
202
    /**
203
     * @return int
204
     */
205
    public function getLevel(): int
206
    {
207
        return $this->level;
208
    }
209
210
    /**
211
     * @return int
212
     */
213
    public function getAchievementPoints(): int
214
    {
215
        return $this->achievementPoints;
216
    }
217
218
    /**
219
     * @return string
220
     */
221
    public function getWorld(): string
222
    {
223
        return $this->world;
224
    }
225
226
    /**
227
     * @return string|null
228
     */
229
    public function getFormerWorld(): ?string
230
    {
231
        return $this->formerWorld;
232
    }
233
234
    /**
235
     * @return string
236
     */
237
    public function getResidence(): string
238
    {
239
        return $this->residence;
240
    }
241
242
    /**
243
     * @return Guild|null
244
     */
245
    public function getGuild(): ?Guild
246
    {
247
        return $this->guild;
248
    }
249
250
    /**
251
     * @return Carbon|null
252
     */
253
    public function getLastLogin(): ?Carbon
254
    {
255
        return $this->lastLogin;
256
    }
257
258
    /**
259
     * @return string
260
     */
261
    public function getComment(): string
262
    {
263
        return $this->comment;
264
    }
265
266
    /**
267
     * @return string
268
     */
269
    public function getAccountStatus(): string
270
    {
271
        return $this->accountStatus;
272
    }
273
274
    /**
275
     * @return string
276
     */
277
    public function getStatus(): string
278
    {
279
        return $this->status;
280
    }
281
282
    /**
283
     * @return Achievement[]
284
     */
285
    public function getAchievements(): Collection
286
    {
287
        return $this->achievements;
288
    }
289
290
    /**
291
     * @return Death[]
292
     */
293
    public function getDeaths(): Collection
294
    {
295
        return $this->deaths;
296
    }
297
298
    /**
299
     * @return AccountInformation
300
     */
301
    public function getAccountInformation(): AccountInformation
302
    {
303
        return $this->accountInformation;
304
    }
305
306
    /**
307
     * @return OtherCharacter[]
308
     */
309
    public function getOtherCharacters(): Collection
310
    {
311
        return $this->otherCharacters;
312
    }
313
}
314