WorldResponse::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.36
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Igorsgm\TibiaDataApi\Response;
4
5
use Carbon\Carbon;
6
use Igorsgm\TibiaDataApi\Exceptions\NotFoundException;
7
use Igorsgm\TibiaDataApi\Models\World;
8
use Igorsgm\TibiaDataApi\Models\World\Character;
9
use Igorsgm\TibiaDataApi\Models\World\OnlineRecord;
10
11
class WorldResponse extends AbstractResponse
12
{
13
    /**
14
     * @var World
15
     */
16
    private $world;
17
18
    /**
19
     * WorldResponse constructor.
20
     *
21
     * @param  \stdClass  $response
22
     * @throws NotFoundException
23
     * @throws \Igorsgm\TibiaDataApi\Exceptions\ImmutableException
24
     * @throws \Exception
25
     */
26
    public function __construct(\stdClass $response)
27
    {
28
        if (!isset($response->world->world_information->pvp_type)) {
29
            throw new NotFoundException('World does not exists.');
30
        }
31
32
        $playersOnline = collect();
33
        foreach ($response->world->players_online as $player) {
34
            $playersOnline->push(new Character(
35
                $player->name,
36
                $player->level,
37
                $player->vocation
38
            ));
39
        }
40
41
        $this->world = World::createFromArray([
42
            'name' => $response->world->world_information->name,
43
            'online' => $response->world->world_information->players_online,
44
            'creation_date' => $response->world->world_information->creation_date,
45
            'location' => $response->world->world_information->location,
46
            'pvp_type' => $response->world->world_information->pvp_type,
47
            'battleye_status' => $response->world->world_information->battleye_status,
48
            'online_record' => new OnlineRecord(
49
                $response->world->world_information->online_record->players,
50
                new Carbon(
51
                    $response->world->world_information->online_record->date->date,
52
                    $response->world->world_information->online_record->date->timezone
53
                )
54
            ),
55
            'players_online' => $playersOnline,
56
            'world_quest_titles' => $response->world->world_information->world_quest_titles,
57
        ]);
58
59
        parent::__construct($response);
60
    }
61
62
    /**
63
     * @return World
64
     */
65
    public function getWorld(): World
66
    {
67
        return $this->world;
68
    }
69
}
70