Skip to content

Commit

Permalink
Merge pull request #24 from joshwtredmond/master
Browse files Browse the repository at this point in the history
File cleanup
  • Loading branch information
Moinax committed Nov 1, 2016
2 parents 6525ece + a14fca8 commit da9b523
Showing 1 changed file with 40 additions and 38 deletions.
78 changes: 40 additions & 38 deletions src/Snowcap/Emarsys/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ class Client
* @var string
*/
private $baseUrl = 'https://api.emarsys.net/api/v2/';

/**
* @var string
*/
private $username;

/**
* @var string
*/
Expand Down Expand Up @@ -63,11 +65,11 @@ public function __construct(
HttpClient $client, $username, $secret, $baseUrl = null, $fieldsMap = array(), $choicesMap = array()
)
{
$this->client = $client;
$this->client = $client;
$this->username = $username;
$this->secret = $secret;
$this->fieldsMapping = $fieldsMap;
$this->choicesMapping = $choicesMap;
$this->fieldsMapping = $fieldsMap;
$this->choicesMapping = $choicesMap;

if (null !== $baseUrl) {
$this->baseUrl = $baseUrl;
Expand Down Expand Up @@ -115,15 +117,15 @@ public function addFieldsMapping($mapping = array())
*/
public function addChoicesMapping($mapping = array())
{
foreach ($mapping as $field => $choices) {
if (is_array($choices)) {
if (!array_key_exists($field, $this->choicesMapping)) {
$this->choicesMapping[$field] = array();
}
foreach ($mapping as $field => $choices) {
if (is_array($choices)) {
if (!array_key_exists($field, $this->choicesMapping)) {
$this->choicesMapping[$field] = array();
}

$this->choicesMapping[$field] = array_merge($this->choicesMapping[$field], $choices);
}
}
$this->choicesMapping[$field] = array_merge($this->choicesMapping[$field], $choices);
}
}
}

/**
Expand All @@ -140,10 +142,10 @@ public function getFieldId($field)
}

if (!isset($this->fieldsMapping[$field])) {
throw new ClientException(sprintf('Unrecognized field name "%s"', $field));
throw new ClientException(sprintf('Unrecognized field name "%s"', $field));
}

return (int)$this->fieldsMapping[$field];
return (int)$this->fieldsMapping[$field];
}

/**
Expand Down Expand Up @@ -173,7 +175,7 @@ public function getFieldName($fieldId)
*/
public function getChoiceId($field, $choice)
{
$fieldName = $this->getFieldName($field);
$fieldName = $this->getFieldName($field);

if (!array_key_exists($fieldName, $this->choicesMapping)) {
throw new ClientException(sprintf('Unrecognized field "%s" for choice "%s"', $field, $choice));
Expand All @@ -197,7 +199,7 @@ public function getChoiceId($field, $choice)
*/
public function getChoiceName($field, $choiceId)
{
$fieldName = $this->getFieldName($field);
$fieldName = $this->getFieldName($field);

if(!array_key_exists($fieldName, $this->choicesMapping)) {
throw new ClientException(sprintf('Unrecognized field "%s" for choice id "%s"', $field, $choiceId));
Expand Down Expand Up @@ -236,11 +238,11 @@ public function getConditions()
public function createContact(array $data)
{
if (isset($data['contacts']) && is_array($data['contacts'])){
foreach($data['contacts'] as &$contact){
$contact = $this->mapFieldsToIds($contact);
}
foreach($data['contacts'] as &$contact){
$contact = $this->mapFieldsToIds($contact);
}
}

return $this->send(HttpClient::POST, 'contact', $this->mapFieldsToIds($data));
}

Expand All @@ -257,7 +259,7 @@ public function updateContact(array $data)
$contact = $this->mapFieldsToIds($contact);
}
}

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

Expand Down Expand Up @@ -697,20 +699,20 @@ public function createSource(array $data)
{
return $this->send(HttpClient::POST, 'source/create', $data);
}

/**
* creates custom field in your Emarsys account
*
* @param string $name
* @param string $type shorttext|longtext|largetext|date|url|numeric
*
* @return Response
*/
* creates custom field in your Emarsys account
*
* @param string $name
* @param string $type shorttext|longtext|largetext|date|url|numeric
*
* @return Response
*/
public function createCustomField($name, $type)
{
return $this->send(HttpClient::POST, 'field', array('name'=>$name, 'application_type'=>$type));
}

/**
* @param string $method
* @param string $uri
Expand All @@ -720,18 +722,18 @@ public function createCustomField($name, $type)
*/
protected function send($method = 'GET', $uri, array $body = array())
{
$headers = array('Content-Type: application/json', 'X-WSSE: ' . $this->getAuthenticationSignature());
$uri = $this->baseUrl . $uri;
$headers = array('Content-Type: application/json', 'X-WSSE: ' . $this->getAuthenticationSignature());
$uri = $this->baseUrl . $uri;

try {
$responseJson = $this->client->send($method, $uri, $headers, $body);
} catch (\Exception $e) {
throw new ServerException($e->getMessage());
}
try {
$responseJson = $this->client->send($method, $uri, $headers, $body);
} catch (\Exception $e) {
throw new ServerException($e->getMessage());
}

$responseArray = json_decode($responseJson, true);
$responseArray = json_decode($responseJson, true);

return new Response($responseArray);
return new Response($responseArray);
}

/**
Expand Down

0 comments on commit da9b523

Please sign in to comment.