1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (C) 2013 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\HttpClient; |
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
|
|
|
public function __construct() |
61
|
|
|
{ |
62
|
|
|
$this->responseHistory = new History(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function createConfiguredClient(): PluginClient |
66
|
|
|
{ |
67
|
|
|
$plugins = [ |
68
|
|
|
new Plugin\AddHostPlugin($this->getUriFactory()->createUri($this->endpoint)), |
69
|
|
|
new Plugin\HeaderDefaultsPlugin([ |
70
|
|
|
'User-Agent' => 'mailgun-sdk-php/v2 (https://github.com/mailgun/mailgun-php)', |
71
|
|
|
'Authorization' => 'Basic '.base64_encode(sprintf('api:%s', $this->getApiKey())), |
72
|
|
|
]), |
73
|
|
|
new Plugin\HistoryPlugin($this->responseHistory), |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
if ($this->debug) { |
77
|
|
|
$plugins[] = new ReplaceUriPlugin($this->getUriFactory()->createUri($this->endpoint)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return new PluginClient($this->getHttpClient(), $plugins); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function setDebug(bool $debug): self |
84
|
|
|
{ |
85
|
|
|
$this->debug = $debug; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function setEndpoint(string $endpoint): self |
91
|
|
|
{ |
92
|
|
|
$this->endpoint = $endpoint; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getApiKey(): string |
98
|
|
|
{ |
99
|
|
|
return $this->apiKey; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function setApiKey(string $apiKey): self |
103
|
|
|
{ |
104
|
|
|
$this->apiKey = $apiKey; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function getUriFactory(): UriFactory |
110
|
|
|
{ |
111
|
|
|
if (null === $this->uriFactory) { |
112
|
|
|
$this->uriFactory = UriFactoryDiscovery::find(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->uriFactory; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setUriFactory(UriFactory $uriFactory): self |
119
|
|
|
{ |
120
|
|
|
$this->uriFactory = $uriFactory; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function getHttpClient(): HttpClient |
126
|
|
|
{ |
127
|
|
|
if (null === $this->httpClient) { |
128
|
|
|
$this->httpClient = HttpClientDiscovery::find(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->httpClient; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function setHttpClient(HttpClient $httpClient): self |
135
|
|
|
{ |
136
|
|
|
$this->httpClient = $httpClient; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getResponseHistory(): History |
142
|
|
|
{ |
143
|
|
|
return $this->responseHistory; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|