Skip to content

Commit

Permalink
Cleanup of code
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin van de Belt committed Mar 25, 2015
1 parent fdd4437 commit 1de1417
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 108 deletions.
150 changes: 75 additions & 75 deletions src/Vdbelt/InmobileSmsApi/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,97 +32,97 @@ class Connector
* @param null $endpoint
*/
public function __construct($api_key, $endpoint = null)
{
$this->api_key = $api_key;
{
$this->api_key = $api_key;

if(!is_null($endpoint))
$this->setEndpoint($endpoint);
}
if(!is_null($endpoint))
$this->setEndpoint($endpoint);
}

/**
* Get the current endpoint
* @param $endpoint
* @return string
*/
public function getEndpoint()
{
return $this->endpoint;
}
{
return $this->endpoint;
}

/**
* Get the current API key
* @return string
*/
public function getApiKey()
{
return $this->api_key;
}
{
return $this->api_key;
}

/**
* Get an array with the current message objects
* @return array
*/
public function getMessages()
{
return $this->messages;
}
{
return $this->messages;
}

/**
* Set the endpoint
* @param $endpoint
* @return $this
*/
public function setEndpoint($endpoint)
{
$this->endpoint = $endpoint;
{
$this->endpoint = $endpoint;

return $this;
}
return $this;
}

/**
* Set the API key
* @param $api_key
* @return $this
*/
public function setApiKey($api_key)
{
$this->api_key = $api_key;
{
$this->api_key = $api_key;

return $this;
}
return $this;
}

/**
* Add a message to the payload
* @param Message $Message
* @return $this
*/
public function addMessage(Message $Message)
{
$this->messages[] = $Message;
{
$this->messages[] = $Message;

return $this;
}
return $this;
}

/**
* Send the actual payload to the endpoint
* @return bool
* @throws \Exception
*/
public function send()
{
if(count($this->messages) < 1)
throw new \Exception('No messages to send');
{
if(count($this->messages) < 1)
throw new \Exception('No messages to send');

$builder = new XML_Payload_Builder($this);
$client = $this->getHttpClient();
$builder = new XML_Payload_Builder($this);
$client = $this->getHttpClient();

$client->post($this->endpoint.'/Api/V2/SendMessages', [
'body' => [
'xml' => $builder->getXML()
]]);
$client->post($this->endpoint.'/Api/V2/SendMessages', [
'body' => [
'xml' => $builder->getXML()
]]);

return true;
}
return true;
}

/**
* Get the http client using
Expand Down Expand Up @@ -159,70 +159,70 @@ class XML_Payload_Builder
* @param Connector $Connector
*/
public function __construct(Connector $Connector)
{
$this->connector = $Connector;
$this->dom = new \DomDocument('1.0', 'UTF-8');
{
$this->connector = $Connector;
$this->dom = new \DomDocument('1.0', 'UTF-8');

$request = $this->dom->createElement('request');
$request = $this->dom->createElement('request');

$request->appendChild($this->buildAuthenticationHeader());
$request->appendChild($this->buildMessages());
$request->appendChild($this->buildAuthenticationHeader());
$request->appendChild($this->buildMessages());

$this->dom->appendChild($request);
}
$this->dom->appendChild($request);
}

/**
* Returns the XML payload
* @return mixed
*/
public function getXML()
{
return $this->dom->saveXML();
}
{
return $this->dom->saveXML();
}

/**
* Builds the authentication header to add
* @return mixed
*/
protected function buildAuthenticationHeader()
{
$authentication = $this->dom->createElement('authentication');
$authentication->setAttribute('apikey', $this->connector->getApiKey());
return $authentication;
}
{
$authentication = $this->dom->createElement('authentication');
$authentication->setAttribute('apikey', $this->connector->getApiKey());

return $authentication;
}

/**
* Transforms Message objects into XML
* @return mixed
*/
protected function buildMessages()
{
$data = $this->dom->createElement('data');
{
$data = $this->dom->createElement('data');

foreach($this->connector->getMessages() as $Message)
{
$element = $this->dom->createElement('message');
$sendername = $this->dom->createElement('sendername', $Message->getSenderName());
$text = $this->dom->createElement('text');
$recipients = $this->dom->createElement('recipients');
foreach($this->connector->getMessages() as $Message)
{
$element = $this->dom->createElement('message');
$sendername = $this->dom->createElement('sendername', $Message->getSenderName());
$text = $this->dom->createElement('text');
$recipients = $this->dom->createElement('recipients');

foreach($Message->getRecipients() as $Recipient)
{
$msisdn = $this->dom->createElement('msisdn', $Recipient);
$recipients->appendChild($msisdn);
}
foreach($Message->getRecipients() as $Recipient)
{
$msisdn = $this->dom->createElement('msisdn', $Recipient);
$recipients->appendChild($msisdn);
}

$text->appendChild($this->dom->createCDATASection($Message->getContent()));
$text->appendChild($this->dom->createCDATASection($Message->getContent()));

$element->appendChild($sendername);
$element->appendChild($text);
$element->appendChild($recipients);
$element->appendChild($sendername);
$element->appendChild($text);
$element->appendChild($recipients);

$data->appendChild($element);
}
$data->appendChild($element);
}

return $data;
}
return $data;
}

}
66 changes: 33 additions & 33 deletions src/Vdbelt/InmobileSmsApi/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,63 +31,63 @@ class Message
* @param $sendername
*/
public function __construct($content, array $recipients, $sendername)
{
$this->setContent($content);
$this->setRecipients($recipients);
$this->setSenderName($sendername);
}
{
$this->setContent($content);
$this->setRecipients($recipients);
$this->setSenderName($sendername);
}

/**
* Get array with msisdns of recipients
* @return array
*/
public function getRecipients()
{
return $this->recipients;
}
{
return $this->recipients;
}

/**
* Get string containing the sendername
* @return string
*/
public function getSenderName()
{
return $this->sendername;
}
{
return $this->sendername;
}

/**
* Get string containing the content
* @return string
*/
public function getContent()
{
return $this->content;
}
{
return $this->content;
}

/**
* Set array with msisdns of the recipients
* @param array $recipients
* @return $this
*/
public function setRecipients(array $recipients)
{
foreach($recipients as $recipient)
$this->addRecipient($recipient);
{
foreach($recipients as $recipient)
$this->addRecipient($recipient);

return $this;
}
return $this;
}

/**
* Add a recipient to the array with msisdn
* @param $recipient
* @return $this
*/
public function addRecipient($recipient)
{
$this->recipients[] = $recipient;
{
$this->recipients[] = $recipient;

return $this;
}
return $this;
}

/**
* Set the sendername and throw an exception if invalid
Expand All @@ -96,25 +96,25 @@ public function addRecipient($recipient)
* @throws \Exception
*/
public function setSenderName($sendername)
{
if(strlen($sendername) > 16 OR strlen($sendername) < 4)
throw new \Exception('Invalid sendername');
{
if(strlen($sendername) > 16 OR strlen($sendername) < 4)
throw new \Exception('Invalid sendername');

$this->sendername = $sendername;
$this->sendername = $sendername;

return $this;
}
return $this;
}

/**
* Set the content of the message
* @param $content
* @return $this
*/
public function setContent($content)
{
$this->content = $content;
{
$this->content = $content;

return $this;
}
return $this;
}

}

0 comments on commit 1de1417

Please sign in to comment.