Passed
Push — master ( 8e3daa...fc64c3 )
by Irfaq
12:01
created

Builder::sendRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Transmission\HttpClient;
4
5
use Http\Client\Common\Plugin;
6
use Http\Client\Common\PluginClientFactory;
7
use Http\Discovery\Psr18ClientDiscovery;
8
use Psr\Http\Client\ClientInterface;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * A builder that builds the API client.
14
 * This will allow you to fluently add and remove plugins.
15
 *
16
 * Based on the original code written by Tobias Nyholm <[email protected]>
17
 */
18
class Builder
19
{
20
    /**
21
     * The object that sends HTTP messages.
22
     *
23
     * @var ClientInterface
24
     */
25
    private $httpClient;
26
27
    /**
28
     * A HTTP client with all our plugins.
29
     *
30
     * @var ClientInterface
31
     */
32
    private $pluginClient;
33
34
    /**
35
     * @var RequestBuilder
36
     */
37
    private $requestBuilder;
38
39
    /**
40
     * True if we should create a new Plugin client at next request.
41
     *
42
     * @var bool
43
     */
44
    private $httpClientModified = true;
45
46
    /**
47
     * @var Plugin[]
48
     */
49
    private $plugins = [];
50
51
    /**
52
     * @param ClientInterface $httpClient The client to send requests with.
53
     */
54
    public function __construct(ClientInterface $httpClient = null)
55
    {
56
        $this->httpClient = $httpClient ?? Psr18ClientDiscovery::find();
57
        $this->requestBuilder = new RequestBuilder();
58
    }
59
60
    /**
61
     * @return ClientInterface
62
     */
63
    public function getHttpClient(): ClientInterface
64
    {
65
        if ($this->httpClientModified) {
66
            $this->httpClientModified = false;
67
68
            $this->pluginClient = (new PluginClientFactory())->createClient($this->httpClient, $this->plugins);
69
        }
70
71
        return $this->pluginClient;
72
    }
73
74
    /**
75
     * Sets the http client.
76
     *
77
     * @param ClientInterface $httpClient
78
     *
79
     * @return Builder
80
     */
81
    public function setHttpClient(ClientInterface $httpClient): self
82
    {
83
        $this->httpClient = $httpClient;
84
85
        return $this;
86
    }
87
88
    /**
89
     * @return RequestBuilder
90
     */
91
    public function getRequestBuilder(): RequestBuilder
92
    {
93
        return $this->requestBuilder;
94
    }
95
96
    /**
97
     * @param RequestBuilder $requestBuilder
98
     *
99
     * @return Builder
100
     */
101
    public function setRequestBuilder(RequestBuilder $requestBuilder): self
102
    {
103
        $this->requestBuilder = $requestBuilder;
104
105
        return $this;
106
    }
107
108
    /**
109
     * Add a new plugin to the end of the plugin chain.
110
     *
111
     * @param Plugin $plugin
112
     *
113
     * @return Builder
114
     */
115
    public function addPlugin(Plugin $plugin): self
116
    {
117
        $this->plugins[] = $plugin;
118
        $this->httpClientModified = true;
119
120
        return $this;
121
    }
122
123
    /**
124
     * Remove a plugin by its fully qualified class name (FQCN).
125
     *
126
     * @param string $fqcn
127
     *
128
     * @return Builder
129
     */
130
    public function removePlugin($fqcn): self
131
    {
132
        foreach ($this->plugins as $idx => $plugin) {
133
            if ($plugin instanceof $fqcn) {
134
                unset($this->plugins[$idx]);
135
                $this->httpClientModified = true;
136
            }
137
        }
138
139
        return $this;
140
    }
141
142
    /**
143
     * @param string $method
144
     * @param        $uri
145
     * @param array  $headers
146
     * @param null   $body
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $body is correct as it would always require null to be passed?
Loading history...
147
     *
148
     * @return ResponseInterface
149
     * @throws \Psr\Http\Client\ClientExceptionInterface
150
     */
151
    public function send(string $method, $uri, array $headers = [], $body = null): ResponseInterface
152
    {
153
        return $this->sendRequest($this->requestBuilder->create(
154
            $method,
155
            $uri,
156
            $headers,
157
            $body
158
        ));
159
    }
160
161
    /**
162
     * @param RequestInterface $request
163
     *
164
     * @return ResponseInterface
165
     * @throws \Psr\Http\Client\ClientExceptionInterface
166
     */
167
    public function sendRequest(RequestInterface $request): ResponseInterface
168
    {
169
        return $this->getHttpClient()->sendRequest($request);
170
    }
171
}
172