HighscoresResponse::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Igorsgm\TibiaDataApi\Response;
4
5
use Igorsgm\TibiaDataApi\Exceptions\NotFoundException;
6
use Igorsgm\TibiaDataApi\Models\Highscores;
7
use Igorsgm\TibiaDataApi\Models\Highscores\Character;
8
9
/**
10
 * Class HighscoresResponse
11
 * @package Igorsgm\TibiaDataApi\Response
12
 */
13
class HighscoresResponse extends AbstractResponse
14
{
15
    /**
16
     * @var Highscores
17
     */
18
    private $highscores;
19
20
    /**
21
     * HighscoresResponse constructor.
22
     * @param  \stdClass  $response
23
     * @throws NotFoundException
24
     * @throws \Igorsgm\TibiaDataApi\Exceptions\ImmutableException
25
     */
26
    public function __construct(\stdClass $response)
27
    {
28
        if (isset($response->highscores->data->error)) {
29
            throw new NotFoundException('World does not exists.');
30
        }
31
32
        $highscores = collect();
33
        foreach ($response->highscores->data as $highscore) {
34
            $highscores->push(new Character(
35
                $highscore->name,
36
                $highscore->rank,
37
                $highscore->vocation,
38
                $highscore->points ?? null,
39
                $highscore->level ?? null
40
            ));
41
        }
42
43
        $this->highscores = new Highscores(
44
            $response->highscores->filters->world,
45
            $response->highscores->filters->category,
46
            $highscores
47
        );
48
49
        parent::__construct($response);
50
    }
51
52
    /**
53
     * @return Highscores
54
     */
55
    public function getHighscores(): Highscores
56
    {
57
        return $this->highscores;
58
    }
59
}
60