Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit 75a3a0c

Browse files
Magnus LindgrenBarnebys
authored andcommitted
Added getMemberByEmail method and tested php 5.5.9
1 parent c42680f commit 75a3a0c

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
}
1010
},
1111
"require": {
12-
"php": ">=5.6",
12+
"php": ">=5.5",
1313
"ext-curl": "*",
1414
"psr/log": "~1.0",
1515
"mautic/api-library": "2.10.0"
1616
}
17-
}
17+
}

src/Api.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)