Api
3 years ago
Connection
3 years ago
Constants
7 years ago
Exception
3 years ago
HttpClient
3 years ago
Hydrator
3 years ago
Lists
7 years ago
Message
7 years ago
Messages
7 years ago
Model
7 years ago
Assert.php
7 years ago
Exception.php
7 years ago
HttpClientConfigurator.php
3 years ago
Mailgun.php
3 years ago
RequestBuilder.php
3 years ago
HttpClientConfigurator.php
186 lines
| 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; |
| 11 | |
| 12 | use AmeliaHttp\Client\HttpClient; |
| 13 | use AmeliaHttp\Client\Common\PluginClient; |
| 14 | use AmeliaHttp\Discovery\HttpClientDiscovery; |
| 15 | use AmeliaHttp\Discovery\UriFactoryDiscovery; |
| 16 | use AmeliaHttp\Message\UriFactory; |
| 17 | use AmeliaHttp\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 <tobias.nyholm@gmail.com> |
| 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 | /** |
| 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) |
| 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 | public 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 (null === $this->uriFactory) { |
| 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) |
| 148 | { |
| 149 | $this->uriFactory = $uriFactory; |
| 150 | |
| 151 | return $this; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @return HttpClient |
| 156 | */ |
| 157 | private function getHttpClient() |
| 158 | { |
| 159 | if (null === $this->httpClient) { |
| 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) |
| 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 |