ClientConfigurator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 40.54%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 110
ccs 15
cts 37
cp 0.4054
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createConfiguredClient() 0 18 2
A setEndpoint() 0 4 1
A appendPlugin() 0 7 2
A prependPlugin() 0 8 2
A removePlugin() 0 16 5
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\Sylius\Http;
11
12
use Http\Client\Common\Plugin;
13
use Http\Client\Common\PluginClient;
14
use Http\Client\HttpClient;
15
use Http\Discovery\HttpClientDiscovery;
16
use Http\Discovery\UriFactoryDiscovery;
17
use Http\Message\UriFactory;
18
19
/**
20
 * Configure an HTTP client.
21
 *
22
 * @author Tobias Nyholm <[email protected]>
23
 */
24
final class ClientConfigurator
25
{
26
    /**
27
     * @var string
28
     */
29
    private $endpoint = 'https://sylius.com/api/v1';
30
31
    /**
32
     * @var UriFactory
33
     */
34
    private $uriFactory;
35
36
    /**
37
     * This is the client we use for actually sending the requests.
38
     *
39
     * @var HttpClient
40
     */
41
    private $httpClient;
42
43
    /**
44
     * This is the client wrapping the $httpClient.
45
     *
46
     * @var PluginClient
47
     */
48
    private $configuredClient;
49
50
    /**
51
     * @var Plugin[]
52
     */
53
    private $prependPlugins = [];
54
55
    /**
56
     * @var Plugin[]
57
     */
58
    private $appendPlugins = [];
59
60
    /**
61
     * True if we should create a new Plugin client at next request.
62
     *
63
     * @var bool
64
     */
65
    private $configurationModified = true;
66
67 4
    public function __construct(HttpClient $httpClient = null, UriFactory $uriFactory = null)
68
    {
69 4
        $this->httpClient = $httpClient ?? HttpClientDiscovery::find();
70 4
        $this->uriFactory = $uriFactory ?? UriFactoryDiscovery::find();
71 4
    }
72
73
    public function createConfiguredClient(): HttpClient
74
    {
75
        if ($this->configurationModified) {
76
            $this->configurationModified = false;
77
            $plugins = $this->prependPlugins;
78
            $plugins[] = new Plugin\AddHostPlugin($this->uriFactory->createUri($this->endpoint));
79
            $plugins[] = new Plugin\HeaderDefaultsPlugin(
80
                [
81
                    'User-Agent' => 'FriendsOfApi/sylius (https://github.com/FriendsOfApi/sylius)',
82
                    'Content-type' => 'application/json',
83
                ]
84
            );
85
86
            $this->configuredClient = new PluginClient($this->httpClient, \array_merge($plugins, $this->appendPlugins));
87
        }
88
89
        return $this->configuredClient;
90
    }
91
92
    public function setEndpoint(string $endpoint): void
93
    {
94
        $this->endpoint = $endpoint;
95
    }
96
97 2
    public function appendPlugin(Plugin ...$plugin): void
98
    {
99 2
        $this->configurationModified = true;
100 2
        foreach ($plugin as $p) {
101 2
            $this->appendPlugins[] = $p;
102
        }
103 2
    }
104
105 2
    public function prependPlugin(Plugin ...$plugin): void
106
    {
107 2
        $this->configurationModified = true;
108 2
        $plugin = \array_reverse($plugin);
109 2
        foreach ($plugin as $p) {
110 2
            \array_unshift($this->prependPlugins, $p);
111
        }
112 2
    }
113
114
    /**
115
     * Remove a plugin by its fully qualified class name (FQCN).
116
     */
117
    public function removePlugin(string $fqcn): void
118
    {
119
        foreach ($this->prependPlugins as $idx => $plugin) {
120
            if ($plugin instanceof $fqcn) {
121
                unset($this->prependPlugins[$idx]);
122
                $this->configurationModified = true;
123
            }
124
        }
125
126
        foreach ($this->appendPlugins as $idx => $plugin) {
127
            if ($plugin instanceof $fqcn) {
128
                unset($this->appendPlugins[$idx]);
129
                $this->configurationModified = true;
130
            }
131
        }
132
    }
133
}
134