Builder::getStreamFactory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Esi\IPQuery.
7
 *
8
 * (c) Eric Sizemore <[email protected]>
9
 *
10
 * This source file is subject to the MIT license. For the full copyright and
11
 * license information, please view the LICENSE file that was distributed with
12
 * this source code.
13
 */
14
15
namespace Esi\IPQuery\HttpClient;
16
17
use Http\Client\Common\HttpMethodsClient;
18
use Http\Client\Common\HttpMethodsClientInterface;
19
use Http\Client\Common\Plugin;
20
use Http\Client\Common\Plugin\CachePlugin;
21
use Http\Client\Common\PluginClientFactory;
22
use Http\Discovery\Psr17FactoryDiscovery;
23
use Http\Discovery\Psr18ClientDiscovery;
24
use Psr\Cache\CacheItemPoolInterface;
25
use Psr\Http\Client\ClientInterface;
26
use Psr\Http\Message\RequestFactoryInterface;
27
use Psr\Http\Message\StreamFactoryInterface;
28
use Psr\Http\Message\UriFactoryInterface;
29
30
/**
31
 * The HTTP client builder class.
32
 */
33
final class Builder
34
{
35
    private ?CachePlugin $cachePlugin = null;
36
37
    private readonly ClientInterface $httpClient;
38
39
    private ?HttpMethodsClientInterface $httpMethodsClient = null;
40
41
    /**
42
     * @var array<Plugin>
43
     */
44
    private array $plugins = [];
45
46
    private readonly RequestFactoryInterface $requestFactory;
47
48
    private readonly StreamFactoryInterface $streamFactory;
49
50
    private readonly UriFactoryInterface $uriFactory;
51
52
    /**
53
     * Create a new client builder instance.
54
     */
55 17
    public function __construct(
56
        ?ClientInterface $httpClient = null,
57
        ?RequestFactoryInterface $requestFactory = null,
58
        ?StreamFactoryInterface $streamFactory = null,
59
        ?UriFactoryInterface $uriFactory = null
60
    ) {
61 17
        $this->httpClient     = $httpClient ?? Psr18ClientDiscovery::find();
0 ignored issues
show
Bug introduced by
The property httpClient is declared read-only in Esi\IPQuery\HttpClient\Builder.
Loading history...
62 17
        $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory();
0 ignored issues
show
Bug introduced by
The property requestFactory is declared read-only in Esi\IPQuery\HttpClient\Builder.
Loading history...
63 17
        $this->streamFactory  = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory();
0 ignored issues
show
Bug introduced by
The property streamFactory is declared read-only in Esi\IPQuery\HttpClient\Builder.
Loading history...
64 17
        $this->uriFactory     = $uriFactory ?? Psr17FactoryDiscovery::findUriFactory();
0 ignored issues
show
Bug introduced by
The property uriFactory is declared read-only in Esi\IPQuery\HttpClient\Builder.
Loading history...
65
    }
66
67
    /**
68
     * @template T
69
     *
70
     * @param array<array-key, T> $config
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, T> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, T>.
Loading history...
71
     */
72 3
    public function addCache(CacheItemPoolInterface $cacheItemPool, array $config = []): void
73
    {
74 3
        $this->cachePlugin       = CachePlugin::clientCache($cacheItemPool, $this->streamFactory, $config);
75 3
        $this->httpMethodsClient = null;
76
    }
77
78 9
    public function addPlugin(Plugin $plugin): void
79
    {
80 9
        $this->plugins[]         = $plugin;
81 9
        $this->httpMethodsClient = null;
82
    }
83
84 10
    public function getHttpClient(): HttpMethodsClientInterface
85
    {
86 10
        if (\is_null($this->httpMethodsClient)) {
87 10
            $plugins = $this->plugins;
88
89 10
            if (!\is_null($this->cachePlugin)) {
90 2
                $plugins[] = $this->cachePlugin;
91
            }
92
93 10
            $this->httpMethodsClient = new HttpMethodsClient(
94 10
                (new PluginClientFactory())->createClient($this->httpClient, $plugins),
95 10
                $this->requestFactory,
96 10
                $this->streamFactory
97 10
            );
98
        }
99
100 10
        return $this->httpMethodsClient;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->httpMethodsClient could return the type null which is incompatible with the type-hinted return Http\Client\Common\HttpMethodsClientInterface. Consider adding an additional type-check to rule them out.
Loading history...
101
    }
102
103 2
    public function getRequestFactory(): RequestFactoryInterface
104
    {
105 2
        return $this->requestFactory;
106
    }
107
108 2
    public function getStreamFactory(): StreamFactoryInterface
109
    {
110 2
        return $this->streamFactory;
111
    }
112
113 8
    public function getUriFactory(): UriFactoryInterface
114
    {
115 8
        return $this->uriFactory;
116
    }
117
118 2
    public function removeCache(): void
119
    {
120 2
        $this->cachePlugin       = null;
121 2
        $this->httpMethodsClient = null;
122
    }
123
124
    /**
125
     * @param class-string<Plugin> $pluginClassName
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Plugin> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Plugin>.
Loading history...
126
     */
127 9
    public function removePlugin(string $pluginClassName): void
128
    {
129 9
        $pluginRemoved = false;
130
131 9
        foreach ($this->plugins as $key => $plugin) {
132 8
            if ($plugin instanceof $pluginClassName) {
133 2
                unset($this->plugins[$key]);
134
135 2
                $pluginRemoved = true;
136
            }
137
        }
138
139 9
        if ($pluginRemoved) {
140 2
            $this->httpMethodsClient = null;
141
        }
142
    }
143
}
144