Skip to content

Commit 6185c4b

Browse files
committed
Updating interface
- added bcc, cc and replyTo - reformat code - added PHPDoc
1 parent 2f54346 commit 6185c4b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+4018
-2503
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/src/nbproject/*
22
/temp/*
33
/doc/*
4-
/vendor/*
4+
/vendor/*

src/.gitignore

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
### PhpStorm ###
2+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
3+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
4+
5+
# User-specific stuff:
6+
.idea/workspace.xml
7+
.idea/tasks.xml
8+
.idea/dictionaries
9+
.idea/vcs.xml
10+
.idea/jsLibraryMappings.xml
11+
12+
# Sensitive or high-churn files:
13+
.idea/dataSources.ids
14+
.idea/dataSources.xml
15+
.idea/dataSources.local.xml
16+
.idea/sqlDataSources.xml
17+
.idea/dynamic.xml
18+
.idea/uiDesigner.xml
19+
20+
# Gradle:
21+
.idea/gradle.xml
22+
.idea/libraries
23+
24+
# Mongo Explorer plugin:
25+
.idea/mongoSettings.xml
26+
27+
## File-based project format:
28+
*.iws
29+
30+
## Plugin-specific files:
31+
32+
# IntelliJ
33+
/out/
34+
35+
# mpeltonen/sbt-idea plugin
36+
.idea_modules/
37+
38+
# JIRA plugin
39+
atlassian-ide-plugin.xml
40+
41+
# Crashlytics plugin (for Android Studio and IntelliJ)
42+
com_crashlytics_export_strings.xml
43+
crashlytics.properties
44+
crashlytics-build.properties
45+
fabric.properties
46+
47+
### PhpStorm Patch ###
48+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
49+
50+
# *.iml
51+
# modules.xml
52+
# .idea/misc.xml
53+
# *.ipr

src/MailQ/Connector.php

+146-133
Original file line numberDiff line numberDiff line change
@@ -6,151 +6,164 @@
66
use MailQ\Exceptions\UnresolvedMxDomainException;
77
use MailQ\Exceptions\InvalidEmailAddressException;
88

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+
{
100110
$errorData = json_decode($response);
101111
if ($errorData) {
102112
if (isset($errorData->error)) {
103113
$this->createSpecificException($errorData->error);
104114
}
105-
}
106-
else {
107-
throw new MailQException("Unknown exception",$responseData->getHttpCode());
115+
} else {
116+
throw new MailQException("Unknown exception ".$responseData->getHttpCode(), $responseData->getHttpCode());
108117
}
109118
}
110-
111-
private function createSpecificException($errorData) {
119+
120+
private function createSpecificException($errorData)
121+
{
112122
// TODO change to resolving messages by errorCode
113123
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+
}
115131
}
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);
118147
}
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;
121165
}
166+
return $curlHeaders;
122167
}
123168

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-
156169
}

0 commit comments

Comments
 (0)