Completed
Push — master ( 0f4fe2...64d1c4 )
by Tobias
05:46 queued 03:44
created

HttpClientConfigurator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 72.5%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 145
ccs 29
cts 40
cp 0.725
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createConfiguredClient() 0 13 1
A getEndpoint() 0 4 1
A setEndpoint() 0 6 1
A getApiKey() 0 4 1
A setApiKey() 0 6 1
A getUriFactory() 0 8 2
A setUriFactory() 0 6 1
A getHttpClient() 0 8 2
A setHttpClient() 0 6 1
A getResponseHistory() 0 4 1
1
<?php
2
3
/*
4
 * Copyright (C) 2013-2016 Mailgun
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 Mailgun;
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\UriFactory;
17
use Http\Client\Common\Plugin;
18
use Mailgun\HttpClient\Plugin\History;
19
20
/**
21
 * Configure a HTTP client.
22
 *
23
 * @author Tobias Nyholm <[email protected]>
24
 */
25
class HttpClientConfigurator
26
{
27
    /**
28
     * @var string
29
     */
30
    private $endpoint = 'https://api.mailgun.net';
31
32
    /**
33
     * @var string
34
     */
35
    private $apiKey;
36
37
    /**
38
     * @var UriFactory
39
     */
40
    private $uriFactory;
41
42
    /**
43
     * @var HttpClient
44
     */
45
    private $httpClient;
46
47
    /**
48
     * @var History
49
     */
50
    private $responseHistory;
51
52 6
    public function __construct()
53
    {
54 6
        $this->responseHistory = new History();
55 6
    }
56
57
    /**
58
     * @return PluginClient
59
     */
60 6
    public function createConfiguredClient()
61
    {
62
        $plugins = [
63 6
            new Plugin\AddHostPlugin($this->getUriFactory()->createUri($this->getEndpoint())),
64 6
            new Plugin\HeaderDefaultsPlugin([
65 6
                'User-Agent' => 'mailgun-sdk-php/v2 (https://github.com/mailgun/mailgun-php)',
66 6
                'Authorization' => 'Basic '.base64_encode(sprintf('api:%s', $this->getApiKey())),
67 6
            ]),
68 6
            new Plugin\HistoryPlugin($this->responseHistory),
69 6
        ];
70
71 6
        return new PluginClient($this->getHttpClient(), $plugins);
72
    }
73
74
    /**
75
     * @return string
76
     */
77 6
    private function getEndpoint()
78
    {
79 6
        return $this->endpoint;
80
    }
81
82
    /**
83
     * @param string $endpoint
84
     *
85
     * @return HttpClientConfigurator
86
     */
87
    public function setEndpoint($endpoint)
88
    {
89
        $this->endpoint = $endpoint;
90
91
        return $this;
92
    }
93
94
    /**
95
     * @return string
96
     */
97 6
    private function getApiKey()
98
    {
99 6
        return $this->apiKey;
100
    }
101
102
    /**
103
     * @param string $apiKey
104
     *
105
     * @return HttpClientConfigurator
106
     */
107 6
    public function setApiKey($apiKey)
108
    {
109 6
        $this->apiKey = $apiKey;
110
111 6
        return $this;
112
    }
113
114
    /**
115
     * @return UriFactory
116
     */
117 6
    private function getUriFactory()
118
    {
119 6
        if ($this->uriFactory === null) {
120 6
            $this->uriFactory = UriFactoryDiscovery::find();
121 6
        }
122
123 6
        return $this->uriFactory;
124
    }
125
126
    /**
127
     * @param UriFactory $uriFactory
128
     *
129
     * @return HttpClientConfigurator
130
     */
131
    public function setUriFactory(UriFactory $uriFactory)
132
    {
133
        $this->uriFactory = $uriFactory;
134
135
        return $this;
136
    }
137
138
    /**
139
     * @return HttpClient
140
     */
141 6
    private function getHttpClient()
142
    {
143 6
        if ($this->httpClient === null) {
144 6
            $this->httpClient = HttpClientDiscovery::find();
145 6
        }
146
147 6
        return $this->httpClient;
148
    }
149
150
    /**
151
     * @param HttpClient $httpClient
152
     *
153
     * @return HttpClientConfigurator
154
     */
155
    public function setHttpClient(HttpClient $httpClient)
156
    {
157
        $this->httpClient = $httpClient;
158
159
        return $this;
160
    }
161
162
    /**
163
     * @return History
164
     */
165
    public function getResponseHistory()
166
    {
167
        return $this->responseHistory;
168
    }
169
}
170