Passed
Branch master (203e41)
by Nate
02:00
created

RetrofitResponse   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 107
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A raw() 0 4 1
A code() 0 4 1
A message() 0 4 1
A headers() 0 4 1
A isSuccessful() 0 4 2
A body() 0 4 1
A errorBody() 0 4 1
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Retrofit\Internal;
10
11
use Psr\Http\Message\ResponseInterface;
12
use Tebru\Retrofit\Response;
13
14
/**
15
 * Wraps a PSR-7 [@see ResponseInterface] and provides convenience methods for getting
16
 * a converted success or error body.
17
 *
18
 * @author Nate Brunette <[email protected]>
19
 */
20
final class RetrofitResponse implements Response
21
{
22
    /**
23
     * The PSR-7 response
24
     *
25
     * @var ResponseInterface
26
     */
27
    private $response;
28
29
    /**
30
     * Converted body on success
31
     *
32
     * @var mixed
33
     */
34
    private $body;
35
36
    /**
37
     * Converted body on failure
38
     *
39
     * @var mixed
40
     */
41
    private $errorBody;
42
43
    /**
44
     * Constructor
45
     *
46
     * @param ResponseInterface $response
47
     * @param mixed $body
48
     * @param mixed $errorBody
49
     */
50 16
    public function __construct(ResponseInterface $response, $body, $errorBody)
51
    {
52 16
        $this->response = $response;
53 16
        $this->body = $body;
54 16
        $this->errorBody = $errorBody;
55 16
    }
56
57
    /**
58
     * Get the raw PSR-7 response
59
     *
60
     * @return ResponseInterface
61
     */
62 6
    public function raw(): ResponseInterface
63
    {
64 6
        return $this->response;
65
    }
66
67
    /**
68
     * Get the response status code
69
     *
70
     * @return int
71
     */
72 2
    public function code(): int
73
    {
74 2
        return $this->response->getStatusCode();
75
    }
76
77
    /**
78
     * Get the response message
79
     *
80
     * @return string
81
     */
82 2
    public function message(): string
83
    {
84 2
        return $this->response->getReasonPhrase();
85
    }
86
87
    /**
88
     * Get response headers
89
     *
90
     * @return array
91
     */
92 2
    public function headers(): array
93
    {
94 2
        return $this->response->getHeaders();
95
    }
96
97
    /**
98
     * Returns true if the response was successful
99
     *
100
     * @return bool
101
     */
102 2
    public function isSuccessful(): bool
103
    {
104 2
        return $this->response->getStatusCode() >= 200 && $this->response->getStatusCode() < 300;
105
    }
106
107
    /**
108
     * Get converted body
109
     *
110
     * @return mixed
111
     */
112 7
    public function body()
113
    {
114 7
        return $this->body;
115
    }
116
117
    /**
118
     * Get converted body on errors
119
     *
120
     * @return mixed
121
     */
122 6
    public function errorBody()
123
    {
124 6
        return $this->errorBody;
125
    }
126
}
127