Skip to content

Commit

Permalink
Merge pull request #8 from goldspecdigital/develop
Browse files Browse the repository at this point in the history
implemented response objects (#7)
  • Loading branch information
matthew-inamdar authored Jun 30, 2018
2 parents fb4a8e8 + 9b9f389 commit d8f55a1
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 15 deletions.
18 changes: 10 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use GoldSpecDigital\VoodooSmsSdk\Exceptions\ExternalReferenceTooLongException;
use GoldSpecDigital\VoodooSmsSdk\Exceptions\MessageTooLongException;
use GoldSpecDigital\VoodooSmsSdk\Responses\DeliveryStatusResponse;
use GoldSpecDigital\VoodooSmsSdk\Responses\SendSmsResponse;
use GuzzleHttp\Client as HttpClient;
use InvalidArgumentException;

Expand Down Expand Up @@ -63,13 +65,12 @@ public function __construct(string $username, string $password, string $from = n
* @param string $to
* @param null|string $from
* @param null|string $externalReference The external reference.
* @return object
* @throws \Exception
* @return \GoldSpecDigital\VoodooSmsSdk\Responses\SendSmsResponse
* @throws \InvalidArgumentException
* @throws \GoldSpecDigital\VoodooSmsSdk\Exceptions\MessageTooLongException
* @throws \GoldSpecDigital\VoodooSmsSdk\Exceptions\ExternalReferenceTooLongException
*/
public function send(string $message, string $to, string $from = null, string $externalReference = null): object
public function send(string $message, string $to, string $from = null, string $externalReference = null): SendSmsResponse
{
if (strlen($message) > static::MESSAGE_LIMIT) {
throw new MessageTooLongException();
Expand Down Expand Up @@ -101,16 +102,16 @@ public function send(string $message, string $to, string $from = null, string $e
$parameters = array_filter($parameters, [$this, 'isNotNull']);

$response = $this->httpClient->post($uri, ['form_params' => $parameters]);
$responseContents = json_decode((string)$response->getBody(), true);

return json_decode((string)$response->getBody());
return new SendSmsResponse($responseContents);
}

/**
* @param string $referenceID
* @return object
* @throws \Exception
* @return \GoldSpecDigital\VoodooSmsSdk\Responses\DeliveryStatusResponse
*/
public function getDeliveryStatus(string $referenceID): object
public function getDeliveryStatus(string $referenceID): DeliveryStatusResponse
{
$uri = 'getDlrStatus';
$parameters = [
Expand All @@ -120,8 +121,9 @@ public function getDeliveryStatus(string $referenceID): object
];

$response = $this->httpClient->post($uri, ['form_params' => $parameters]);
$responseContents = json_decode((string)$response->getBody(), true);

return json_decode((string)$response->getBody());
return new DeliveryStatusResponse($responseContents);
}

/**
Expand Down
50 changes: 50 additions & 0 deletions src/Responses/DeliveryStatusResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace GoldSpecDigital\VoodooSmsSdk\Responses;

use DateTime;

class DeliveryStatusResponse extends Response
{
/**
* @return int
*/
public function getResult(): int
{
return (int)$this->response['result'];
}

/**
* @return string
*/
public function getReferenceId(): string
{
return $this->response['reference_id'];
}

/**
* @return string
*/
public function getMessage(): string
{
return $this->response['message'];
}

/**
* @return string
*/
public function getDeliveryStatus(): string
{
return $this->response['delivery_status'];
}

/**
* @return \DateTime
*/
public function getDeliveryDateTime(): DateTime
{
return new DateTime($this->response['delivery_datetime']);
}
}
23 changes: 23 additions & 0 deletions src/Responses/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace GoldSpecDigital\VoodooSmsSdk\Responses;

abstract class Response
{
/**
* @var array
*/
protected $response;

/**
* SendSmsResponse constructor.
*
* @param array $response
*/
public function __construct(array $response)
{
$this->response = $response;
}
}
32 changes: 32 additions & 0 deletions src/Responses/SendSmsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace GoldSpecDigital\VoodooSmsSdk\Responses;

class SendSmsResponse extends Response
{
/**
* @return int
*/
public function getResult(): int
{
return (int)$this->response['result'];
}

/**
* @return string
*/
public function getResultText(): string
{
return $this->response['resultText'];
}

/**
* @return array
*/
public function getReferenceId(): array
{
return $this->response['reference_id'];
}
}
14 changes: 7 additions & 7 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testSendWorksWithExternalReference(): void
$client = $this->createClient();
$response = $client->send('This is a test message', getenv('VOODOO_TO'), 'PHPUnitTest', 'testing');

$this->assertEquals(200, $response->result);
$this->assertEquals(200, $response->getResult());
}

/**
Expand All @@ -43,7 +43,7 @@ public function testSendWorksWithoutExternalReference(): void
$client = $this->createClient();
$response = $client->send('This is a test message', getenv('VOODOO_TO'), 'PHPUnitTest');

$this->assertEquals(200, $response->result);
$this->assertEquals(200, $response->getResult());
}

/**
Expand All @@ -55,7 +55,7 @@ public function testSendWorksWithFromSetInConstructor(): void
$client = $this->createClient('PHPUnitTest');
$response = $client->send('This is a test message', getenv('VOODOO_TO'));

$this->assertEquals(200, $response->result);
$this->assertEquals(200, $response->getResult());
}

/**
Expand All @@ -65,11 +65,11 @@ public function testGetDeliveryStatusWorks(): void
{
$client = $this->createClient('PHPUnitTest');
$response = $client->send('This is a test message', getenv('VOODOO_TO'));
$deliveryStatus = $client->getDeliveryStatus($response->reference_id[0]);
$deliveryStatus = $client->getDeliveryStatus($response->getReferenceId()[0]);

$this->assertEquals('200 OK', $deliveryStatus->result);
$this->assertEquals($response->reference_id[0], $deliveryStatus->reference_id);
$this->assertEquals('This is a test message', $deliveryStatus->message);
$this->assertEquals(200, $deliveryStatus->getResult());
$this->assertEquals($response->getReferenceId()[0], $deliveryStatus->getReferenceId());
$this->assertEquals('This is a test message', $deliveryStatus->getMessage());
}

/**
Expand Down

0 comments on commit d8f55a1

Please sign in to comment.