1 | <?php |
||
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 | /** |
||
66 | * @return PluginClient |
||
67 | */ |
||
68 | public function createConfiguredClient() |
||
69 | { |
||
70 | $plugins = [ |
||
71 | new Plugin\AddHostPlugin($this->getUriFactory()->createUri($this->endpoint)), |
||
72 | new Plugin\HeaderDefaultsPlugin([ |
||
73 | 'User-Agent' => 'mailgun-sdk-php/v2 (https://github.com/mailgun/mailgun-php)', |
||
74 | 'Authorization' => 'Basic '.base64_encode(sprintf('api:%s', $this->getApiKey())), |
||
75 | ]), |
||
76 | new Plugin\HistoryPlugin($this->responseHistory), |
||
77 | ]; |
||
78 | |||
79 | if ($this->debug) { |
||
80 | $plugins[] = new ReplaceUriPlugin($this->getUriFactory()->createUri($this->endpoint)); |
||
81 | } |
||
82 | |||
83 | return new PluginClient($this->getHttpClient(), $plugins); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param bool $debug |
||
88 | * |
||
89 | * @return HttpClientConfigurator |
||
90 | */ |
||
91 | public function setDebug($debug) |
||
97 | |||
98 | /** |
||
99 | * @param string $endpoint |
||
100 | * |
||
101 | * @return HttpClientConfigurator |
||
102 | */ |
||
103 | public function setEndpoint($endpoint) |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | private function getApiKey() |
||
114 | { |
||
115 | return $this->apiKey; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param string $apiKey |
||
120 | * |
||
121 | * @return HttpClientConfigurator |
||
122 | */ |
||
123 | public function setApiKey($apiKey) |
||
124 | { |
||
125 | $this->apiKey = $apiKey; |
||
126 | |||
127 | return $this; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return UriFactory |
||
132 | */ |
||
133 | private function getUriFactory() |
||
134 | { |
||
135 | if ($this->uriFactory === null) { |
||
136 | $this->uriFactory = UriFactoryDiscovery::find(); |
||
137 | } |
||
138 | |||
139 | return $this->uriFactory; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param UriFactory $uriFactory |
||
144 | * |
||
145 | * @return HttpClientConfigurator |
||
146 | */ |
||
147 | public function setUriFactory(UriFactory $uriFactory) |
||
153 | |||
154 | /** |
||
155 | * @return HttpClient |
||
156 | */ |
||
157 | private function getHttpClient() |
||
158 | { |
||
159 | if ($this->httpClient === null) { |
||
160 | $this->httpClient = HttpClientDiscovery::find(); |
||
161 | } |
||
162 | |||
163 | return $this->httpClient; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param HttpClient $httpClient |
||
168 | * |
||
169 | * @return HttpClientConfigurator |
||
170 | */ |
||
171 | public function setHttpClient(HttpClient $httpClient) |
||
177 | |||
178 | /** |
||
179 | * @return History |
||
180 | */ |
||
181 | public function getResponseHistory() |
||
185 | } |
||
186 |