Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Buzz with Guzzle #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ sudo: false
language: php

php:
- 5.4
- 5.5
- 5.6
- hhvm
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Xi SMS
=======

SMS library for PHP 5.3+.
SMS library for PHP 5.5+.

Abstracts away the gateways. Just send'n go!

Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "xi/sms",
"type": "library",
"description": "Short messaging for PHP 5.3",
"description": "Short messaging for PHP 5.5",
"keywords": ["SMS"],
"homepage": "http://github.com/xi-project/xi-sms",
"license": "BSD",
Expand All @@ -14,8 +14,8 @@
}
],
"require": {
"php": ">=5.4.0",
"kriswallsmith/buzz": "~0.7",
"php": ">=5.5.0",
"guzzlehttp/guzzle": "~6.0",
"symfony/event-dispatcher": "~2.0|~3.0",
"messagebird/php-rest-api": "~1.1"
},
Expand All @@ -25,7 +25,8 @@
},
"autoload": {
"psr-4": {
"Xi\\Sms\\": "library/Xi/Sms/"
"Xi\\Sms\\": "library/Xi/Sms/",
"Xi\\Sms\\Tests\\": "tests/Xi/Sms/Tests/"
}
},
"minimum-stability": "stable"
Expand Down
10 changes: 5 additions & 5 deletions library/Xi/Sms/Filter/NumberLimitingFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ class NumberLimitingFilter implements FilterInterface
/**
* @var array
*/
private $whitelisted = array();
private $whitelisted = [];

/**
* @var array
*/
private $blacklisted = array();
private $blacklisted = [];

/**
* @param array $whitelisted An array of whitelist regexes
* @param array $blacklisted An array of blacklist regexes
*/
public function __construct($whitelisted = array(), $blacklisted = array())
public function __construct(array $whitelisted = [], array $blacklisted = [])
{
$this->whitelisted = $whitelisted;
$this->blacklisted = $blacklisted;
Expand All @@ -44,11 +44,11 @@ public function accept(SmsMessage $message)
$to = $message->getTo();

if ($this->whitelisted) {
$to = array_filter($to, array($this, 'handleWhitelisted'));
$to = array_filter($to, [$this, 'handleWhitelisted']);
}

if ($this->blacklisted) {
$to = array_filter($to, array($this, 'handleBlacklisted'));
$to = array_filter($to, [$this, 'handleBlacklisted']);
}
return (bool) $to;
}
Expand Down
19 changes: 11 additions & 8 deletions library/Xi/Sms/Gateway/BaseHttpRequestGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,37 @@

namespace Xi\Sms\Gateway;

use Buzz\Browser;
use Buzz\Client\Curl;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\RequestOptions;

/**
* Convenience class for http sending gateways
*/
abstract class BaseHttpRequestGateway Implements GatewayInterface
{
/**
* @var Browser
* @var ClientInterface
*/
private $client;

/**
* @param Browser $browser
* @param ClientInterface $client
*/
public function setClient(Browser $browser)
public function setClient(ClientInterface $client)
{
$this->client = $browser;
$this->client = $client;
}

/**
* @return Browser
* @return ClientInterface
*/
public function getClient()
{
if (!$this->client) {
$this->client = new Browser(new Curl());
$this->client = new Client([
RequestOptions::HTTP_ERRORS => false
]);
}
return $this->client;
}
Expand Down
2 changes: 1 addition & 1 deletion library/Xi/Sms/Gateway/ClickatellGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function send(SmsMessage $message)
foreach ($message->getTo() as $to) {
$url = "{$this->endpoint}/http/sendmsg?api_id={$this->apiKey}&user={$this->user}" .
"&password={$this->password}&to={$to}&text={$body}&from={$from}";
$this->getClient()->post($url, array());
$this->getClient()->post($url);
}
return true;
}
Expand Down
80 changes: 29 additions & 51 deletions library/Xi/Sms/Gateway/InfobipGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

namespace Xi\Sms\Gateway;

use GuzzleHttp\RequestOptions;
use Xi\Sms\SmsMessage;
use XMLWriter;

