AbstractRequest::gateway()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Gregoriohc\Protean\Common\Messages;
4
5
use ArrayAccess;
6
use Gregoriohc\Protean\Common\Concerns\Parametrizable;
7
use IteratorAggregate;
8
use JsonSerializable;
9
use RuntimeException;
10
11
abstract class AbstractRequest implements RequestInterface, ArrayAccess, IteratorAggregate, JsonSerializable
12
{
13
    use Parametrizable;
14
15
    /**
16
     * The gateway
17
     *
18
     * @var \Gregoriohc\Protean\Common\AbstractGateway
19
     */
20
    protected $gateway;
21
22
    /**
23
     * The response to this request (if the request has been sent)
24
     *
25
     * @var ResponseInterface
26
     */
27
    protected $response;
28
29
    /**
30
     * AbstractRequest constructor.
31
     * @param \Gregoriohc\Protean\Common\AbstractGateway $gateway
32
     * @param array $parameters
33
     */
34 24
    public function __construct($gateway, $parameters = [])
35
    {
36 24
        $this->gateway = $gateway;
37 24
        $this->bootParameters($parameters);
38 24
    }
39
40
    /**
41
     * @param array $data
42
     * @return ResponseInterface
43
     */
44 9
    protected function createResponse($data)
45
    {
46 9
        $responseClass = $this->responseClass();
47
48 9
        return $this->response = new $responseClass($this, $data);
49
    }
50
51
    /**
52
     * Get the response to this request (if the request has been sent)
53
     *
54
     * @return ResponseInterface
55
     */
56 6
    public function response()
57
    {
58 6
        if (null === $this->response) {
59 3
            throw new RuntimeException('You must call send() before accessing the Response.');
60
        }
61
62 3
        return $this->response;
63
    }
64
65
    /**
66
     * Send the request
67
     *
68
     * @return ResponseInterface
69
     */
70 15
    public function send()
71
    {
72 15
        $this->validateParameters();
73
74 9
        $data = $this->data();
75
76 9
        return $this->sendData($data);
77
    }
78
79
    /**
80
     * @return \Gregoriohc\Protean\Common\AbstractGateway
81
     */
82 3
    public function gateway()
83
    {
84 3
        return $this->gateway;
85
    }
86
}
87