Skip to content

Commit 5bc8d47

Browse files
committed
added endpoint webhooks
1 parent b664fc4 commit 5bc8d47

19 files changed

+353
-20
lines changed

src/BaseClient.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ private function request(string $path, string $method, array $options = []) {
105105
} catch (Exception $e) {
106106
}
107107

108+
var_dump($res);
109+
108110
return $res;
109111
}
110112

src/Client.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Sms77\Api;
44

55
use Sms77\Api\Constant\ContactsConstants;
6+
use Sms77\Api\Constant\HooksConstants;
67
use Sms77\Api\Exception\InvalidOptionalArgumentException;
78
use Sms77\Api\Exception\InvalidRequiredArgumentException;
89
use Sms77\Api\Exception\UnexpectedApiResponseException;
@@ -22,8 +23,11 @@
2223
use Sms77\Api\Response\Status;
2324
use Sms77\Api\Response\ValidateForVoice;
2425
use Sms77\Api\Response\Voice;
26+
use Sms77\Api\Response\WebhookAction;
27+
use Sms77\Api\Response\Webhooks;
2528
use Sms77\Api\Validator\AnalyticsValidator;
2629
use Sms77\Api\Validator\ContactsValidator;
30+
use Sms77\Api\Validator\HooksValidator;
2731
use Sms77\Api\Validator\LookupValidator;
2832
use Sms77\Api\Validator\PricingValidator;
2933
use Sms77\Api\Validator\SmsValidator;
@@ -90,6 +94,59 @@ private function contacts(string $action, array $options = []) {
9094
return $this->$method('contacts', $options);
9195
}
9296

