GuildsResponse::__construct()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
cc 5
nc 5
nop 1
1
<?php
2
3
namespace Igorsgm\TibiaDataApi\Response;
4
5
use Igorsgm\TibiaDataApi\Exceptions\NotFoundException;
6
use Igorsgm\TibiaDataApi\Models\Guilds;
7
use Igorsgm\TibiaDataApi\Models\Guilds\Guild;
8
9
/**
10
 * @see https://tibiadata.com/doc-api-v2/guilds/
11
 */
12
class GuildsResponse extends AbstractResponse
13
{
14
15
    /**
16
     * @var Guilds
17
     */
18
    private $guilds;
19
20
    /**
21
     * GuildsResponse constructor.
22
     * @param  \stdClass  $response
23
     * @throws NotFoundException
24
     * @throws \Igorsgm\TibiaDataApi\Exceptions\ImmutableException
25
     */
26
    public function __construct(\stdClass $response)
27
    {
28
        if (empty($response->guilds->active) && empty($response->guilds->formation)) {
29
            throw new NotFoundException('Guilds do not exists. Are you sure server name is valid?');
30
        }
31
32
        $active = collect();
33
        foreach ($response->guilds->active as $activeGuild) {
34
            $active->push(new Guild($activeGuild->name, $activeGuild->desc, $activeGuild->guildlogo, true));
35
        }
36
37
        $formation = collect();
38
        foreach ($response->guilds->formation as $inactiveGuild) {
39
            $formation->push(new Guild($inactiveGuild->name, $inactiveGuild->desc, $inactiveGuild->guildlogo, false));
40
        }
41
42
        $this->guilds = new Guilds($response->guilds->world, $active, $formation);
43
44
        parent::__construct($response);
45
    }
46
47
    /**
48
     * @return Guilds
49
     */
50
    public function getGuilds(): Guilds
51
    {
52
        return $this->guilds;
53
    }
54
}
55