HttpClientConfigurator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 48.15%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 0
loc 114
ccs 13
cts 27
cp 0.4815
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createConfiguredClient() 0 15 2
A setEndpoint() 0 6 1
A setApiKey() 0 6 1
A appendPlugin() 0 8 2
A prependPlugin() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace FAPI\Boilerplate;
11
12
use Http\Client\HttpClient;
13
use Http\Client\Common\PluginClient;
14
use Http\Discovery\HttpClientDiscovery;
15
use Http\Discovery\UriFactoryDiscovery;
16
use Http\Message\Authentication;
17
use Http\Message\UriFactory;
18
use Http\Client\Common\Plugin;
19
20
/**
21
 * Configure an HTTP client.
22
 *
23
 * @author Tobias Nyholm <[email protected]>
24
 *
25
 * @internal This class should not be used outside of the API Client, it is not part of the BC promise.
26
 */
27
final class HttpClientConfigurator
28
{
29
    /**
30
     * @var string
31
     */
32
    private $endpoint = 'https://fake-twitter.com';
33
34
    /**
35
     * @var string
36
     */
37
    private $apiKey;
38
39
    /**
40
     * @var UriFactory
41
     */
42
    private $uriFactory;
43
44
    /**
45
     * @var HttpClient
46
     */
47
    private $httpClient;
48
49
    /**
50
     * @var Plugin[]
51
     */
52
    private $prependPlugins = [];
53
54
    /**
55
     * @var Plugin[]
56
     */
57
    private $appendPlugins = [];
58
59
    /**
60
     * @param HttpClient|null $httpClient
61
     * @param UriFactory|null $uriFactory
62
     */
63 4
    public function __construct(HttpClient $httpClient = null, UriFactory $uriFactory = null)
64
    {
65 4
        $this->httpClient = $httpClient ?? HttpClientDiscovery::find();
66 4
        $this->uriFactory = $uriFactory ?? UriFactoryDiscovery::find();
67 4
    }
68
69
    /**
70
     * @return HttpClient
71
     */
72
    public function createConfiguredClient(): HttpClient
73
    {
74
        $plugins = $this->prependPlugins;
75
76
        $plugins[] = new Plugin\AddHostPlugin($this->uriFactory->createUri($this->endpoint));
77
        $plugins[] = new Plugin\HeaderDefaultsPlugin([
78
            'User-Agent' => 'FriendsOfApi/boilerplate (https://github.com/FriendsOfApi/boilerplate)',
79
        ]);
80
81
        if (null !== $this->apiKey) {
82
            $plugins[] = new Plugin\AuthenticationPlugin(new Authentication\Bearer($this->apiKey));
83
        }
84
85
        return new PluginClient($this->httpClient, array_merge($plugins, $this->appendPlugins));
86
    }
87
88
    /**
89
     * @param string $endpoint
90
     *
91
     * @return HttpClientConfigurator
92
     */
93
    public function setEndpoint(string $endpoint): HttpClientConfigurator
94
    {
95
        $this->endpoint = $endpoint;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @param string $apiKey
102
     *
103
     * @return HttpClientConfigurator
104
     */
105
    public function setApiKey(string $apiKey): HttpClientConfigurator
106
    {
107
        $this->apiKey = $apiKey;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @param Plugin $plugin
114
     *
115
     * @return HttpClientConfigurator
116
     */
117 2
    public function appendPlugin(Plugin ...$plugin): HttpClientConfigurator
118
    {
119 2
        foreach ($plugin as $p) {
120 2
            $this->appendPlugins[] = $p;
121
        }
122
123 2
        return $this;
124
    }
125
126
    /**
127
     * @param Plugin $plugin
128
     *
129
     * @return HttpClientConfigurator
130
     */
131 2
    public function prependPlugin(Plugin ...$plugin): HttpClientConfigurator
132
    {
133 2
        $plugin = array_reverse($plugin);
134 2
        foreach ($plugin as $p) {
135 2
            array_unshift($this->prependPlugins, $p);
136
        }
137
138 2
        return $this;
139
    }
140
}
141