|
6 | 6 | use MailQ\Exceptions\UnresolvedMxDomainException;
|
7 | 7 | use MailQ\Exceptions\InvalidEmailAddressException;
|
8 | 8 |
|
9 |
| -class Connector { |
10 |
| - |
11 |
| - private $apiKey; |
12 |
| - private $baseUrl; |
13 |
| - |
14 |
| - private static $instance; |
15 |
| - |
16 |
| - function __construct($baseUrl,$apiKey) { |
17 |
| - $this->baseUrl = $baseUrl; |
18 |
| - $this->apiKey = $apiKey; |
19 |
| - } |
20 |
| - |
21 |
| - public static function getInstance($baseUrl,$apiKey) { |
22 |
| - if (!isset(self::$instance)) { |
23 |
| - self::$instance = new Connector($baseUrl, $apiKey); |
24 |
| - } |
25 |
| - return self::$instance; |
26 |
| - } |
27 |
| - |
28 |
| - /** |
29 |
| - * |
30 |
| - * @var array |
31 |
| - */ |
32 |
| - private $headers; |
33 |
| - |
34 |
| - /** |
35 |
| - * |
36 |
| - * @param \MailQ\Request $request |
37 |
| - * @return \MailQ\Response |
38 |
| - */ |
39 |
| - public function sendRequest(Request $request) { |
40 |
| - $ch = curl_init(); |
41 |
| - $curlHeaders = $this->createCurlHeaders(array_merge($request->getHeaders(),$this->createDefaultHeaders())); |
42 |
| - $url = $this->baseUrl.'/companies/'.$request->getPath(); |
43 |
| - if ($request->hasParameters()) { |
44 |
| - $url .= '?'.http_build_query($request->getParameters()); |
45 |
| - } |
46 |
| - curl_setopt($ch, CURLOPT_URL, $url); |
47 |
| - curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
48 |
| - curl_setopt($ch, CURLOPT_HEADER, FALSE); |
49 |
| - curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this,'storeHeader')); |
50 |
| - curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeaders); |
51 |
| - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
52 |
| - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
53 |
| - if ($request->hasContent()) { |
54 |
| - curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getContent()); |
55 |
| - } |
56 |
| - if ($request->isPost()) { |
57 |
| - curl_setopt($ch, CURLOPT_POST, TRUE); |
58 |
| - } elseif ($request->isDelete()) { |
59 |
| - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, Request::HTTP_METHOD_DELETE); |
60 |
| - } elseif ($request->isPut()) { |
61 |
| - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, Request::HTTP_METHOD_PUT); |
62 |
| - } |
63 |
| - $responseData = curl_exec($ch); |
64 |
| - $response = $this->createResponse($ch,$responseData); |
65 |
| - curl_close($ch); |
66 |
| - return $response; |
67 |
| - } |
68 |
| - |
69 |
| - /** |
70 |
| - * |
71 |
| - * @param $ch |
72 |
| - * @param string $response |
73 |
| - * @return Response |
74 |
| - */ |
75 |
| - private function createResponse($ch,$response) { |
76 |
| - $responseData = new Response(); |
77 |
| - $responseData->setHttpCode(curl_getinfo($ch, CURLINFO_HTTP_CODE)); |
78 |
| - if ($responseData->isOk()) { |
79 |
| - if (!$responseData->isNoContent()) { |
80 |
| - $responseData->setContent($response); |
81 |
| - } |
82 |
| - } |
83 |
| - else { |
84 |
| - $this->processError($responseData, $response); |
85 |
| - } |
86 |
| - $responseData->setHeaders($this->headers); |
87 |
| - $responseData->setHttpCode(curl_getinfo($ch, CURLINFO_HTTP_CODE)); |
88 |
| - return $responseData; |
89 |
| - } |
90 |
| - |
91 |
| - function processError(Response $responseData,$response) { |
92 |
| - switch ($responseData->getHttpCode()) { |
93 |
| - case 401: throw new MailQException("Invalid API key.",$responseData->getHttpCode()); |
94 |
| - case 405: throw new MailQException("Method not allowed.",$responseData->getHttpCode()); |
95 |
| - default: $this->handleDefaultException($responseData,$response); |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - private function handleDefaultException(Response $responseData,$response) { |
| 9 | +class Connector |
| 10 | +{ |
| 11 | + |
| 12 | + private $apiKey; |
| 13 | + |
| 14 | + private $baseUrl; |
| 15 | + |
| 16 | + private static $instance; |
| 17 | + |
| 18 | + function __construct($baseUrl, $apiKey) |
| 19 | + { |
| 20 | + $this->baseUrl = $baseUrl; |
| 21 | + $this->apiKey = $apiKey; |
| 22 | + } |
| 23 | + |
| 24 | + public static function getInstance($baseUrl, $apiKey) |
| 25 | + { |
| 26 | + if (!isset(self::$instance)) { |
| 27 | + self::$instance = new Connector($baseUrl, $apiKey); |
| 28 | + } |
| 29 | + return self::$instance; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * |
| 34 | + * @var array |
| 35 | + */ |
| 36 | + private $headers; |
| 37 | + |
| 38 | + /** |
| 39 | + * |
| 40 | + * @param \MailQ\Request $request |
| 41 | + * @return \MailQ\Response |
| 42 | + */ |
| 43 | + public function sendRequest(Request $request) |
| 44 | + { |
| 45 | + $ch = curl_init(); |
| 46 | + $curlHeaders = $this->createCurlHeaders(array_merge($request->getHeaders(), $this->createDefaultHeaders())); |
| 47 | + $url = $this->baseUrl . '/companies/' . $request->getPath(); |
| 48 | + if ($request->hasParameters()) { |
| 49 | + $url .= '?' . http_build_query($request->getParameters()); |
| 50 | + } |
| 51 | + curl_setopt($ch, CURLOPT_URL, $url); |
| 52 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 53 | + curl_setopt($ch, CURLOPT_HEADER, false); |
| 54 | + curl_setopt($ch, CURLOPT_HEADERFUNCTION, [$this, 'storeHeader']); |
| 55 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeaders); |
| 56 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
| 57 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
| 58 | + if ($request->hasContent()) { |
| 59 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getContent()); |
| 60 | + } |
| 61 | + if ($request->isPost()) { |
| 62 | + curl_setopt($ch, CURLOPT_POST, true); |
| 63 | + } elseif ($request->isDelete()) { |
| 64 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, Request::HTTP_METHOD_DELETE); |
| 65 | + } elseif ($request->isPut()) { |
| 66 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, Request::HTTP_METHOD_PUT); |
| 67 | + } |
| 68 | + $responseData = curl_exec($ch); |
| 69 | + $response = $this->createResponse($ch, $responseData); |
| 70 | + curl_close($ch); |
| 71 | + return $response; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * |
| 76 | + * @param $ch |
| 77 | + * @param string $response |
| 78 | + * @return Response |
| 79 | + */ |
| 80 | + private function createResponse($ch, $response) |
| 81 | + { |
| 82 | + $responseData = new Response(); |
| 83 | + $responseData->setHttpCode(curl_getinfo($ch, CURLINFO_HTTP_CODE)); |
| 84 | + if ($responseData->isOk()) { |
| 85 | + if (!$responseData->isNoContent()) { |
| 86 | + $responseData->setContent($response); |
| 87 | + } |
| 88 | + } else { |
| 89 | + $this->processError($responseData, $response); |
| 90 | + } |
| 91 | + $responseData->setHeaders($this->headers); |
| 92 | + $responseData->setHttpCode(curl_getinfo($ch, CURLINFO_HTTP_CODE)); |
| 93 | + return $responseData; |
| 94 | + } |
| 95 | + |
| 96 | + function processError(Response $responseData, $response) |
| 97 | + { |
| 98 | + switch ($responseData->getHttpCode()) { |
| 99 | + case 401: |
| 100 | + throw new MailQException("Invalid API key.", $responseData->getHttpCode()); |
| 101 | + case 405: |
| 102 | + throw new MailQException("Method not allowed.", $responseData->getHttpCode()); |
| 103 | + default: |
| 104 | + $this->handleDefaultException($responseData, $response); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private function handleDefaultException(Response $responseData, $response) |
| 109 | + { |
100 | 110 | $errorData = json_decode($response);
|
101 | 111 | if ($errorData) {
|
102 | 112 | if (isset($errorData->error)) {
|
103 | 113 | $this->createSpecificException($errorData->error);
|
104 | 114 | }
|
105 |
| - } |
106 |
| - else { |
107 |
| - throw new MailQException("Unknown exception",$responseData->getHttpCode()); |
| 115 | + } else { |
| 116 | + throw new MailQException("Unknown exception ".$responseData->getHttpCode(), $responseData->getHttpCode()); |
108 | 117 | }
|
109 | 118 | }
|
110 |
| - |
111 |
| - private function createSpecificException($errorData) { |
| 119 | + |
| 120 | + private function createSpecificException($errorData) |
| 121 | + { |
112 | 122 | // TODO change to resolving messages by errorCode
|
113 | 123 | if ($errorData->message === 'Could not resolve MX domain.') {
|
114 |
| - throw new UnresolvedMxDomainException($errorData->message,$errorData->code); |
| 124 | + throw new UnresolvedMxDomainException($errorData->message, $errorData->code); |
| 125 | + } else { |
| 126 | + if ($errorData->message === 'Invalid recipient email.') { |
| 127 | + throw new InvalidEmailAddressException($errorData->message, $errorData->code); |
| 128 | + } else { |
| 129 | + throw new MailQException($errorData->message, $errorData->code); |
| 130 | + } |
115 | 131 | }
|
116 |
| - else if ($errorData->message === 'Invalid recipient email.') { |
117 |
| - throw new InvalidEmailAddressException($errorData->message,$errorData->code); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Storing headers from curl |
| 136 | + * @param $ch |
| 137 | + * @param $str |
| 138 | + * @return int |
| 139 | + */ |
| 140 | + function storeHeader($ch, $str) |
| 141 | + { |
| 142 | + $strex = explode(": ", $str); |
| 143 | + if (count($strex) == 2) { |
| 144 | + $this->headers[$strex[0]] = trim($strex[1]); |
| 145 | + } else { |
| 146 | + $this->headers[] = trim($str); |
118 | 147 | }
|
119 |
| - else { |
120 |
| - throw new MailQException($errorData->message,$errorData->code); |
| 148 | + return strlen($str); |
| 149 | + } |
| 150 | + |
| 151 | + function createDefaultHeaders() |
| 152 | + { |
| 153 | + $headers = [ |
| 154 | + "X-Api-Key" => $this->apiKey, |
| 155 | + ]; |
| 156 | + $headers["Content-Type"] = "application/json"; |
| 157 | + return $headers; |
| 158 | + } |
| 159 | + |
| 160 | + function createCurlHeaders($headers) |
| 161 | + { |
| 162 | + $curlHeaders = []; |
| 163 | + foreach ($headers as $headerName => $value) { |
| 164 | + $curlHeaders[] = $headerName . ": " . $value; |
121 | 165 | }
|
| 166 | + return $curlHeaders; |
122 | 167 | }
|
123 | 168 |
|
124 |
| - /** |
125 |
| - * Storing headers from curl |
126 |
| - * @param $ch |
127 |
| - * @param $str |
128 |
| - * @return int |
129 |
| - */ |
130 |
| - function storeHeader($ch, $str) { |
131 |
| - $strex = explode(": ", $str); |
132 |
| - if (count($strex) == 2) { |
133 |
| - $this->headers[$strex[0]] = trim($strex[1]); |
134 |
| - } else { |
135 |
| - $this->headers[] = trim($str); |
136 |
| - } |
137 |
| - return strlen($str); |
138 |
| - } |
139 |
| - |
140 |
| - function createDefaultHeaders() { |
141 |
| - $headers = [ |
142 |
| - "X-Api-Key" => $this->apiKey |
143 |
| - ]; |
144 |
| - $headers["Content-Type"] = "application/json"; |
145 |
| - return $headers; |
146 |
| - } |
147 |
| - |
148 |
| - function createCurlHeaders($headers) { |
149 |
| - $curlHeaders = []; |
150 |
| - foreach ($headers as $headerName => $value) { |
151 |
| - $curlHeaders[] = $headerName.": ".$value; |
152 |
| - } |
153 |
| - return $curlHeaders; |
154 |
| - } |
155 |
| - |
156 | 169 | }
|
0 commit comments