NewsResponse::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
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\News;
8
9
/**
10
 * Class NewsResponse
11
 * @package Igorsgm\TibiaDataApi\Response
12
 */
13
class NewsResponse extends AbstractResponse
14
{
15
16
    /**
17
     * @var News
18
     */
19
    private $news;
20
21
    /**
22
     * NewsResponse constructor.
23
     * @param  \stdClass  $response
24
     * @throws NotFoundException
25
     * @throws \Igorsgm\TibiaDataApi\Exceptions\ImmutableException
26
     * @throws \Exception
27
     */
28
    public function __construct(\stdClass $response)
29
    {
30
        if (isset($response->news->error)) {
31
            throw new NotFoundException('News does not exists.');
32
        }
33
34
        $this->news = new News(
35
            $response->news->id,
36
            $response->news->title,
37
            $response->news->content,
38
            new Carbon($response->news->date->date, $response->news->date->timezone)
39
        );
40
41
        parent::__construct($response);
42
    }
43
44
    /**
45
     * @return News
46
     */
47
    public function getNews(): News
48
    {
49
        return $this->news;
50
    }
51
}
52