/**
* Infobip gateway
*/
class InfobipGateway extends BaseHttpRequestGateway
{

/**
* @var string
*/
Expand All @@ -30,73 +31,50 @@ class InfobipGateway extends BaseHttpRequestGateway
/**
* @var string
*/
private $endpoint;
private $endpoint = 'https://api.infobip.com';

public function __construct(
$user,
$password,
$endpoint = 'https://api2.infobip.com/api'
$password
) {
$this->user = $user;
$this->password = $password;
$this->endpoint = $endpoint;
}

/**
* @see GatewayInterface::send
*/
public function send(SmsMessage $message)
{
$writer = new XMLWriter();

$writer->openMemory();
$writer->startDocument('1.0', 'UTF-8');

$writer->startElement('SMS');

$writer->startElement('authentication');

$writer->startElement('username');
$writer->text($this->user);
$writer->endElement();

$writer->startElement('password');
$writer->text($this->password);
$writer->endElement();

$writer->endElement();

$writer->startElement('message');
$headers = $this->createAuthorizationHeaders();

$writer->startElement('sender');
$writer->text($message->getFrom());
$writer->endElement();
$data = [
'from' => $message->getFrom(),
'to' => $message->getTo(),
'text' => $message->getBody()
];

$writer->startElement('datacoding');
$writer->text('3');
$writer->endElement();
$endpoint = $this->endpoint . '/sms/1/text/single';

$writer->startElement('text');
$writer->text(utf8_decode($message->getBody()));
$writer->endElement();
$response = $this->getClient()->post(
$endpoint,
[
RequestOptions::HEADERS => $headers,
RequestOptions::JSON => $data
]
);

$writer->endElement();

$writer->startElement('recipients');
foreach ($message->getTo() as $to) {
$writer->startElement('gsm');
$writer->text($to);
$writer->endElement();
}
$writer->endElement();

$writer->endElement();
$writer->endDocument();

$requestBody = 'XML=' . preg_replace('/<\?xml.*\?>\n?/', '', $writer->outputMemory());

$this->getClient()->post($this->endpoint . '/v3/sendsms/xml', array(), $requestBody);
return ($response->getStatusCode() == 200);
}

return true;
/**
* @todo Infobip recommends API key authentication
* @return array
*/
private function createAuthorizationHeaders()
{
return [
'Authorization' => 'Basic ' . base64_encode($this->user . ':' . $this->password)
];
}
}
12 changes: 6 additions & 6 deletions library/Xi/Sms/Gateway/IpxGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function getClient()
//create soap client while setting response timeout to 10 minutes
$this->client = new \SoapClient(
$this->wsdlUrl,
array('default_socket_timeout' => $this->timeout)
['default_socket_timeout' => $this->timeout]
);
}

Expand All @@ -120,7 +120,7 @@ protected function getClient()
*
* @return array
*/
protected function getParams($from, $to, $body, $params = array())
protected function getParams($from, $to, $body, array $params = [])
{
$correlationId = microtime(true);
$originatingAddress = $from; //If MSISDN
Expand All @@ -134,7 +134,7 @@ protected function getParams($from, $to, $body, $params = array())
}

return array_merge(
array(
[
'correlationId' => $correlationId,
'originatingAddress' => $originatingAddress,
'originatorTON' => $originatorTON,
Expand All @@ -156,7 +156,7 @@ protected function getParams($from, $to, $body, $params = array())
'campaignName' => '#NULL#',
'username' => $this->username,
'password' => $this->password
),
],
$params
);
}
Expand Down Expand Up @@ -194,12 +194,12 @@ protected function sendMessage($from, $to, $body)
foreach($parts as $index => $body) {
$params = array('userDataHeader' => sprintf('0500030F%02d%02d', $messageCount, $index+1));
$params = $this->getParams($from, $to, $body, $params);
$result = $client->__soapCall('send', array('request' => $params));
$result = $client->__soapCall('send', ['request' => $params]);
}
}
else {
$params = $this->getParams($from, $to, $body);
$result = $client->__soapCall('send', array('request' => $params));
$result = $client->__soapCall('send', ['request' => $params]);
}


Expand Down
2 changes: 1 addition & 1 deletion library/Xi/Sms/Gateway/Legacy/MessageBirdGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function send(SmsMessage $message)
'&recipients=' . urlencode($to) .
'&type=' . urlencode($this->type) .
'&message=' . $body;
$ret = $this->getClient()->post($url, array());
$ret = $this->getClient()->post($url);
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion library/Xi/Sms/Gateway/MockGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MockGateway implements GatewayInterface
/**
* @var array
*/
private $sentMessages = array();
private $sentMessages = [];

public function send(SmsMessage $message)
{
Expand Down
Loading