Completed
Pull Request — master (#2)
by Craig
02:58
created

Response   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 27
c 3
b 1
f 0
dl 0
loc 62
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 4
A parseResponseBody() 0 16 4
1
<?php
2
3
namespace Phpsa\LaravelYourlsPlugin;
4
5
class Response
6
{
7
8
    /**
9
     * status of the server response - fail is not nessisary a fail in this case.
10
     *
11
     * @var string
12
     */
13
    public $status;
14
15
    /**
16
     * status code of the server response.
17
     *
18
     * @var int
19
     */
20
    public $statusCode;
21
22
    /**
23
     * message from the response.
24
     *
25
     * @var string
26
     * @author Craig Smith <[email protected]>
27
     */
28
    public $message;
29
30
31
    public function __construct($body, $format)
32
    {
33
        switch ($format) {
34
            case 'xml':
35
                libxml_use_internal_errors(true);
36
                $res = simplexml_load_string((string) $body);
37
                if ($res) {
38
                    $res = json_decode(json_encode($res));
39
                }
40
                break;
41
            case 'json':
42
                $res = json_decode((string) $body);
43
                break;
44
            default:
45
                $res = (string) $body;
46
            break;
47
        }
48
        $this->parseResponseBody($res);
49
    }
50
51
    protected function parseResponseBody($body)
52
    {
53
        if(empty($body)) {
54
            throw new \Exception('No response recieved');
55
        }
56
57
        if (\is_string($body)) {
58
            $this->status = 'success';
59
            $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...
60
            $this->shorturl = $url;
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $url seems to be never defined.
Loading history...
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...
61
62
            return;
63
        }
64
65
        foreach ((array) $body as $key => $value) {
66
            $this->{$key} = $value;
67
        }
68
69
    }
70
71
}
72