97+
/**
98+
* @param int|null $id
99+
* @param string|null $target_url
100+
* @param string|null $event_type
101+
* @param string|null $request_method
102+
* @return WebhookAction
103+
* @throws InvalidRequiredArgumentException
104+
*/
105+
public function unsubscribeWebhook(
106+
?int $id,
107+
?string $target_url = null,
108+
?string $event_type = null,
109+
?string $request_method = null): WebhookAction {
110+
return new WebhookAction($this->hooks(HooksConstants::ACTION_UNSUBSCRIBE,
111+
compact('id', 'target_url', 'event_type', 'request_method')));
112+
}
113+
114+
/**
115+
* @param string $action
116+
* @param array $options
117+
* @return mixed
118+
* @throws InvalidRequiredArgumentException
119+
*/
120+
private function hooks(string $action, array $options = []) {
121+
$options['action'] = $action;
122+
123+
(new HooksValidator($options))->validate();
124+
125+
$method = HooksConstants::ACTION_READ === $action ? 'get' : 'post';
126+
127+
return $this->$method('hooks', $options);
128+
}
129+
130+
/**
131+
* @param string $target_url
132+
* @param string $event_type
133+
* @param string $request_method
134+
* @return WebhookAction
135+
* @throws InvalidRequiredArgumentException
136+
*/
137+
public function subscribeWebhook(
138+
string $target_url,
139+
string $event_type,
140+
string $request_method = HooksConstants::REQUEST_METHOD_DEFAULT): WebhookAction {
141+
return new WebhookAction($this->hooks(HooksConstants::ACTION_SUBSCRIBE,
142+
compact('target_url', 'event_type', 'request_method')));
143+
}
144+
145+
/** @throws InvalidRequiredArgumentException */
146+
public function getWebhooks(): Webhooks {
147+
return new Webhooks($this->hooks(HooksConstants::ACTION_READ));
148+
}
149+
93150
/**
94151
* @param bool $json
95152
* @return string|Contact[]

src/Constant/HooksConstants.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Sms77\Api\Constant;
4+
5+
class HooksConstants {
6+
public const ACTION_READ = 'read';
7+
8+
public const ACTION_SUBSCRIBE = 'subscribe';
9+
10+
public const ACTION_UNSUBSCRIBE = 'unsubscribe';
11+
12+
public const ACTIONS = [
13+
self::ACTION_READ,
14+
self::ACTION_SUBSCRIBE,
15+
self::ACTION_UNSUBSCRIBE,
16+
];
17+
18+
public const EVENT_TYPE_SMS_INBOUND = 'sms_mo';
19+
20+
public const EVENT_TYPE_SMS_STATUS = 'dlr';
21+
22+
public const EVENT_TYPE_VOICE_STATUS = 'voice_status';
23+
24+
public const EVENT_TYPES = [
25+
self::EVENT_TYPE_SMS_INBOUND,
26+
self::EVENT_TYPE_SMS_STATUS,
27+
self::EVENT_TYPE_VOICE_STATUS,
28+
];
29+
30+
public const REQUEST_METHOD_GET = 'GET';
31+
32+
public const REQUEST_METHOD_POST = 'POST';
33+
34+
public const REQUEST_METHOD_DEFAULT = self::REQUEST_METHOD_POST;
35+
36+
public const REQUEST_METHODS = [
37+
self::REQUEST_METHOD_POST,
38+
self::REQUEST_METHOD_GET,
39+
];
40+
}

src/Library/Util.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use DateTime;
66

77
class Util {
8+
public static function isValidUrl(string $url): bool {
9+
return false !== filter_var($url, FILTER_VALIDATE_URL);
10+
}
11+
812
public static function toArrayOfObject(array $array, string $class): array {
913
foreach ($array as $k => $v) {
1014
$array[$k] = new $class($v);
@@ -18,7 +22,7 @@ public static function toArrayOfObject(array $array, string $class): array {
1822
* @return bool
1923
*/
2024
public static function isUnixTimestamp($timestamp): bool {
21-
/*https://stackoverflow.com/questions/2524680/check-whether-the-string-is-a-unix-timestamp*/
25+
/* THX to Gordon @ https://stackoverflow.com/questions/2524680/check-whether-the-string-is-a-unix-timestamp */
2226
return ((string)$timestamp === $timestamp)
2327
&& ($timestamp <= PHP_INT_MAX)
2428
&& ($timestamp >= ~PHP_INT_MAX);

src/Response/Contact.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
class Contact extends JsonObject {
1313
public function __construct(?object $class = null) {
1414
/** @var self $class */
15-
$class && $class->ID = (int)$class->ID;
15+
if ($class) {
16+
$class->ID = (int)$class->ID;
17+
}
1618

1719
parent::__construct($class);
1820
}

src/Response/ContactEdit.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
class ContactEdit extends JsonObject {
1111
public function __construct(?object $class = null) {
1212
/** @var self $class */
13-
$class && $class->return = (int)$class->return;
13+
if ($class) {
14+
$class->return = (int)$class->return;
15+
}
1416

1517
parent::__construct($class);
1618
}

src/Response/LookupCnam.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
class LookupCnam extends JsonObject {
1414
public function __construct(?object $class = null) {
1515
/** @var self $class */
16-
$class && $class->success = (bool)$class->success;
16+
if ($class) {
17+
$class->success = (bool)$class->success;
18+
}
1719

1820
parent::__construct($class);
1921
}

src/Response/LookupMnp.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
class LookupMnp extends JsonObject {
1414
public function __construct(?object $class = null) {
1515
/** @var self $class */
16-
$class && $class->mnp = new Mnp($class->mnp);
16+
if ($class) {
17+
$class->mnp = new Mnp($class->mnp);
18+
}
1719

1820
parent::__construct($class);
1921
}

src/Response/SmsMessage.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
class SmsMessage extends JsonObject {
2121
public function __construct(?object $class = null) {
2222
/** @var self $class */
23-
$class && $class->id = (int)$class->id;
23+
if ($class) {
24+
$class->id = (int)$class->id;
25+
}
2426

2527
parent::__construct($class);
2628
}

src/Response/ValidateForVoice.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
class ValidateForVoice extends JsonObject {
1717
public function __construct(?object $class = null) {
1818
/** @var self $class */
19-
if ($class) {
20-
if (property_exists($class, 'code')) {
21-
$class->code = (int)$class->code;
22-
}
19+
if ($class && property_exists($class, 'code')) {
20+
$class->code = (int)$class->code;
2321
}
2422

2523
parent::__construct($class);

0 commit comments

Comments
 (0)