|
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\Message\Request\AbstractRequest; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Response wrapper |
|
18
|
|
|
* @package Bee4\Transport\Message |
|
19
|
|
|
*/ |
|
20
|
|
|
class Response extends AbstractMessage |
|
21
|
|
|
{ |
|
22
|
|
|
use WithBodyTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The request which allow to generate this response |
|
26
|
|
|
* @var AbstractRequest |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $request; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* HTTP Status code |
|
32
|
|
|
* @var integer |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $status; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* HTTP total transaction time in second |
|
38
|
|
|
* @var double |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $transactionTime; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Build the response with Request dependency injection |
|
44
|
|
|
* @param AbstractRequest $request |
|
45
|
|
|
*/ |
|
46
|
18 |
|
public function __construct(AbstractRequest $request = null) |
|
47
|
|
|
{ |
|
48
|
18 |
|
$this->request = $request; |
|
49
|
18 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Set the linked request |
|
53
|
|
|
* @param AbstractRequest $request |
|
54
|
|
|
* @return Response |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function setRequest(AbstractRequest $request) |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->request = $request; |
|
59
|
1 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return AbstractRequest |
|
64
|
|
|
*/ |
|
65
|
7 |
|
public function getRequest() |
|
66
|
|
|
{ |
|
67
|
7 |
|
return $this->request; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param int $code |
|
72
|
|
|
* @return Response |
|
73
|
|
|
*/ |
|
74
|
11 |
|
public function setStatus($code) |
|
75
|
|
|
{ |
|
76
|
11 |
|
$this->status = (int)$code; |
|
77
|
11 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return int |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function getStatus() |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->status; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getStatusMessage() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->request->getStatusMessage($this->status); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param double $time |
|
98
|
|
|
* @return Response |
|
99
|
|
|
* @throws \RuntimeException |
|
100
|
|
|
*/ |
|
101
|
13 |
|
public function setTransactionTime($time) |
|
102
|
|
|
{ |
|
103
|
13 |
|
if (!is_numeric($time) || $time < 0) { |
|
104
|
2 |
|
throw new \RuntimeException("Invalid response time format"); |
|
105
|
|
|
} |
|
106
|
11 |
|
$this->transactionTime = (float)$time; |
|
107
|
11 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return double |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function getTransactionTime() |
|
114
|
|
|
{ |
|
115
|
1 |
|
return $this->transactionTime; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Try to json_decode the response body |
|
120
|
|
|
* @return string |
|
121
|
|
|
* @throws \RuntimeException |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function json() |
|
124
|
|
|
{ |
|
125
|
1 |
|
$json = json_decode($this->getBody(), true); |
|
126
|
1 |
|
if ($json === null) { |
|
127
|
1 |
|
throw new \RuntimeException( |
|
128
|
|
|
"Can't decode JSON response: ".$json |
|
129
|
1 |
|
); |
|
130
|
|
|
} |
|
131
|
1 |
|
return $json; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|