@@ -56,6 +56,32 @@ public static function getContactIdByMail($mail)
5656 throw new ContactNotFoundException ();
5757 }
5858
59+ /**
60+ * @param string $mail
61+ * @return integer
62+ *
63+ * @throws ContactNotFoundException
64+ * @throws Exception\AuthInstanceException
65+ * @throws \Exception
66+ */
67+ public static function getContactByMail ($ mail )
68+ {
69+ $ api = self ::getApiInstance ();
70+
71+ $ parameters = [
72+ 'search ' => $ mail ,
73+ 'limit ' => 1
74+ ];
75+
76+ $ data = $ api ->makeRequest ('contacts ' , $ parameters );
77+ $ contacts = $ data ['contacts ' ];
78+ if (sizeof ($ contacts ) > 0 ){
79+ return current ($ contacts );
80+ }
81+
82+ throw new ContactNotFoundException ();
83+ }
84+
5985 /**
6086 * @param string $email
6187 * @param integer $templateId
@@ -91,7 +117,11 @@ public static function sendToContact($email, $templateId, array $tokens = [], $h
91117 $ tokens = ['tokens ' => $ tokens ];
92118 }
93119
94- $ contactId = self ::getContactIdByMail ($ email );
120+ try {
121+ $ contactId = self ::getContactIdByMail ($ email );
122+ } catch (ContactNotFoundException $ e ) {
123+ $ contactId = self ::createContact ($ email );
124+ }
95125
96126 $ api = self ::getApiInstance ();
97127
@@ -111,7 +141,11 @@ public static function sendToContact($email, $templateId, array $tokens = [], $h
111141 */
112142 public static function updateContact ($ email , array $ data ) {
113143
114- $ contactId = self ::getContactIdByMail ($ email );
144+ try {
145+ $ contactId = self ::getContactIdByMail ($ email );
146+ } catch (ContactNotFoundException $ e ) {
147+ $ contactId = self ::createContact ($ email );
148+ }
115149
116150 $ api = self ::getApiInstance ();
117151
0 commit comments