HousesResponse::getHouses()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Igorsgm\TibiaDataApi\Response;
4
5
use Igorsgm\TibiaDataApi\Exceptions\NotFoundException;
6
use Igorsgm\TibiaDataApi\Models\Houses;
7
use Igorsgm\TibiaDataApi\Models\Houses\House;
8
9
/**
10
 * Class HousesResponse
11
 * @package Igorsgm\TibiaDataApi\Response
12
 */
13
class HousesResponse extends AbstractResponse
14
{
15
    /**
16
     * @var Houses
17
     */
18
    private $houses;
19
20
    /**
21
     * HousesResponse 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->houses->houses)) {
29
            throw new NotFoundException('World does not exists.');
30
        }
31
32
        if (count($response->houses->houses) === 1
33
            && $response->houses->houses[0]->name === 'No houses or flats found.'
34
        ) {
35
            throw new NotFoundException('Town seems to not exists. Are you sure you type town that has houses?');
36
        }
37
38
        $houses = collect();
39
        foreach ($response->houses->houses as $house) {
40
            $houses->push(new House(
41
                $house->houseid,
42
                $house->name,
43
                $house->size,
44
                $house->rent,
45
                $house->status
46
            ));
47
        }
48
49
        $this->houses = new Houses($response->houses->town, $response->houses->world, $response->houses->type, $houses);
50
51
        parent::__construct($response);
52
    }
53
54
    /**
55
     * @return Houses
56
     */
57
    public function getHouses(): Houses
58
    {
59
        return $this->houses;
60
    }
61
}
62