Completed
Pull Request — master (#10)
by Tobias
07:59
created

ClientConfigurator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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