Skip to content

Commit 4f73a98

Browse files
committed
refactored some constants for outer access and done some cleanup
1 parent 8d46be7 commit 4f73a98

26 files changed

+363
-337
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
<p align='center'>
2-
<img
3-
width="400"
4-
height="79"
5-
src="https://www.sms77.io/wp-content/uploads/2019/07/sms77-Logo-400x79.png" alt="sms77io Logo"
6-
/>
7-
</p>
8-
9-
<h1 align='center'>sms77io PHP API Client</h1>
1+
![](https://www.sms77.io/wp-content/uploads/2019/07/sms77-Logo-400x79.png "Sms77.io Logo")
2+
#sms77io PHP API Client
103

114
## Installation
125

@@ -21,7 +14,16 @@ $client = new Client('MYVERYSECRETAPIKEY1234!?');
2114
$client->sms('+4901234567890', 'HI2U');
2215
```
2316

24-
#### Implemented Endpoints
17+
#### Tests
18+
Some basic tests are implemented. Run them like this:
19+
```shell script
20+
SMS77_API_KEY= SMS77_RECIPIENT= SMS77_MSG_ID= php vendor/bin/phpunit tests/Client
21+
```
22+
Make sure to fill in the values.
23+
SMS77_MSG_ID refers to a message ID sent from this particular API key.
24+
SMS77_RECIPIENT is the recipient of the test SMS.
25+
26+
##### Implemented Endpoints
2527

2628
- sms
2729
- status

src/Client.php

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,45 @@
1010
use Sms77\Api\Validator\ValidateForVoiceValidator;
1111
use Sms77\Api\Validator\VoiceValidator;
1212

13-
class Client
14-
{
13+
class Client {
14+
const BASE_URI = 'https://gateway.sms77.io/api';
1515
/* @var string $apiKey */
1616
private $apiKey;
17-
1817
/* @var string $sendWith */
1918
private $sendWith;
2019

21-
const BASE_URI = 'https://gateway.sms77.io/api';
22-
23-
public function __construct($apiKey, $sendWith = 'php-api')
24-
{
20+
public function __construct($apiKey, $sendWith = 'php-api') {
2521
$this->apiKey = $apiKey;
2622
$this->sendWith = $sendWith;
2723
}
2824

29-
public function balance()
30-
{
25+
public function balance() {
3126
return $this->request('balance', $this->buildOptions([]));
3227
}
3328

34-
public function contacts($action, array $extra = [])
35-
{
29+
private function request($path, $options = []) {
30+
$curl_get_contents = static function($url) {
31+
$ch = curl_init();
32+
curl_setopt($ch, CURLOPT_URL, $url);
33+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
34+
$output = curl_exec($ch);
35+
curl_close($ch);
36+
return $output;
37+
};
38+
39+
return $curl_get_contents(self::BASE_URI . '/' . $path . '?' . http_build_query($options));
40+
}
41+
42+
private function buildOptions(array $required, array $extra = []) {
43+
$required = array_merge($required, [
44+
'p' => $this->apiKey,
45+
'sendwith' => '' === $this->sendWith ? 'unknown' : $this->sendWith,
46+
]);
47+
48+
return array_merge($required, $extra);
49+
}
50+
51+
public function contacts($action, array $extra = []) {
3652
$options = $this->buildOptions([
3753
'action' => $action,
3854
], $extra);
@@ -42,8 +58,7 @@ public function contacts($action, array $extra = [])
4258
return $this->request('contacts', $options);
4359
}
4460

45-
public function lookup($type, $number, array $extra = [])
46-
{
61+
public function lookup($type, $number, array $extra = []) {
4762
$options = $this->buildOptions([
4863
'type' => $type,
4964
'number' => $number,
@@ -54,29 +69,26 @@ public function lookup($type, $number, array $extra = [])
5469
return $this->request('lookup', $options);
5570
}
5671

57-
public function pricing(array $extra = [])
58-
{
72+
public function pricing(array $extra = []) {
5973
$options = $this->buildOptions([], $extra);
6074

6175
(new PricingValidator($options))->validate();
6276

6377
return $this->request('pricing', $options);
6478
}
6579

66-
public function sms($to, $text, array $extra = [])
67-
{
80+
public function sms($to, $text, array $extra = []) {
6881
$options = $this->buildOptions([
6982
'to' => $to,
70-
'text' => $text
83+
'text' => $text,
7184
], $extra);
7285

7386
(new SmsValidator($options))->validate();
7487

7588
return $this->request('sms', $options);
7689
}
7790

78-
public function status($msgId)
79-
{
91+
public function status($msgId) {
8092
$options = $this->buildOptions([
8193
'msg_id' => $msgId,
8294
]);
@@ -86,8 +98,7 @@ public function status($msgId)
8698
return $this->request('status', $options);
8799
}
88100

89-
public function validateForVoice($number, array $extra = [])
90-
{
101+
public function validateForVoice($number, array $extra = []) {
91102
$options = $this->buildOptions([
92103
'number' => $number,
93104
], $extra);
@@ -97,39 +108,14 @@ public function validateForVoice($number, array $extra = [])
97108
return $this->request('validate_for_voice', $options);
98109
}
99110

100-
public function voice($to, $text, array $extra = [])
101-
{
111+
public function voice($to, $text, array $extra = []) {
102112
$options = $this->buildOptions([
103113
'to' => $to,
104-
'text' => $text
114+
'text' => $text,
105115
], $extra);
106116

107117
(new VoiceValidator($options))->validate();
108118

109119
return $this->request('voice', $options);
110120
}
111-
112-
private function buildOptions(array $required, array $extra = [])
113-
{
114-
$required = array_merge($required, [
115-
'p' => $this->apiKey,
116-
'sendwith' => '' === $this->sendWith ? 'unknown' : $this->sendWith
117-
]);
118-
119-
return array_merge($required, $extra);
120-
}
121-
122-
private function request($path, $options = [])
123-
{
124-
$curl_get_contents = static function ($url) {
125-
$ch = curl_init();
126-
curl_setopt($ch, CURLOPT_URL, $url);
127-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
128-
$output = curl_exec($ch);
129-
curl_close($ch);
130-
return $output;
131-
};
132-
133-
return $curl_get_contents(self::BASE_URI . '/' . $path . '?' . http_build_query($options));
134-
}
135121
}

src/Constant/MnpType.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Sms77\Api\Constant;
4+
5+
use Sms77\Api\Reflectable;
6+
7+
class MnpType {
8+
use Reflectable;
9+
10+
const D1 = 'd1';
11+
const D2 = 'd2';
12+
const O2 = 'o2';
13+
const Eplus = 'eplus';
14+
const NotAvailable = 'N/A';
15+
const Int = 'int';
16+
}

src/Constant/NetworkType.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Sms77\Api\Constant;
4+
5+
use Sms77\Api\Reflectable;
6+
7+
class NetworkType {
8+
use Reflectable;
9+
10+
const FixedLine = 'fixed_line';
11+
const FixedLineOrMobile = 'fixed_line_or_mobile';
12+
const Mobile = 'mobile';
13+
const Pager = 'pager';
14+
const PersonalNumber = 'personal_number';
15+
const PremiumRate = 'premium_rate';
16+
const SharedCost = 'shared_cost';
17+
const TollFree = 'toll_free';
18+
const Uan = 'uan';
19+
const Unknown = 'unknown';
20+
const Voicemail = 'voicemail';
21+
const Voip = 'voip';
22+
}

src/Constant/PortingStatus.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Sms77\Api\Constant;
4+
5+
use Sms77\Api\Reflectable;
6+
7+
class PortingStatus {
8+
use Reflectable;
9+
10+
const Unknown = 'unknown';
11+
const Ported = 'ported';
12+
const NotPorted = 'not_ported';
13+
const AssumedNotPorted = 'assumed_not_ported';
14+
const AssumedPorted = 'assumed_ported';
15+
}

src/Constant/ReachableStatus.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Sms77\Api\Constant;
4+
5+
use Sms77\Api\Reflectable;
6+
7+
class ReachableStatus {
8+
use Reflectable;
9+
10+
const Unknown = 'unknown';
11+
const Reachable = 'reachable';
12+
const Undeliverable = 'undeliverable';
13+
const Absent = 'absent';
14+
const BadNumber = 'bad_number';
15+
const Blacklisted = 'blacklisted';
16+
}

src/Constant/RoamingStatus.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Sms77\Api\Constant;
4+
5+
use Sms77\Api\Reflectable;
6+
7+
class RoamingStatus {
8+
use Reflectable;
9+
10+
const Unknown = 'unknown';
11+
const Roaming = 'roaming';
12+
const NotRoaming = 'not_roaming';
13+
}

src/SmsOptions.php renamed to src/Constant/SmsOptions.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?php
22

3-
namespace Sms77\Api;
3+
namespace Sms77\Api\Constant;
4+
5+
use Sms77\Api\Reflectable;
6+
7+
class SmsOptions {
8+
use Reflectable;
49

5-
abstract class SmsOptions
6-
{
710
const Debug = 'debug';
811
const Delay = 'delay';
912
const Details = 'details';

src/Constant/SmsType.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Sms77\Api\Constant;
4+
5+
use Sms77\Api\Reflectable;
6+
7+
class SmsType {
8+
use Reflectable;
9+
10+
const Direct = 'direct';
11+
const Economy = 'economy';
12+
}

src/Constant/StatusCode.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Sms77\Api\Constant;
4+
5+
use Sms77\Api\Reflectable;
6+
7+
class StatusCode {
8+
use Reflectable;
9+
10+
const SmsCarrierTemporarilyUnavailable = 11;
11+
const Sent = 100;
12+
const SentPartially = 101;
13+
const FromIsInvalid = 201;
14+
const ToIsInvalid = 202;
15+
const ToIsMissing = 301;
16+
const TextIsMissing = 305;
17+
const TextIsTooLong = 401;
18+
const ReloadLockPrevention = 402;
19+
const DailyNumberLimitReached = 403;
20+
const NotEnoughCredits = 500;
21+
const CarrierDeliveryFailed = 600;
22+
const UnknownError = 700;
23+
const AuthError = 900;
24+
const HttpApiIsDisabled = 902;
25+
const InvalidServerIp = 903;
26+
}

src/Constant/StatusMessage.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Sms77\Api\Constant;
4+
5+
use Sms77\Api\Reflectable;
6+
7+
class StatusMessage {
8+
use Reflectable;
9+
10+
const Delivered = 'DELIVERED';
11+
const NotDelivered = 'NOTDELIVERED';
12+
const Buffered = 'BUFFERED';
13+
const Transmitted = 'TRANSMITTED';
14+
const Accepted = 'ACCEPTED';
15+
const Expired = 'EXPIRED';
16+
const Rejected = 'REJECTED';
17+
const Failed = 'FAILED';
18+
const Unknown = 'UNKNOWN';
19+
}

0 commit comments

Comments
 (0)