From 516c07d64ccdd68c5edda221652236295243489c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Lok=C5=A1=C3=ADk?= Date: Tue, 21 Feb 2017 10:57:10 +0100 Subject: [PATCH 1/5] pripojenie do Smartemailing API obalene do try-catch --- src/SmartEmailing.php | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/SmartEmailing.php b/src/SmartEmailing.php index a6856b9..44719e5 100644 --- a/src/SmartEmailing.php +++ b/src/SmartEmailing.php @@ -236,21 +236,32 @@ protected function createSimpleXml($array, $rootElementName) { protected function callSmartemailingApiWithCurl($data) { - $ch = curl_init(); + try { + $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $this->url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); - curl_setopt($ch, CURLOPT_HEADER, FALSE); - curl_setopt($ch, CURLOPT_POST, TRUE); + curl_setopt($ch, CURLOPT_URL, $this->url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($ch, CURLOPT_HEADER, FALSE); + curl_setopt($ch, CURLOPT_POST, TRUE); - $postFields = $this->createSimpleXml($data, 'xmlrequest'); + $postFields = $this->createSimpleXml($data, 'xmlrequest'); - curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); - curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); + curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); - $response = curl_exec($ch); + $response = curl_exec($ch); + + curl_close($ch); + + } catch (\Exception $e) { + $xml = new \SimpleXMLElement(''); + $errorData = ['code' => $e->getCode(), 'message' => $e->getMessage()]; + + $this->arrayToXml($errorData, $xml); + + return $xml; + } - curl_close($ch); return new \SimpleXMLElement($response); } From 2ed3252ab68151d57ce70f0240838adcaeca7d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Lok=C5=A1=C3=ADk?= Date: Tue, 21 Feb 2017 11:27:23 +0100 Subject: [PATCH 2/5] - doplnene komentare k funkciam - pridana funkcia isValidXmlString, ktora kontroluje ci je xml response od Smartemailingu validny - pridana funkcia getErrorXml, ktora vrati xml s chybou --- src/SmartEmailing.php | 88 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 8 deletions(-) diff --git a/src/SmartEmailing.php b/src/SmartEmailing.php index 44719e5..8050636 100644 --- a/src/SmartEmailing.php +++ b/src/SmartEmailing.php @@ -17,17 +17,41 @@ class SmartEmailing extends \Nette\Object protected $token; + /** + * SmartEmailing constructor. + * @param $username + * @param $token + */ public function __construct($username, $token) { $this->username = $username; $this->token = $token; } + + /** + * insert contact to Smartemailing + * + * @param $email + * @param array $contactlists + * @param array $properties + * @param array $customfields + * @return \SimpleXMLElement + */ public function contactInsert($email, $contactlists = array(), $properties = array(), $customfields = array()) { return $this->contactUpdate($email, $contactlists, $properties, $customfields); } + /** + * update contact in Smartemailing + * + * @param $email + * @param array $contactlists + * @param array $properties + * @param array $customfields + * @return \SimpleXMLElement + */ public function contactUpdate($email, $contactlists = array(), $properties = array(), $customfields = array()) { $details = []; @@ -168,8 +192,6 @@ public function getAllUnsubscribedContacts() { * @return \SimpleXMLElement */ public function multipleContactsInsert($contacts) { - $dateTime = new \DateTime(); - $contactsArray = []; foreach ($contacts as $email => $cData) { @@ -202,6 +224,9 @@ public function multipleContactsInsert($contacts) { /** * convert array to xml + * + * @param $array + * @param $xml */ protected function arrayToXml($array, &$xml) { foreach($array as $key => $value) { @@ -225,6 +250,10 @@ protected function arrayToXml($array, &$xml) { /** * creating simple xml + * + * @param $array + * @param $rootElementName + * @return mixed */ protected function createSimpleXml($array, $rootElementName) { $xml = new \SimpleXMLElement('<' . $rootElementName . '>'); @@ -235,6 +264,12 @@ protected function createSimpleXml($array, $rootElementName) { } + /** + * connect to Smartemailing API v2 + * + * @param $data + * @return \SimpleXMLElement + */ protected function callSmartemailingApiWithCurl($data) { try { $ch = curl_init(); @@ -254,17 +289,54 @@ protected function callSmartemailingApiWithCurl($data) { curl_close($ch); } catch (\Exception $e) { - $xml = new \SimpleXMLElement(''); - $errorData = ['code' => $e->getCode(), 'message' => $e->getMessage()]; + return $this->getErrorXml($e->getCode(), $e->getMessage()); + } + - $this->arrayToXml($errorData, $xml); - - return $xml; + if ($this->isValidXmlString($response)) { + return new \SimpleXMLElement($response); + + } else { + return $this->getErrorXml('500', 'Unknown Smartemailing API error.'); } + } - return new \SimpleXMLElement($response); + /** + * return error XML + * + * @param $code + * @param $message + * @return \SimpleXMLElement + */ + public function getErrorXml($code, $message) { + $xml = new \SimpleXMLElement(''); + $errorData = ['code' => $code, 'message' => $message]; + + $this->arrayToXml($errorData, $xml); + + return $xml; } + /** + * check if xml string is valid + * + * @param $xmlString + * @return bool + */ + protected function isValidXmlString($xmlString) { + libxml_use_internal_errors(TRUE); + + $doc = simplexml_load_string($xmlString); + + if (!$doc) { + $errors = libxml_get_errors(); + + return empty($errors); + } + + return FALSE; + } + } From fd901fc95edf23fa1cd98c35af420deb5a446d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Lok=C5=A1=C3=ADk?= Date: Tue, 21 Feb 2017 13:27:25 +0100 Subject: [PATCH 3/5] doplnene datove typy do anotacii --- src/SmartEmailing.php | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/SmartEmailing.php b/src/SmartEmailing.php index 8050636..d574cf3 100644 --- a/src/SmartEmailing.php +++ b/src/SmartEmailing.php @@ -19,8 +19,9 @@ class SmartEmailing extends \Nette\Object /** * SmartEmailing constructor. - * @param $username - * @param $token + * + * @param string $username + * @param string $token */ public function __construct($username, $token) { @@ -31,8 +32,8 @@ public function __construct($username, $token) /** * insert contact to Smartemailing - * - * @param $email + * + * @param string $email * @param array $contactlists * @param array $properties * @param array $customfields @@ -46,7 +47,7 @@ public function contactInsert($email, $contactlists = array(), $properties = arr /** * update contact in Smartemailing * - * @param $email + * @param string $email * @param array $contactlists * @param array $properties * @param array $customfields @@ -93,7 +94,7 @@ public function contactUpdate($email, $contactlists = array(), $properties = arr /** * get Smartemailing contact by email address * - * @param String $email + * @param string $email * * @return \SimpleXMLElement */ @@ -117,7 +118,7 @@ public function getOneByEmail($email) { /** * get Smartemailing contact by ID * - * @param int $id + * @param int $id * * @return [ty\SimpleXMLElement */ @@ -141,7 +142,7 @@ public function contactGetOneByID($id) { /** * delete Smartemailing contact by email address * - * @param String $email + * @param string $email * * @return \SimpleXMLElement */ @@ -225,7 +226,7 @@ public function multipleContactsInsert($contacts) { /** * convert array to xml * - * @param $array + * @param array $array * @param $xml */ protected function arrayToXml($array, &$xml) { @@ -251,8 +252,8 @@ protected function arrayToXml($array, &$xml) { /** * creating simple xml * - * @param $array - * @param $rootElementName + * @param array $array + * @param string $rootElementName * @return mixed */ protected function createSimpleXml($array, $rootElementName) { @@ -267,7 +268,7 @@ protected function createSimpleXml($array, $rootElementName) { /** * connect to Smartemailing API v2 * - * @param $data + * @param array $data * @return \SimpleXMLElement */ protected function callSmartemailingApiWithCurl($data) { @@ -305,8 +306,8 @@ protected function callSmartemailingApiWithCurl($data) { /** * return error XML * - * @param $code - * @param $message + * @param string $code + * @param string $message * @return \SimpleXMLElement */ public function getErrorXml($code, $message) { @@ -322,7 +323,7 @@ public function getErrorXml($code, $message) { /** * check if xml string is valid * - * @param $xmlString + * @param string $xmlString * @return bool */ protected function isValidXmlString($xmlString) { From 6abd4a076ea3b8048446248073a6fbaad39deff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Lok=C5=A1=C3=ADk?= Date: Tue, 21 Feb 2017 13:30:00 +0100 Subject: [PATCH 4/5] doplneny datovy typ do anotacie --- src/SmartEmailing.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SmartEmailing.php b/src/SmartEmailing.php index d574cf3..3ab68b0 100644 --- a/src/SmartEmailing.php +++ b/src/SmartEmailing.php @@ -227,7 +227,7 @@ public function multipleContactsInsert($contacts) { * convert array to xml * * @param array $array - * @param $xml + * @param \SimpleXMLElement $xml */ protected function arrayToXml($array, &$xml) { foreach($array as $key => $value) { From 4cbb7819e59e67eff1d1847d4197e5958d52a470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Lok=C5=A1=C3=ADk?= Date: Tue, 21 Feb 2017 13:34:45 +0100 Subject: [PATCH 5/5] upravena anotacia funckie createSimpleXml --- src/SmartEmailing.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SmartEmailing.php b/src/SmartEmailing.php index 3ab68b0..ac95be5 100644 --- a/src/SmartEmailing.php +++ b/src/SmartEmailing.php @@ -254,7 +254,7 @@ protected function arrayToXml($array, &$xml) { * * @param array $array * @param string $rootElementName - * @return mixed + * @return string | bool ... string on success and FALSE on error */ protected function createSimpleXml($array, $rootElementName) { $xml = new \SimpleXMLElement('<' . $rootElementName . '>');