Skip to content

Commit 5783c78

Browse files
Merge pull request #29 from webarchitect609/fix/update_contact
Fix updateContact method according to API manual
2 parents 6615e4f + 9198836 commit 5783c78

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/Snowcap/Emarsys/Client.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,7 @@ public function getConditions()
237237
*/
238238
public function createContact(array $data)
239239
{
240-
if (isset($data['contacts']) && is_array($data['contacts'])){
241-
foreach($data['contacts'] as &$contact){
242-
$contact = $this->mapFieldsToIds($contact);
243-
}
244-
}
240+
$data = $this->mapFieldsForMultipleContacts($data);
245241

246242
return $this->send(HttpClient::POST, 'contact', $this->mapFieldsToIds($data));
247243
}
@@ -254,15 +250,25 @@ public function createContact(array $data)
254250
*/
255251
public function updateContact(array $data)
256252
{
257-
if (isset($data['contacts']) && is_array($data['contacts'])){
258-
foreach($data['contacts'] as &$contact){
259-
$contact = $this->mapFieldsToIds($contact);
260-
}
261-
}
253+
$data = $this->mapFieldsForMultipleContacts($data);
262254

263255
return $this->send(HttpClient::PUT, 'contact', $this->mapFieldsToIds($data));
264256
}
265257

258+
/**
259+
* Updates one or more contacts/recipients, identified by an external ID. If the contact does not exist in the
260+
* database, it is created.
261+
*
262+
* @param array $data
263+
* @return Response
264+
*/
265+
public function updateContactAndCreateIfNotExists(array $data)
266+
{
267+
$data = $this->mapFieldsForMultipleContacts($data);
268+
269+
return $this->send(HttpClient::PUT, 'contact/?create_if_not_exists=1', $this->mapFieldsToIds($data));
270+
}
271+
266272
/**
267273
* Deletes a single contact/recipient, identified by an external ID.
268274
*
@@ -860,4 +866,18 @@ private function castIniFileValues($data)
860866

861867
return $data;
862868
}
869+
870+
/**
871+
* @param array $data
872+
* @return array
873+
*/
874+
private function mapFieldsForMultipleContacts(array $data)
875+
{
876+
if (!isset($data['contacts']) || !is_array($data['contacts'])) {
877+
return $data;
878+
}
879+
880+
return array_merge($data, ['contacts' => array_map([$this, 'mapFieldsToIds'], $data['contacts'])]);
881+
}
882+
863883
}

0 commit comments

Comments
 (0)