Completed
Pull Request — master (#18)
by Stéphane
14:50 queued 10:12
created

Response::setExecutionInfos()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
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\Handle\ExecutionInfos;
15
use Bee4\Transport\Exception\RuntimeException;
16
use Bee4\Transport\Message\Request\AbstractRequest;
17
18
/**
19
 * Response wrapper
20
 * @package Bee4\Transport\Message
21
 */
22
class Response extends AbstractMessage
23
{
24
    use WithBodyTrait;
25
26
    /**
27
     * The request which allow to generate this response
28
     * @var AbstractRequest
29
     */
30
    protected $request;
31
32
    /**
33
     * The execution details which helped to build the request
34
     * @var ExecutionInfos
35
     */
36
    protected $infos;
37
38
    /**
39
     * HTTP Status code
40
     * @var integer
41
     */
42
    protected $status;
43
44
    /**
45
     * HTTP total transaction time in second
46
     * @var double
47 18
     */
48
    protected $transactionTime;
49 18
50 18
    /**
51 18
     * Build the response with Request dependency injection
52
     * @param AbstractRequest $request
53
     */
54
    public function __construct(AbstractRequest $request = null)
55
    {
56
        parent::__construct();
57
        $this->request = $request;
58 1
    }
59
60 1
    /**
61 1
     * Set the linked request
62
     * @param AbstractRequest $request
63
     * @return Response
64
     */
65
    public function setRequest(AbstractRequest $request)
66
    {
67 7
        $this->request = $request;
68
        return $this;
69 7
    }
70
71
    /**
72
     * @return AbstractRequest
73
     */
74
    public function getRequest()
75
    {
76 11
        return $this->request;
77
    }
78 11
79 11
    /**
80
     * Set the execution infos
81
     * @param ExecutionInfos $infos
82
     * @return Response
83
     */
84
    public function setExecutionInfos(ExecutionInfos $infos)
85 1
    {
86
        $this->infos = $infos;
87 1
        return $this;
88
    }
89
90
    /**
91
     * @return ExecutionInfos
92
     */
93
    public function getExecutionInfos()
94
    {
95
        return $this->infos;
96
    }
97
98
    /**
99
     * @param int $code
100
     * @return Response
101
     */
102
    public function setStatus($code)
103 13
    {
104
        $this->status = (int)$code;
105 13
        return $this;
106 2
    }
107
108 11
    /**
109 11
     * @return int
110
     */
111
    public function getStatus()
112
    {
113
        return $this->status;
114
    }
115 1
116
    /**
117 1
     * @return string
118
     */
119
    public function getStatusMessage()
120
    {
121
        return $this->request->getStatusMessage($this->status);
122
    }
123
124
    /**
125 1
     * @param double $time
126
     * @return Response
127 1
     * @throws RuntimeException
128 1
     */
129 1
    public function setTransactionTime($time)
130 1
    {
131 1
        if (!is_numeric($time) || $time < 0) {
132 1
            throw new RuntimeException("Invalid response time format");
133
        }
134 1
        $this->transactionTime = (float)$time;
135
        return $this;
136 1
    }
137
138
    /**
139
     * @return double
140
     */
141
    public function getTransactionTime()
142
    {
143
        return $this->transactionTime;
144
    }
145
146
    /**
147
     * Try to json_decode the response body
148
     * @return string
149
     * @throws RuntimeException
150
     */
151
    public function json()
152
    {
153
        $json = json_decode($this->getBody(), true);
154
        if ($json === null) {
155
            throw new RuntimeException(sprintf(
156
                "Can't decode response as JSON: %s",
157
                function_exists('json_last_error_msg')?
158
                    json_last_error_msg():
159
                    'Error code '.json_last_error()
160
            ));
161
        }
162
        return $json;
163
    }
164
}
165