Response::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 4
eloc 14
c 3
b 2
f 0
nc 4
nop 2
dl 0
loc 18
rs 9.7998
1
<?php
2
3
namespace Phpsa\LaravelYourlsPlugin;
4
5
class Response
6
{
7
    /**
8
     * status of the server response - fail is not nessisary a fail in this case.
9
     *
10
     * @var string
11
     */
12
    public $status;
13
14
    /**
15
     * status code of the server response.
16
     *
17
     * @var int
18
     */
19
    public $statusCode;
20
21
    /**
22
     * message from the response.
23
     *
24
     * @var string
25
     * @author Craig Smith <[email protected]>
26
     */
27
    public $message;
28
29
    public function __construct($body, $format)
30
    {
31
        switch ($format) {
32
            case 'xml':
33
                libxml_use_internal_errors(true);
34
                $res = simplexml_load_string((string) $body);
35
                if ($res) {
36
                    $res = json_decode(json_encode($res));
37
                }
38
                break;
39
            case 'json':
40
                $res = json_decode((string) $body);
41
                break;
42
            default:
43
                $res = (string) $body;
44
            break;
45
        }
46
        $this->parseResponseBody($res);
47
    }
48
49
    protected function parseResponseBody($body)
50
    {
51
        if (empty($body)) {
52
            throw new \Exception('No response recieved');
53
        }
54
55
        if (\is_string($body)) {
56
            $this->status = 'success';
57
            $this->statusCode = 200;
58
            $this->shorturl = $body;
59
60
            return;
61
        }
62
63
        foreach ((array) $body as $key => $value) {
64
            $this->{$key} = $value;
65
        }
66
    }
67
}
68