HttpClientConfigurator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 9
dl 0
loc 122
rs 10

6 Methods

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