Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
response to message feature merge
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspijak committed Jul 3, 2018
1 parent 146605d commit ee56841
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 7 deletions.
25 changes: 25 additions & 0 deletions src/Sms/BulkMessage.php
Expand Up @@ -41,6 +41,31 @@ public function addMessage(Message $message)
}


/**
* @param BulkGate\Message\Response $response
*/
public function setStatus(BulkGate\Message\Response $response)
{
foreach($this->array as $key => $item)
{
if($item instanceof Message)
{
if(isset($response->response) && is_array($response->response) && isset($response->response[$key]))
{
$item->setStatus(
isset($response->response[$key]['status']) ? $response->response[$key]['status'] : 'error',
isset($response->response[$key]['sms_id']) ? $response->response[$key]['sms_id'] : '',
isset($response->response[$key]['price']) ? $response->response[$key]['price'] : 0.0);
}
else
{
$item->setStatus('error');
}
}
}
}


/**
* @return string
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Sms/IMessage.php
Expand Up @@ -13,6 +13,12 @@ interface IMessage

const TEXT = 'text';

const PRICE = 'price';

const STATUS = 'status';

const ID = 'id';

const ISO = 'iso';

const VARIABLES = 'variables';
Expand Down
57 changes: 55 additions & 2 deletions src/Sms/Message.php
Expand Up @@ -21,6 +21,15 @@ class Message implements IMessage, \JsonSerializable
/** @var BulkGate\Sms\Message\Text */
private $text;

/** @var string */
private $status = 'preparation';

/** @var string|null */
private $id = null;

/** @var float */
private $price = 0.0;


/**
* Message constructor.
Expand Down Expand Up @@ -73,6 +82,22 @@ public function text($text, array $variables = [])
}


/**
* @param string $status
* @param string|null $id
* @param float $price
* @return $this
*/
public function setStatus($status, $id = null, $price = 0.0)
{
$this->status = (string) $status;
$this->id = $id !== null ? (string) $id : null;
$this->price = (float) $price;

return $this;
}


/**
* @return Message\PhoneNumber
*/
Expand All @@ -91,6 +116,33 @@ public function getText()
}


/**
* @return string
*/
public function getStatus()
{
return (string) $this->status;
}


/**
* @return string|null
*/
public function getId()
{
return $this->id !== null ? (string) $this->id : null;
}


/**
* @return float
*/
public function getPrice()
{
return (float) $this->price;
}


/**
* @return string
*/
Expand All @@ -108,10 +160,12 @@ public function toArray()
return [
self::NUMBER => $this->phone_number,
self::TEXT => $this->text,
self::STATUS => $this->status,
self::PRICE => $this->price,
self::ID => $this->id
];
}


/**
* @return array
*/
Expand All @@ -120,7 +174,6 @@ public function jsonSerialize()
return $this->toArray();
}


/**
* @return string
*/
Expand Down
21 changes: 20 additions & 1 deletion src/Sms/Sender.php
Expand Up @@ -100,12 +100,31 @@ public function send(IMessage $message)
{
$this->fillDefaultCountryIso($message);

return $this->connection->send(new Request($message->getType(), [
/** @var Response $response */
$response = $this->connection->send(new Request($message->getType(), [
self::MESSAGE => $message,
self::SENDER => $this->senderSettings instanceof ISenderSettings ? $this->senderSettings : [],
self::UNICODE => $this->unicode,
self::FLASH => $this->flash,
], true));

if($message instanceof BulkMessage)
{
if($response->isSuccess())
{
$message->setStatus($response);
}
}
else if($message instanceof Message)
{
$message->setStatus(
isset($response->status) ? $response->status : 'error',
isset($response->sms_id) ? $response->sms_id : '',
isset($response->price) ? $response->price : 0.0
);
}

return $response;
}


Expand Down
9 changes: 5 additions & 4 deletions tests/Sms/BulkMessage.phpt
Expand Up @@ -9,6 +9,7 @@
namespace Test;

use BulkGate\Sms\BulkMessage, BulkGate\Sms\Message;

use Tester\Assert;

require __DIR__ . '/../bootstrap.php';
Expand All @@ -29,8 +30,8 @@ Assert::same('bulk-sms', $bulk->getType());

Assert::equal(
[
['number' => $phone_number, 'text' => $bulk->get(0)->getText()],
['number' => $phone_number_new, 'text' => $bulk->get(1)->getText()],
['number' => $phone_number, 'text' => $bulk->get(0)->getText(), 'status' => 'preparation', 'price' => 0.0, 'id' => null],
['number' => $phone_number_new, 'text' => $bulk->get(1)->getText(), 'status' => 'preparation', 'price' => 0.0, 'id' => null],
],
$bulk->toArray()
);
Expand All @@ -42,8 +43,8 @@ $bulk->addMessage($message);

Assert::equal(
[
['number' => $phone_number, 'text' => new Message\Text($text)],
['number' => $phone_number_new, 'text' => new Message\Text($text_new)],
['number' => $phone_number, 'text' => new Message\Text($text), 'status' => 'preparation', 'price' => 0.0, 'id' => null],
['number' => $phone_number_new, 'text' => new Message\Text($text_new), 'status' => 'preparation', 'price' => 0.0, 'id' => null],
$message->toArray(),
],
$bulk->toArray()
Expand Down
10 changes: 10 additions & 0 deletions tests/Sms/Message.phpt
Expand Up @@ -37,3 +37,13 @@ Assert::type("BulkGate\\Sms\\Message\\PhoneNumber", $message->getPhoneNumber());
Assert::type("BulkGate\\Sms\\Message\\Text", $message->getText());

Assert::same(Sms\Message::TYPE, $message->getType());

$message->setStatus('accepted', 'id', 1.2);

Assert::equal([
'number' => $message->getPhoneNumber(),
'text' => $message->getText(),
'status' => 'accepted',
'id' => 'id',
'price' => 1.2
], $message->toArray());

0 comments on commit ee56841

Please sign in to comment.