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