GuildResponse::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 8.3158
c 0
b 0
f 0
cc 6
nc 7
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Igorsgm\TibiaDataApi\Response;
4
5
use Carbon\Carbon;
6
use Igorsgm\TibiaDataApi\Exceptions\NotFoundException;
7
use Igorsgm\TibiaDataApi\Models\Guild;
8
use Igorsgm\TibiaDataApi\Models\Guild\Character;
9
use Igorsgm\TibiaDataApi\Models\Guild\Guildhall;
10
use Igorsgm\TibiaDataApi\Models\Guild\Invited;
11
use Igorsgm\TibiaDataApi\Models\Guild\Invitee;
12
use Igorsgm\TibiaDataApi\Models\Guild\Members;
13
14
/**
15
 * @see https://tibiadata.com/doc-api-v2/guilds/
16
 */
17
class GuildResponse extends AbstractResponse
18
{
19
20
    /**
21
     * @var Guild
22
     */
23
    private $guild;
24
25
    /**
26
     * GuildResponse constructor.
27
     * @param  \stdClass  $response
28
     * @throws NotFoundException
29
     * @throws \Igorsgm\TibiaDataApi\Exceptions\ImmutableException
30
     * @throws \Exception
31
     */
32
    public function __construct(\stdClass $response)
33
    {
34
        if (isset($response->guild->error)) {
35
            throw new NotFoundException('Guild does not exists.');
36
        }
37
38
        $invitees = collect();
39
        foreach ($response->guild->invited as $invitee) {
40
            $invitees->push(new Invitee($invitee->name, new Carbon($invitee->invited)));
41
        }
42
        $invited = new Invited($invitees);
43
44
        $members = collect();
45
        $characters = collect();
46
        foreach ($response->guild->members as $members_data) {
47
            foreach ($members_data->characters as $character) {
48
                $characters->push(new Character(
49
                    $character->name,
50
                    $character->nick,
51
                    $character->level,
52
                    $character->vocation,
53
                    new Carbon($character->joined),
54
                    $character->status
55
                ));
56
            }
57
58
            $members->push(new Members($members_data->rank_title, $characters));
59
        }
60
61
        $this->guild = Guild::createFromArray([
62
            'name' => $response->guild->data->name,
63
            'description' => $response->guild->data->description,
64
            'guildhall' => $response->guild->data->guildhall ? new Guildhall(
65
                $response->guild->data->guildhall->name,
66
                $response->guild->data->guildhall->town,
67
                new Carbon($response->guild->data->guildhall->paid),
68
                $response->guild->data->guildhall->world,
69
                $response->guild->data->guildhall->houseid
70
            ) : null,
71
            'application' => $response->guild->data->application,
72
            'war' => $response->guild->data->war,
73
            'online_status' => $response->guild->data->online_status,
74
            'offline_status' => $response->guild->data->offline_status,
75
            'disbanded' => $response->guild->data->disbanded,
76
            'totalmembers' => $response->guild->data->totalmembers,
77
            'totalinvited' => $response->guild->data->totalinvited,
78
            'world' => $response->guild->data->world,
79
            'founded' => new Carbon($response->guild->data->founded),
80
            'active' => $response->guild->data->active ?? null,
81
            'homepage' => $response->guild->data->homepage ?? '',
82
            'guildlogo' => $response->guild->data->guildlogo,
83
            'members' => $members,
84
            'invited' => $invited,
85
        ]);
86
87
        parent::__construct($response);
88
    }
89
90
    /**
91
     * @return Guild
92
     */
93
    public function getGuild(): Guild
94
    {
95
        return $this->guild;
96
    }
97
}
98