HttpClientConfigurator::appendPlugin()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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