Passed
Branch init (10e2f1)
by Pulkit
05:32
created

Response::send()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 8.9137
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 6
1
<?php
2
namespace Sourceout\LastFm\Http;
3
4
use Tightenco\Collect\Support\Collection;
5
use Sourceout\LastFm\Http\ResponseInterface;
6
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
7
use Sourceout\LastFm\Providers\LastFm\Exception\LastFmException;
8
9
class Response implements ResponseInterface
10
{
11
    /** @inheritDoc */
12 5
    public static function send(
13
        PsrResponseInterface $response
14
    ) : Collection
15
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
16 5
        $returnValue = new Collection();
0 ignored issues
show
Unused Code introduced by
The assignment to $returnValue is dead and can be removed.
Loading history...
17
18 5
        if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== false) {
19 4
            $contents = json_decode($response->getBody()->getContents(), true);
20
            if (
21 4
                ($response->getStatusCode() === 200)
22 4
                && ! isset($contents['error'])
23
            ) {
24 3
                $returnValue = new Collection($contents);
25
            } else {
26 1
                $code = isset($contents['error'])?$contents['error']:0;
27 1
                $message = isset($contents['message'])?$contents['message']:'';
28 4
                throw new LastFmException($message, $code);
29
            }
30
        } else {
31 1
            $responseType = $response->getHeaderLine('Content-Type');
32 1
            throw new \InvalidArgumentException("Invalid response type {$responseType}");
33
        }
34
35 3
        return $returnValue;
36
    }
37
}