@@ -17,17 +17,41 @@ class SmartEmailing extends \Nette\Object
1717
1818 protected $ token ;
1919
20+ /**
21+ * SmartEmailing constructor.
22+ * @param $username
23+ * @param $token
24+ */
2025 public function __construct ($ username , $ token )
2126 {
2227 $ this ->username = $ username ;
2328 $ this ->token = $ token ;
2429 }
2530
31+
32+ /**
33+ * insert contact to Smartemailing
34+ *
35+ * @param $email
36+ * @param array $contactlists
37+ * @param array $properties
38+ * @param array $customfields
39+ * @return \SimpleXMLElement
40+ */
2641 public function contactInsert ($ email , $ contactlists = array (), $ properties = array (), $ customfields = array ()) {
2742 return $ this ->contactUpdate ($ email , $ contactlists , $ properties , $ customfields );
2843 }
2944
3045
46+ /**
47+ * update contact in Smartemailing
48+ *
49+ * @param $email
50+ * @param array $contactlists
51+ * @param array $properties
52+ * @param array $customfields
53+ * @return \SimpleXMLElement
54+ */
3155 public function contactUpdate ($ email , $ contactlists = array (), $ properties = array (), $ customfields = array ()) {
3256 $ details = [];
3357
@@ -168,8 +192,6 @@ public function getAllUnsubscribedContacts() {
168192 * @return \SimpleXMLElement
169193 */
170194 public function multipleContactsInsert ($ contacts ) {
171- $ dateTime = new \DateTime ();
172-
173195 $ contactsArray = [];
174196
175197 foreach ($ contacts as $ email => $ cData ) {
@@ -202,6 +224,9 @@ public function multipleContactsInsert($contacts) {
202224
203225 /**
204226 * convert array to xml
227+ *
228+ * @param $array
229+ * @param $xml
205230 */
206231 protected function arrayToXml ($ array , &$ xml ) {
207232 foreach ($ array as $ key => $ value ) {
@@ -225,6 +250,10 @@ protected function arrayToXml($array, &$xml) {
225250
226251 /**
227252 * creating simple xml
253+ *
254+ * @param $array
255+ * @param $rootElementName
256+ * @return mixed
228257 */
229258 protected function createSimpleXml ($ array , $ rootElementName ) {
230259 $ xml = new \SimpleXMLElement ('< ' . $ rootElementName . '></ ' . $ rootElementName . '> ' );
@@ -235,6 +264,12 @@ protected function createSimpleXml($array, $rootElementName) {
235264 }
236265
237266
267+ /**
268+ * connect to Smartemailing API v2
269+ *
270+ * @param $data
271+ * @return \SimpleXMLElement
272+ */
238273 protected function callSmartemailingApiWithCurl ($ data ) {
239274 try {
240275 $ ch = curl_init ();
@@ -254,17 +289,54 @@ protected function callSmartemailingApiWithCurl($data) {
254289 curl_close ($ ch );
255290
256291 } catch (\Exception $ e ) {
257- $ xml = new \SimpleXMLElement ('<response></response> ' );
258- $ errorData = ['code ' => $ e ->getCode (), 'message ' => $ e ->getMessage ()];
292+ return $ this ->getErrorXml ($ e ->getCode (), $ e ->getMessage ());
293+ }
294+
259295
260- $ this ->arrayToXml ($ errorData , $ xml );
261-
262- return $ xml ;
296+ if ($ this ->isValidXmlString ($ response )) {
297+ return new \SimpleXMLElement ($ response );
298+
299+ } else {
300+ return $ this ->getErrorXml ('500 ' , 'Unknown Smartemailing API error. ' );
263301 }
302+ }
264303
265304
266- return new \SimpleXMLElement ($ response );
305+ /**
306+ * return error XML
307+ *
308+ * @param $code
309+ * @param $message
310+ * @return \SimpleXMLElement
311+ */
312+ public function getErrorXml ($ code , $ message ) {
313+ $ xml = new \SimpleXMLElement ('<response></response> ' );
314+ $ errorData = ['code ' => $ code , 'message ' => $ message ];
315+
316+ $ this ->arrayToXml ($ errorData , $ xml );
317+
318+ return $ xml ;
267319 }
268320
269321
322+ /**
323+ * check if xml string is valid
324+ *
325+ * @param $xmlString
326+ * @return bool
327+ */
328+ protected function isValidXmlString ($ xmlString ) {
329+ libxml_use_internal_errors (TRUE );
330+
331+ $ doc = simplexml_load_string ($ xmlString );
332+
333+ if (!$ doc ) {
334+ $ errors = libxml_get_errors ();
335+
336+ return empty ($ errors );
337+ }
338+
339+ return FALSE ;
340+ }
341+
270342}
0 commit comments