World::getBattlEyedAt()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Igorsgm\TibiaDataApi\Models;
4
5
use Carbon\Carbon;
6
use Exception;
7
use Igorsgm\TibiaDataApi\Exceptions\ImmutableException;
8
use Igorsgm\TibiaDataApi\Models\World\Character;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Igorsgm\TibiaDataApi\Models\Character.

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...
9
use Igorsgm\TibiaDataApi\Models\World\OnlineRecord;
10
use Igorsgm\TibiaDataApi\Traits\ImmutableTrait;
11
use Igorsgm\TibiaDataApi\Traits\SerializableTrait;
12
use Illuminate\Support\Collection;
13
14
class World
15
{
16
    use ImmutableTrait, SerializableTrait;
17
18
    /**
19
     * @var string
20
     */
21
    private $name;
22
23
    /**
24
     * @var int
25
     */
26
    private $online;
27
28
    /**
29
     * @var
30
     */
31
    private $onlineRecord;
32
33
    /**
34
     * @var
35
     */
36
    private $creationDate;
37
38
    /**
39
     * @var
40
     */
41
    private $location;
42
43
    /**
44
     * @var
45
     */
46
    private $pvpType;
47
48
    /**
49
     * @var
50
     */
51
    private $worldQuestTitles;
52
53
    /**
54
     * @var
55
     */
56
    private $battleyeStatus;
57
58
    /**
59
     * @var
60
     */
61
    private $playersOnline;
62
63
    /**
64
     * World constructor.
65
     * @param  string  $name
66
     * @param  int  $online
67
     * @throws ImmutableException
68
     */
69
    public function __construct(string $name, int $online)
70
    {
71
        $this->handleImmutableConstructor();
72
73
        $this->name = $name;
74
        $this->online = $online;
75
    }
76
77
    /**
78
     * @param  array  $array
79
     * @return World
80
     * @throws ImmutableException
81
     */
82
    public static function createFromArray(array $array)
83
    {
84
        $world = new self($array['name'], $array['online']);
85
86
        $world->creationDate = $array['creation_date'];
87
        $world->location = $array['location'];
88
        $world->pvpType = $array['pvp_type'];
89
        $world->battleyeStatus = $array['battleye_status'];
90
        $world->onlineRecord = $array['online_record'];
91
        $world->playersOnline = $array['players_online'];
92
        $world->worldQuestTitles = $array['world_quest_titles'];
93
94
        return $world;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getName(): string
101
    {
102
        return $this->name;
103
    }
104
105
    /**
106
     * @return int
107
     */
108
    public function getOnline(): int
109
    {
110
        return $this->online;
111
    }
112
113
    /**
114
     * @return OnlineRecord
115
     */
116
    public function getOnlineRecord(): OnlineRecord
117
    {
118
        return $this->onlineRecord;
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getCreationDate(): string
125
    {
126
        return $this->creationDate;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getLocation(): string
133
    {
134
        return $this->location;
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getPvpType(): string
141
    {
142
        return $this->pvpType;
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    public function getWorldQuestTitles(): Collection
149
    {
150
        return $this->worldQuestTitles;
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function getBattleyeStatus(): string
157
    {
158
        return $this->battleyeStatus;
159
    }
160
161
    /**
162
     * @return Character[]
163
     */
164
    public function getPlayersOnline(): Collection
165
    {
166
        return $this->playersOnline;
167
    }
168
169
    /**
170
     * Gets Carbon from battleye status string.
171
     *
172
     * @return Carbon|null
173
     * @throws Exception
174
     */
175
    public function getBattlEyedAt(): ?Carbon
176
    {
177
        if ($this->battleyeStatus === 'Not protected by BattlEye.') {
178
            return null;
179
        }
180
181
        if ($this->battleyeStatus === 'Protected by BattlEye since its release.') {
182
            return new Carbon($this->creationDate.'-01');
183
        }
184
185
        preg_match('/Protected by BattlEye since ([a-zA-Z0-9, ]+)\./', $this->battleyeStatus, $matches);
186
        return Carbon::createFromFormat('F d, Y', $matches[1]);
187
    }
188
}
189