Skip to content

Commit

Permalink
misc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
anlutro committed Apr 26, 2015
1 parent b929f15 commit 35c8a55
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 127 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013 Andreas Lutro
Copyright (c) 2013-2015 Andreas Lutro

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
{
"name": "Andreas Lutro",
"email": "[email protected]"
},
{
"name": "Michael Nowag",
"email": "<[email protected]>"
}
],
"require": {
Expand Down
83 changes: 44 additions & 39 deletions src/anlutro/BulkSms/BulkSmsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
*/
class BulkSmsService
{
public static $TEST_ALWAYS_SUCCEED = 1;
public static $TEST_ALWAYS_FAIL = 2;
const TEST_ALWAYS_SUCCEED = 1;
const TEST_ALWAYS_FAIL = 2;

/**
* Meaning of response status codes.
*
*
* @var array
*/
protected static $statusMessages = array(
Expand All @@ -42,35 +41,43 @@ class BulkSmsService
40 => 'Temporarily unavailable',
201 => 'Maximum batch size exceeded',
);
protected $test_mode = false;

/**
* Whether test mode is enabled.
*
* @var boolean
*/
protected $testMode = false;

/**
* BulkSMS username
*
* @var string
*/
protected $username;

/**
* BulkSMS password
*
* @var string
*/
protected $password;

/**
* @var null
* @var string
*/
protected $baseurl;
protected $baseUrl;

/**
* @param string $username BulkSMS username
* @param string $password BulkSMS password
* @param string $baseurl
* @param anlutro\cURL\cURL $curl (optional) If you have an existing
* instance of my cURL wrapper, you can pass it.
* @param string $username BulkSMS username
* @param string $password BulkSMS password
* @param string $baseUrl Optional - defaults to "http://bulksms.vsms.net:5567"
* @param cURL $curl Optional - a new instance will be constructed if null is passed.
*/
public function __construct($username, $password, $baseurl = "http://bulksms.vsms.net:5567", $curl = null)
public function __construct($username, $password, $baseUrl = "http://bulksms.vsms.net:5567", $curl = null)
{
v::url()->setName("Base Bulksms URL")->check($baseurl);
$this->baseurl = $baseurl;
v::url()->setName("Base Bulksms URL")->check($baseUrl);
$this->baseUrl = $baseUrl;
$this->username = $username;
$this->password = $password;
$this->curl = $curl ?: new cURL();
Expand All @@ -83,16 +90,13 @@ public function __construct($username, $password, $baseurl = "http://bulksms.vsm
*/
public function setTestMode($mode)
{
if (BulkSmsService::$TEST_ALWAYS_SUCCEED == $mode) {
$this->test_mode = BulkSmsService::$TEST_ALWAYS_SUCCEED;

return;
} elseif (BulkSmsService::$TEST_ALWAYS_FAIL == $mode) {
$this->test_mode = BulkSmsService::$TEST_ALWAYS_FAIL;

return;
if (BulkSmsService::TESTALWAYS_SUCCEED == $mode) {
$this->testMode = BulkSmsService::TESTALWAYS_SUCCEED;
} elseif (BulkSmsService::TESTALWAYS_FAIL == $mode) {
$this->testMode = BulkSmsService::TESTALWAYS_FAIL;
} else {
throw new \InvalidArgumentException("Invalid test mode: " . $mode);
}
throw new \InvalidArgumentException("Invalid test mode: " . $mode);
}

/**
Expand All @@ -110,7 +114,7 @@ public function sendMessage($recipient, $message)
$msg = $this->createMessage($recipient, $message);

$sender->setMessage($msg);
$response = $sender->send($this->test_mode);
$response = $sender->send($this->testMode);
$this->validateResponse($response);

return $sender->extractResponse($response);
Expand All @@ -123,7 +127,7 @@ public function sendMessage($recipient, $message)
*/
protected function createMessageSender()
{
return new Sender\Single($this->username, $this->password, $this->baseurl, $this->curl);
return new Sender\Single($this->username, $this->password, $this->baseUrl, $this->curl);
}

/**
Expand Down Expand Up @@ -155,22 +159,22 @@ public function validateResponse($response)

$parts = explode('|', $response->body);

if (!is_numeric($parts[ 0 ])) {
if (!is_numeric($parts[0])) {
throw new \UnexpectedValueException(
'Unknown response code: ' . $parts[ 0 ] . ' - full response: ' . $response->body
'Unknown response code: ' . $parts[0] . ' - full response: ' . $response->body
);
}

$code = (int) $parts[ 0 ];
$code = (int) $parts[0];

if ($code === 0 || $code === 1) {
return true;
} else {
$message = array_key_exists($code, static::$statusMessages)
? static::$statusMessages[ $code ]
: $parts[ 1 ];
throw new BulkSmsException('BulkSMS API responded with code: ' . $code . ' - ' . $message);
}

$message = array_key_exists($code, static::$statusMessages)
? static::$statusMessages[$code]
: $parts[1];
throw new BulkSmsException('BulkSMS API responded with code: ' . $code . ' - ' . $message);
}

/**
Expand All @@ -190,7 +194,8 @@ public function sendBulkMessages(array $messages)
v::instance('anlutro\BulkSms\Message')->check($message);
$sender->addMessage($message);
}
$response = $sender->send($this->test_mode);

$response = $sender->send($this->testMode);
$this->validateResponse($response);

return $sender->extractResponse($response);
Expand All @@ -203,7 +208,7 @@ public function sendBulkMessages(array $messages)
*/
protected function createBulkSender()
{
return new Sender\Bulk($this->username, $this->password, $this->baseurl, $this->curl);
return new Sender\Bulk($this->username, $this->password, $this->baseUrl, $this->curl);
}

/**
Expand All @@ -216,7 +221,7 @@ protected function createBulkSender()
public function getStatusForBatchId($bulksmsid)
{
$sender = $this->createBulkStatusSender();
$response = $sender->getStatusForBatchId($bulksmsid, $this->test_mode);
$response = $sender->getStatusForBatchId($bulksmsid, $this->testMode);
$this->validateResponse($response);

return $sender->extractResponse($response);
Expand All @@ -229,6 +234,6 @@ public function getStatusForBatchId($bulksmsid)
*/
protected function createBulkStatusSender()
{
return new Sender\Status($this->username, $this->password, $this->baseurl, $this->curl);
return new Sender\Status($this->username, $this->password, $this->baseUrl, $this->curl);
}
}
12 changes: 6 additions & 6 deletions src/anlutro/BulkSms/Laravel/BulkSmsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class BulkSmsServiceProvider extends ServiceProvider
*/
public function register()
{
$this->app[ 'bulksms' ] = $this->app->share(
$this->app['bulksms'] = $this->app->share(
function ($app) {
$username = $app[ 'config' ]->get('bulk-sms::username');
$password = $app[ 'config' ]->get('bulk-sms::password');
$baseurl = $app[ 'config' ]->get('bulk-sms::baseurl');
$username = $app['config']->get('bulk-sms::username');
$password = $app['config']->get('bulk-sms::password');
$baseurl = $app['config']->get('bulk-sms::baseurl');

if (isset($app[ 'curl' ])) {
return new BulkSmsService($username, $password, $baseurl, $app[ 'curl' ]);
if (isset($app['curl'])) {
return new BulkSmsService($username, $password, $baseurl, $app['curl']);
} else {
return new BulkSmsService($username, $password, $baseurl, null);
}
Expand Down
62 changes: 0 additions & 62 deletions src/anlutro/BulkSms/Sender/ASender.php

This file was deleted.

86 changes: 86 additions & 0 deletions src/anlutro/BulkSms/Sender/AbstractSender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* BulkSMS PHP implementation
*
* @author Andreas Lutro <[email protected]>
* @license http://opensource.org/licenses/MIT
* @package anlutro/bulk-sms
*/

namespace anlutro\BulkSms\Sender;

use anlutro\cURL\cURL;
use anlutro\cURL\Response;
use Respect\Validation\Validator as v;

abstract class AbstractSender
{
/**
* BulkSMS username
*
* @var string
*/
protected $username;

/**
* BulkSMS password
*
* @var string
*/
protected $password;

/**
* The endpoint the call should go to.
*
* @var string
*/
protected $endpoint;

/**
* The base URL of the API.
*
* @var string
*/
protected $baseUrl;

/**
* The cURL instance.
*
* @var cURL
*/
protected $curl;

/**
* @param string $username BulkSMS username
* @param string $password BulkSMS password
* @param $baseUrl
* @param cURL $curl
*/
public function __construct($username, $password, $baseUrl, cURL $curl = null)
{
v::url()->setName("Base Bulksms URL")->check($baseUrl);
$this->baseUrl = $baseUrl;
$this->username = $username;
$this->password = $password;
$this->curl = $curl ?: new cURL();
}

/**
* Extract response from Sender - depends on sender
*
* @param Response $response
*
* @return mixed
*/
abstract public function extractResponse(Response $response);

/**
* Get the full URL for the request.
*
* @return string
*/
protected function getUrl()
{
return $this->baseUrl . $this->endpoint;
}
}
Loading

0 comments on commit 35c8a55

Please sign in to comment.