Completed
Push — master ( 913926...597557 )
by Denis
02:01
created

AbstractTransport::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace PhpJsonRpc\Client;
4
5
use PhpJsonRpc\Common\Chain\Chain;
6
use PhpJsonRpc\Common\Chain\Container;
7
8
abstract class AbstractTransport implements TransportInterface
9
{
10
    /**
11
     * @var Chain
12
     */
13
    protected $preRequest;
14
15
    /**
16
     * @var Chain
17
     */
18
    protected $postRequest;
19
20
    /**
21
     * AbstractTransport constructor.
22
     */
23
    public function __construct()
24
    {
25
        $this->preRequest  = Chain::createBase();
26
        $this->postRequest = Chain::createBase();
27
    }
28
29
    /**
30
     * @return Chain
31
     */
32
    public function onPreRequest(): Chain
33
    {
34
        return $this->preRequest;
35
    }
36
37
    /**
38
     * @return Chain
39
     */
40
    public function onPostRequest(): Chain
41
    {
42
        return $this->postRequest;
43
    }
44
45
    /**
46
     * Execute request
47
     *
48
     * @param string $request
49
     *
50
     * @return string
51
     */
52
    public function request(string $request): string
53
    {
54
        $request  = $this->preRequest($request);
55
        $response = $this->send($request);
56
57
        return $this->postRequest($response);
58
    }
59
60
    abstract public function send(string $request): string;
61
62
    /**
63
     * @param string $request
64
     *
65
     * @return string
66
     */
67
    private function preRequest(string $request): string
68
    {
69
        return $this->preRequest->handle(new Container($this, $request))->last();
70
    }
71
72
    /**
73
     * @param string $response
74
     *
75
     * @return string
76
     */
77
    private function postRequest(string $response): string
78
    {
79
        return $this->postRequest->handle(new Container($this, $response))->last();
80
    }
81
}
82