Completed
Push — feature/configuration ( 48ff2d...765854 )
by Stéphane
53:12 queued 25:50
created

Response::setTransactionTime()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 5
nc 2
nop 1
crap 3
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
     */
48
    protected $transactionTime;
49
50
    /**
51
     * Build the response with Request dependency injection
52
     * @param AbstractRequest $request
53
     */
54 18
    public function __construct(AbstractRequest $request = null)
55
    {
56 18
        parent::__construct();
57 18
        $this->request = $request;
58 18
    }
59
60
    /**
61
     * Set the linked request
62
     * @param AbstractRequest $request
63
     * @return Response
64
     */
65 1
    public function setRequest(AbstractRequest $request)
66
    {
67 1
        $this->request = $request;
68 1
        return $this;
69
    }
70
71
    /**
72
     * @return AbstractRequest
73
     */
74 7
    public function getRequest()
75
    {
76 7
        return $this->request;
77
    }
78
79
    /**
80
     * Set the execution infos
81
     * @param ExecutionInfos $infos
82
     * @return Response
83
     */
84 10
    public function setExecutionInfos(ExecutionInfos $infos)
85
    {
86 10
        $this->infos = $infos;
87 10
        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 11
    public function setStatus($code)
103
    {
104 11
        $this->status = (int)$code;
105 11
        return $this;
106
    }
107
108
    /**
109
     * @return int
110
     */
111 1
    public function getStatus()
112
    {
113 1
        return $this->status;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getStatusMessage()
120
    {
121
        return $this->request->getStatusMessage($this->status);
122
    }
123
124
    /**
125
     * @param double $time
126
     * @return Response
127
     * @throws RuntimeException
128
     */
129 13
    public function setTransactionTime($time)
130
    {
131 13
        if (!is_numeric($time) || $time < 0) {
132 2
            throw new RuntimeException("Invalid response time format");
133
        }
134 11
        $this->transactionTime = (float)$time;
135 11
        return $this;
136
    }
137
138
    /**
139
     * @return double
140
     */
141 1
    public function getTransactionTime()
142
    {
143 1
        return $this->transactionTime;
144
    }
145
146
    /**
147
     * Try to json_decode the response body
148
     * @return string
149
     * @throws RuntimeException
150
     */
151 1
    public function json()
152
    {
153 1
        $json = json_decode($this->getBody(), true);
154 1
        if ($json === null) {
155 1
            throw new RuntimeException(sprintf(
156 1
                "Can't decode response as JSON: %s",
157 1
                function_exists('json_last_error_msg')?
158 1
                    json_last_error_msg():
159
                    'Error code '.json_last_error()
160 1
            ));
161
        }
162 1
        return $json;
163
    }
164
}
165