Completed
Push — master ( aa2e8f...b6326a )
by Radu
04:41
created

AbstractResponse::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WebServCo\Api;
3
4
abstract class AbstractResponse
5
{
6
    protected $endpoint;
7
    protected $data;
8
    protected $method;
9
    protected $response; // \WebServCo\Framework\Http\Response
10
    protected $status;
11
12
    public function __construct($endpoint, $method, \WebServCo\Framework\Http\Response $response)
13
    {
14
        $this->endpoint = $endpoint;
15
        $this->method = $method;
16
        $this->response = $response;
17
        $this->status = $this->response->getStatus();
18
        $this->data = $this->processResponseData();
19
    }
20
21
    public function getData()
22
    {
23
        return $this->data;
24
    }
25
26
    public function getEndpoint()
27
    {
28
        return $this->endpoint;
29
    }
30
31
    public function getMethod()
32
    {
33
        return $this->method;
34
    }
35
36
    public function getStatus()
37
    {
38
        return $this->status;
39
    }
40
41
    protected function processResponseData()
42
    {
43
        $responseContent = $this->response->getContent();
44
        $contentType = $this->response->getHeader('Content-Type');
45
        $parts = explode(';', $contentType);
0 ignored issues
show
Bug introduced by
It seems like $contentType can also be of type false; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $parts = explode(';', /** @scrutinizer ignore-type */ $contentType);
Loading history...
46
47
        switch ($parts[0]) {
48
            case 'application/json':
49
            case 'text/json':
50
                return json_decode($responseContent, true);
51
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
52
            case 'application/x-www-form-urlencoded':
53
                if (false === strpos($responseContent, '=')) {
54
                    /* Sometimes Discogs returns text/plain with this content type ... */
55
                    return $responseContent;
56
                }
57
                $data = [];
58
                parse_str($responseContent, $data);
59
                return $data;
60
                break;
61
            case 'text/plain':
62
            case 'text/html':
63
                return $responseContent;
64
                break;
65
            default:
66
                throw new \WebServCo\Api\Exceptions\ApiException(
67
                    sprintf('Api returned unsupported content type: %s.', $contentType)
0 ignored issues
show
Bug introduced by
It seems like $contentType can also be of type false; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
                    sprintf('Api returned unsupported content type: %s.', /** @scrutinizer ignore-type */ $contentType)
Loading history...
68
                );
69
                break;
70
        }
71
    }
72
}
73