Completed
Push — master ( 1212b3...010c30 )
by Stéphane
05:43
created

Response::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the bee4/transport package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2015
8
 * @author  Stephane HULARD <[email protected]>
9
 * @package Bee4\Transport\Message
10
 */
11
12
namespace Bee4\Transport\Message;
13
14
use Bee4\Transport\Exception\RuntimeException;
15
use Bee4\Transport\Message\Request\AbstractRequest;
16
17
/**
18
 * Response wrapper
19
 * @package Bee4\Transport\Message
20
 */
21
class Response extends AbstractMessage
22
{
23
    use WithBodyTrait;
24
25
    /**
26
     * The request which allow to generate this response
27
     * @var AbstractRequest
28
     */
29
    protected $request;
30
31
    /**
32
     * HTTP Status code
33
     * @var integer
34
     */
35
    protected $status;
36
37
    /**
38
     * HTTP total transaction time in second
39
     * @var double
40
     */
41
    protected $transactionTime;
42
43
    /**
44
     * Build the response with Request dependency injection
45
     * @param AbstractRequest $request
46
     */
47 18
    public function __construct(AbstractRequest $request = null)
48
    {
49 18
        parent::__construct();
50 18
        $this->request = $request;
51 18
    }
52
53
    /**
54
     * Set the linked request
55
     * @param AbstractRequest $request
56
     * @return Response
57
     */
58 1
    public function setRequest(AbstractRequest $request)
59
    {
60 1
        $this->request = $request;
61 1
        return $this;
62
    }
63
64
    /**
65
     * @return AbstractRequest
66
     */
67 7
    public function getRequest()
68
    {
69 7
        return $this->request;
70
    }
71
72
    /**
73
     * @param int $code
74
     * @return Response
75
     */
76 11
    public function setStatus($code)
77
    {
78 11
        $this->status = (int)$code;
79 11
        return $this;
80
    }
81
82
    /**
83
     * @return int
84
     */
85 1
    public function getStatus()
86
    {
87 1
        return $this->status;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getStatusMessage()
94
    {
95
        return $this->request->getStatusMessage($this->status);
96
    }
97
98
    /**
99
     * @param double $time
100
     * @return Response
101
     * @throws RuntimeException
102
     */
103 13
    public function setTransactionTime($time)
104
    {
105 13
        if (!is_numeric($time) || $time < 0) {
106 2
            throw new RuntimeException("Invalid response time format");
107
        }
108 11
        $this->transactionTime = (float)$time;
109 11
        return $this;
110
    }
111
112
    /**
113
     * @return double
114
     */
115 1
    public function getTransactionTime()
116
    {
117 1
        return $this->transactionTime;
118
    }
119
120
    /**
121
     * Try to json_decode the response body
122
     * @return string
123
     * @throws RuntimeException
124
     */
125 1
    public function json()
126
    {
127 1
        $json = json_decode($this->getBody(), true);
128 1
        if ($json === null) {
129 1
            throw new RuntimeException(sprintf(
130 1
                "Can't decode response as JSON: %s",
131 1
                function_exists('json_last_error_msg')?
132 1
                    json_last_error_msg():
133
                    'Error code '.json_last_error()
134 1
            ));
135
        }
136 1
        return $json;
137
    }
138
}
139