Skip to content

Commit e8e9920

Browse files
author
Franco Dalfovo
committed
Added Credit object
1 parent cdf5959 commit e8e9920

File tree

6 files changed

+83
-32
lines changed

6 files changed

+83
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/vendor
2+
.vscode

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"homepage": "http://github.com/comilio/php-sms-send",
66
"type": "library",
77
"license": "MIT",
8-
"version": "0.1.0",
8+
"version": "0.2.0",
99
"authors": [
1010
{
1111
"name": "Comilio",

examples/get_credits.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
$comilio_username = 'your_username_here'; // Please register on https://www.comilio.it
6+
$comilio_password = 'your_password_here';
7+
8+
$credit = new Comilio\Credit();
9+
$credit->authenticate($comilio_username, $comilio_password);
10+
11+
var_dump($credit->get());

src/ComilioRequest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Comilio;
4+
use \Httpful\Request;
5+
6+
class ComilioRequest
7+
{
8+
protected $username = false,
9+
$password = false;
10+
11+
/**
12+
* Set authentication data
13+
* @return SmsMessage
14+
* @throws Exception
15+
*/
16+
public function authenticate($api_username, $api_password)
17+
{
18+
$this->username = filter_var($api_username, FILTER_SANITIZE_STRING);
19+
$this->password = filter_var($api_password, FILTER_SANITIZE_STRING);
20+
21+
if ($this->username === false) {
22+
throw new Exception("API Username cannot be empty");
23+
}
24+
25+
if ($this->password === false) {
26+
throw new Exception("API Password cannot be empty");
27+
}
28+
29+
return $this;
30+
31+
}
32+
33+
protected static function buildUrl($resource)
34+
{
35+
return 'https://api.comilio.it/rest/v1'.$resource;
36+
}
37+
38+
}

src/Credit.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Comilio;
4+
use \Httpful\Request;
5+
6+
class Credit extends ComilioRequest
7+
{
8+
9+
public function get()
10+
{
11+
$get_request = Request::get(self::buildUrl('/credits'));
12+
$get_request->autoParse(true);
13+
$get_request->authenticateWithBasic($this->username, $this->password);
14+
15+
$result = $get_request->send();
16+
17+
switch ($result->code) {
18+
case 200:
19+
return $result->body;
20+
case 401:
21+
throw new Exception("Authentication failed");
22+
return false;
23+
default:
24+
throw new Exception("Unable to get SMS credits. Gateway response: $result->raw_body");
25+
return false;
26+
}
27+
}
28+
29+
30+
}

src/SmsMessage.php

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
namespace Comilio;
44
use \Httpful\Request;
55

6-
class SmsMessage
6+
class SmsMessage extends ComilioRequest
77
{
88
const SMS_TYPE_CLASSIC = 'Classic';
99
const SMS_TYPE_SMART = 'Smart';
1010
const SMS_TYPE_SMARTPRO = 'SmartPro';
1111

12-
private $username = false,
13-
$password = false,
14-
$type = self::SMS_TYPE_SMART,
12+
private $type = self::SMS_TYPE_SMART,
1513
$sender = null,
1614
$recipients = null,
1715
$sms_id = null,
@@ -22,28 +20,6 @@ public function __construct($sms_id = null)
2220
$this->sms_id = $sms_id;
2321
}
2422

25-
/**
26-
* Set authentication data
27-
* @return SmsMessage
28-
* @throws Exception
29-
*/
30-
public function authenticate($api_username, $api_password)
31-
{
32-
$this->username = filter_var($api_username, FILTER_SANITIZE_STRING);
33-
$this->password = filter_var($api_password, FILTER_SANITIZE_STRING);
34-
35-
if ($this->username === false) {
36-
throw new Exception("API Username cannot be empty");
37-
}
38-
39-
if ($this->password === false) {
40-
throw new Exception("API Password cannot be empty");
41-
}
42-
43-
return $this;
44-
45-
}
46-
4723
/**
4824
* Sets SMS type
4925
* @param string $type message type (one of SmsMessage::SMS_TYPE_CLASSIC, SmsMessage::SMS_TYPE_SMART or SmsMessage::SMS_TYPE_SMARTPRO) defaults to SmsMessage::SMS_TYPE_SMART
@@ -194,11 +170,6 @@ public function getId()
194170
return $this->sms_id;
195171
}
196172

197-
private static function buildUrl($resource)
198-
{
199-
return 'https://api.comilio.it/rest/v1'.$resource;
200-
}
201-
202173
/**
203174
* Check format of a number
204175
* @param string $number Number to be checked

0 commit comments

Comments
 (0)