Skip to content

Commit

Permalink
Merge branch 'release/2.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslaw.wasilewski committed Aug 25, 2023
2 parents 3d9e6af + d1ade95 commit 7d179a7
Show file tree
Hide file tree
Showing 29 changed files with 389 additions and 126 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ data/DoctrineORMModule/cache/
demos/
extras/documentation
/tests/src/config.local.php
/nbproject/
/.idea/
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ Currently supported gateways:
* [SerwerSms.pl](https://serwersms.pl/),
* [Infobip.com](http://www.infobip.com/)
* [Clickatell](https://www.clickatell.com/)
* [SmsPlanet.pl](https://smsplanet.pl/)
* [Orange SMSOffnetNat](https://api.orange.pl/produktyapi.html)
* Cisco EHWIC and 880G for 3.7G (HSPA+)/3.5G (HSPA) device,
* text files generator (for gateways which monitor shared folder,
* mock (dummy gateway for testing)
* [Orange SMSOffnetNat](https://api.orange.pl/produktyapi.html)

The library can be easily extended to support new gateways or integrated into your application, such as filtering of recipients based on consent to receiving SMS messages.

Expand Down Expand Up @@ -59,7 +60,7 @@ $result = $smsSender->send();

See tests for more examples.

See https://github.com/Orajo/zf-sms-zilla if you need ZendFramework 2 module for sending SMS. The module is based on this library.
See https://github.com/Orajo/zf-sms-zilla if you need Laminas module for sending SMS. The module is based on this library.

Author
------
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"SmsCenter",
"Infobip",
"Cisco",
"Orange"
"Orange",
"SmsPlanet"
],
"homepage": "https://github.com/Orajo/sms-zilla",
"authors": [
Expand All @@ -23,17 +24,17 @@
}
],
"require": {
"php": "^7.1 || ^8.0",
"arcturial/clickatell": "3.*",
"smsapi.pl/php-client": "^1.7",
"mlebkowski/mobitex": "1.0.*",
"giggsey/libphonenumber-for-php": "8.*",
"laminas/laminas-http": "2.*",
"serwersms/serwersms-php-client": "1.*",
"laminas/laminas-dependency-plugin": "^2.1",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "5.*"
"phpunit/phpunit": "^9.6"
},
"autoload": {
"psr-0": {
Expand Down
38 changes: 20 additions & 18 deletions src/SmsZilla/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class AbstractAdapter implements AdapterInterface {
* Adapter configuration
* @var array
*/
protected $params = null;
protected $params = [];

/**
* List of errors while sendind messages to recipients
Expand All @@ -38,10 +38,10 @@ abstract class AbstractAdapter implements AdapterInterface {
* Constructor
* @param array $params Adapter parameters
*/
public function __construct($params = null) {
public function __construct($params = []) {
$this->clearErrors();

if (is_array($params) && !empty($params)) {
if (!empty($params)) {
$this->setParams($params);
}
}
Expand All @@ -54,16 +54,17 @@ public function __construct($params = null) {
* @param bool $skipErrors If false error during sending message breaks sending others
* @return bool true if success, false if error
*/
abstract function send(MessageInterface $message, $skipErrors = true);
abstract function send(MessageInterface $message, bool $skipErrors = true): bool;

/**
* Returns value gateway param
*
* @param string $name
* @return mixed Value of the $name parameter
*/
public function getParam($name) {
public function getParam(string $name) {
if (empty($name) || !is_string($name)) {
throw new \InvalidArgumentException('Paramater name must be not empty string');
throw new \InvalidArgumentException('Parameter name must be not empty string');
}

if (isset($this->params[$name]) || array_key_exists($name, $this->params)) {
Expand All @@ -74,14 +75,12 @@ public function getParam($name) {

/**
* Sets options of gateway
*
* @param array $params List of options as associative array name => value
* @return mixed Value of the $name parameter
* @return AdapterInterface Value of the $name parameter
* @throws ConfigurationException
*/
public function setParams($params) {
if (!is_array($params)) {
throw new \InvalidArgumentException('Paramater $params must be an array.');
}

public function setParams(array $params): AdapterInterface {
foreach ($params as $name => $value) {
if (isset($this->params[$name]) || array_key_exists($name, $this->params)) {
if (is_array($this->params[$name])) {
Expand All @@ -97,14 +96,15 @@ public function setParams($params) {
throw new ConfigurationException(sprintf('Parameter "%s" doesn\'t exists', $name));
}
}
return $this;
}

/**
* Return all errors, that occured during sending process.
* Error code depends of selected Adapter
* @return ArrayObject
* Return all errors, that occurred during sending process.
* Error code depends on selected Adapter
* @return \ArrayObject
*/
public function getErrors() {
public function getErrors(): \ArrayObject {
return $this->errors;
}

Expand All @@ -113,7 +113,8 @@ public function getErrors() {
* @param SendingErrorInterface $error
* @return AbstractAdapter
*/
protected function addError(SendingErrorInterface $error) {
protected function addError(SendingErrorInterface $error): AbstractAdapter
{
$this->errors->append($error);
return $this;
}
Expand All @@ -123,7 +124,8 @@ protected function addError(SendingErrorInterface $error) {
*
* @return AbstractAdapter
*/
protected function clearErrors() {
protected function clearErrors(): AbstractAdapter
{
$this->errors = new \ArrayObject();
return $this;
}
Expand Down
11 changes: 7 additions & 4 deletions src/SmsZilla/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,33 @@ interface AdapterInterface{

/**
* Sets options of gateway
*
* @param array $params List of options as associative array name => value
* @return AdapterInterface
*/
public function setParams($params);
public function setParams(array $params): AdapterInterface;

/**
* Returns value gateway param
*
* @param string $name
* @return mixed Value of the $name parameter
*/
public function getParam($name);
public function getParam(string $name);

/**
* Send message with given SMS gateway
*
* @param MessageModel $message
* @param bool $skipErrors If false error during sending message breaks sending others
* @return bool true if success, false if error
*/
public function send(MessageInterface $message, $skipErrors = true);
public function send(MessageInterface $message, bool $skipErrors = true): bool;

/**
* Return error messages.
* Error code depends of selected Adapter
* @return \ArrayObject
*/
public function getErrors();
public function getErrors(): \ArrayObject;
}
4 changes: 3 additions & 1 deletion src/SmsZilla/Adapter/CiscoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ class CiscoAdapter extends AbstractAdapter {

/**
* Send message
*
* @param SmsMessageModel $message
* @return bool
*/
public function send(MessageInterface $message, $skipErrors = true) {
public function send(MessageInterface $message, bool $skipErrors = true): bool
{
$this->clearErrors();

$command = $this->prepareCommand($message->getText());
Expand Down
18 changes: 10 additions & 8 deletions src/SmsZilla/Adapter/ClickatellAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ class ClickatellAdapter extends AbstractAdapter {

/**
* Send message through Clickatell.com gateway
*
* @param SmsMessageModel $message
* @return bool
*/
public function send(MessageInterface $message, $skipErrors = true) {
public function send(MessageInterface $message, bool $skipErrors = true): bool
{

// check clickatell limit
// check Clickatell limit
if (count($message->getRecipients()) > 600) {
$this->addError(new SendingError($message->getRecipients(),
'You have excedded provider limit of messages to send in one time.'
Expand All @@ -50,14 +52,14 @@ public function send(MessageInterface $message, $skipErrors = true) {
$gateway = $this->getClient();

try {
$response = $gateway->sendMessage($message->getRecipients(), $message->getText());
foreach ($response as $datum) {
if ($datum->errorCode) {
$this->addError(new SendingError($datum->destination, $datum->errorCode, $datum->error));
$response = $gateway->sendMessage(['to' => $message->getRecipients(), 'content' => $message->getText()]);
foreach ($response as $msg) {
if ($msg['error']) {
$this->addError(new SendingError($msg['to'], $msg['errorCode'], $msg['error'] . ' Description; ' . $msg['errorDescription']));
}
}
}
catch (Exception $e) {
catch (\Exception $e) {
$this->addError(new SendingError('', $e->getCode(), $e->getMessage()));
if (!$skipErrors) {
throw new \RuntimeException($e->getMessage(), $e->getCode());
Expand All @@ -78,7 +80,7 @@ private function getClient() {
throw new ConfigurationException(__CLASS__ . ' is not configured properly. Please set "token" parameter properly.');
}

return new \Clickatell\Api\ClickatellRest($token);
return new \Clickatell\Rest($token);
}

}
4 changes: 3 additions & 1 deletion src/SmsZilla/Adapter/FileAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ class FileAdapter extends AbstractAdapter {

/**
* Save message in file
*
* @param SmsMessageModel $message
* @return bool
*/
public function send(MessageInterface $message, $skipErrors = true) {
public function send(MessageInterface $message, bool $skipErrors = true): bool
{
$this->clearErrors();

$storePath = $this->getParam('store_path');
Expand Down
35 changes: 19 additions & 16 deletions src/SmsZilla/Adapter/InfobipAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,20 @@ class InfobipAdapter extends AbstractAdapter {

/**
* Send message through infobip.com gateway
* @param SmsMessageModel $error
*
* @param SmsMessageModel $message
* @return bool
*/
public function send(MessageInterface $error, $skipErrors = true) {
public function send(MessageInterface $message, bool $skipErrors = true): bool
{

$sender = $this->getParam('sender');

$request = $this->getRequest();
$body = [
'from' => $sender,
'to' => $error->getRecipients(),
'text' => $error->getText()
'to' => $message->getRecipients(),
'text' => $message->getText()
];

$request->setContent(json_encode($body));
Expand All @@ -77,13 +79,13 @@ public function send(MessageInterface $error, $skipErrors = true) {
$status = $response->getStatusCode();

if ($status !== 200) { // OK code
$error = $this->decodeError($response);
$message = $this->decodeError($response);
}
else {
$this->decodeResponse($response);
}
}
catch (Exception $e) {
catch (\Exception $e) {
$this->addError(new SendingError(json_encode($body), $e->getCode(), $e->getMessage()));
if (!$skipErrors) {
throw new \RuntimeException($e->getMessage(), $e->getCode(), true);
Expand Down Expand Up @@ -133,8 +135,9 @@ private function getRequest() {
* @link https://dev.infobip.com/v1/docs/2fa-status-codes-and-error-details Status codes and error details
* @param Response $response
*/
private function decodeError(Response $response) {
$data = json_decode($response->getContent());
private function decodeError(Response $response): void
{
$data = json_decode($response->getBody(), false);

if (is_object($data)) {
$code = $data->requestError->serviceException->messageId;
Expand All @@ -149,7 +152,7 @@ private function decodeError(Response $response) {
/**
* Decodes response
*
* Response is has JSON structure:
* Response has JSON structure:
* {"messages":[
* {"to":"null",
* "status":{
Expand All @@ -166,11 +169,12 @@ private function decodeError(Response $response) {
* @param Response $response
* @return void
*/
private function decodeResponse($response) {
private function decodeResponse($response): void
{

$data = json_decode($response->getContent());
$data = json_decode($response->getBody(), false);

if (count($data->messages)> 0) {
if (count($data->messages) > 0) {
foreach ($data->messages as $message) {
$this->parseResponseMessage($message);
}
Expand All @@ -182,9 +186,10 @@ private function decodeResponse($response) {
*
* @link https://dev.infobip.com/docs/send-sms-response Description of the response message
* @param Object $msg Message object according to {@link https://dev.infobip.com/docs/send-sms-response}
* @return boolean True if there are no errors
* @return void True if there are no errors
*/
private function parseResponseMessage($msg) {
private function parseResponseMessage($msg): void
{
if (in_array($msg->status->groupId, [
self::STATUS_GROUPS_UNDELIVERABLE, self::STATUS_GROUPS_EXPIRED,
self::STATUS_GROUPS_REJECTED])) {
Expand All @@ -194,8 +199,6 @@ private function parseResponseMessage($msg) {
$msg->status->groupId,
$msg->status->description);
$this->addError(new SendingError($msg->to, $code, $text));
return false;
}
return true;
}
}
7 changes: 5 additions & 2 deletions src/SmsZilla/Adapter/MockAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ class MockAdapter extends AbstractAdapter {

/**
* Stores messages on stack
*
* @param MessageInterface $message
* @param bool $skipErrors
* @return boolean Always true
*/
public function send(MessageInterface $message, $skipErrors = true) {
public function send(MessageInterface $message, bool $skipErrors = true): bool
{
$this->sentMessages[] = $message;
return true;
}

/**
* @return array
*/
public function getSentMessages() {
public function getSentMessages(): array
{
return $this->sentMessages;
}
}
Loading

0 comments on commit 7d179a7

Please sign in to comment.