Passed
Branch init (4c4ee0)
by Pulkit
02:43
created

Response::send()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.0111
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
        if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== false) {
17 4
            $contents = json_decode($response->getBody()->getContents(), true);
18
            if (
19 4
                ($response->getStatusCode() === 200)
20 4
                && ! isset($contents['error'])
21
            ) {
22 3
                return new Collection($contents);
23
            } else {
24 1
                $code = isset($contents['error'])?$contents['error']:0;
25 1
                $message = isset($contents['message'])?$contents['message']:'';
26 1
                throw new LastFmException($message, $code);
27
            }
28
        } else {
29 1
            $responseType = $response->getHeaderLine('Content-Type');
30 1
            throw new \InvalidArgumentException("Invalid response type {$responseType}");
31
        }
32
    }
33
}