Test Setup Failed
Push — master ( 92137c...5836d1 )
by Craig
44s queued 11s
created

Response::__construct()   A

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';
0 ignored issues
show
Documentation Bug introduced by
The property $statusCode was declared of type integer, but '200' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
58
            $this->shorturl = $url;
1 ignored issue
show
Bug Best Practice introduced by
The property shorturl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Comprehensibility Best Practice introduced by
The variable $url seems to be never defined.
Loading history...
59
60
            return;
61
        }
62
63
        foreach ((array) $body as $key => $value) {
64
            $this->{$key} = $value;
65
        }
66
    }
67
}
68