Skip to content

Commit

Permalink
added endpoint webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiez committed Nov 4, 2020
1 parent b664fc4 commit 5bc8d47
Show file tree
Hide file tree
Showing 19 changed files with 353 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ private function request(string $path, string $method, array $options = []) {
} catch (Exception $e) {
}

var_dump($res);

return $res;
}

Expand Down
57 changes: 57 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sms77\Api;

use Sms77\Api\Constant\ContactsConstants;
use Sms77\Api\Constant\HooksConstants;
use Sms77\Api\Exception\InvalidOptionalArgumentException;
use Sms77\Api\Exception\InvalidRequiredArgumentException;
use Sms77\Api\Exception\UnexpectedApiResponseException;
Expand All @@ -22,8 +23,11 @@
use Sms77\Api\Response\Status;
use Sms77\Api\Response\ValidateForVoice;
use Sms77\Api\Response\Voice;
use Sms77\Api\Response\WebhookAction;
use Sms77\Api\Response\Webhooks;
use Sms77\Api\Validator\AnalyticsValidator;
use Sms77\Api\Validator\ContactsValidator;
use Sms77\Api\Validator\HooksValidator;
use Sms77\Api\Validator\LookupValidator;
use Sms77\Api\Validator\PricingValidator;
use Sms77\Api\Validator\SmsValidator;
Expand Down Expand Up @@ -90,6 +94,59 @@ private function contacts(string $action, array $options = []) {
return $this->$method('contacts', $options);
}

/**
* @param int|null $id
* @param string|null $target_url
* @param string|null $event_type
* @param string|null $request_method
* @return WebhookAction
* @throws InvalidRequiredArgumentException
*/
public function unsubscribeWebhook(
?int $id,
?string $target_url = null,
?string $event_type = null,
?string $request_method = null): WebhookAction {
return new WebhookAction($this->hooks(HooksConstants::ACTION_UNSUBSCRIBE,
compact('id', 'target_url', 'event_type', 'request_method')));
}

/**
* @param string $action
* @param array $options
* @return mixed
* @throws InvalidRequiredArgumentException
*/
private function hooks(string $action, array $options = []) {
$options['action'] = $action;

(new HooksValidator($options))->validate();

$method = HooksConstants::ACTION_READ === $action ? 'get' : 'post';

return $this->$method('hooks', $options);
}

/**
* @param string $target_url
* @param string $event_type
* @param string $request_method
* @return WebhookAction
* @throws InvalidRequiredArgumentException
*/
public function subscribeWebhook(
string $target_url,
string $event_type,
string $request_method = HooksConstants::REQUEST_METHOD_DEFAULT): WebhookAction {
return new WebhookAction($this->hooks(HooksConstants::ACTION_SUBSCRIBE,
compact('target_url', 'event_type', 'request_method')));
}

/** @throws InvalidRequiredArgumentException */
public function getWebhooks(): Webhooks {
return new Webhooks($this->hooks(HooksConstants::ACTION_READ));
}

/**
* @param bool $json
* @return string|Contact[]
Expand Down
40 changes: 40 additions & 0 deletions src/Constant/HooksConstants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace Sms77\Api\Constant;

class HooksConstants {
public const ACTION_READ = 'read';

public const ACTION_SUBSCRIBE = 'subscribe';

public const ACTION_UNSUBSCRIBE = 'unsubscribe';

public const ACTIONS = [
self::ACTION_READ,
self::ACTION_SUBSCRIBE,
self::ACTION_UNSUBSCRIBE,
];

public const EVENT_TYPE_SMS_INBOUND = 'sms_mo';

public const EVENT_TYPE_SMS_STATUS = 'dlr';

public const EVENT_TYPE_VOICE_STATUS = 'voice_status';

public const EVENT_TYPES = [
self::EVENT_TYPE_SMS_INBOUND,
self::EVENT_TYPE_SMS_STATUS,
self::EVENT_TYPE_VOICE_STATUS,
];

public const REQUEST_METHOD_GET = 'GET';

public const REQUEST_METHOD_POST = 'POST';

public const REQUEST_METHOD_DEFAULT = self::REQUEST_METHOD_POST;

public const REQUEST_METHODS = [
self::REQUEST_METHOD_POST,
self::REQUEST_METHOD_GET,
];
}
6 changes: 5 additions & 1 deletion src/Library/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use DateTime;

class Util {
public static function isValidUrl(string $url): bool {
return false !== filter_var($url, FILTER_VALIDATE_URL);
}

public static function toArrayOfObject(array $array, string $class): array {
foreach ($array as $k => $v) {
$array[$k] = new $class($v);
Expand All @@ -18,7 +22,7 @@ public static function toArrayOfObject(array $array, string $class): array {
* @return bool
*/
public static function isUnixTimestamp($timestamp): bool {
/*https://stackoverflow.com/questions/2524680/check-whether-the-string-is-a-unix-timestamp*/
/* THX to Gordon @ https://stackoverflow.com/questions/2524680/check-whether-the-string-is-a-unix-timestamp */
return ((string)$timestamp === $timestamp)
&& ($timestamp <= PHP_INT_MAX)
&& ($timestamp >= ~PHP_INT_MAX);
Expand Down
4 changes: 3 additions & 1 deletion src/Response/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
class Contact extends JsonObject {
public function __construct(?object $class = null) {
/** @var self $class */
$class && $class->ID = (int)$class->ID;
if ($class) {
$class->ID = (int)$class->ID;
}

parent::__construct($class);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Response/ContactEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
class ContactEdit extends JsonObject {
public function __construct(?object $class = null) {
/** @var self $class */
$class && $class->return = (int)$class->return;
if ($class) {
$class->return = (int)$class->return;
}

parent::__construct($class);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Response/LookupCnam.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
class LookupCnam extends JsonObject {
public function __construct(?object $class = null) {
/** @var self $class */
$class && $class->success = (bool)$class->success;
if ($class) {
$class->success = (bool)$class->success;
}

parent::__construct($class);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Response/LookupMnp.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
class LookupMnp extends JsonObject {
public function __construct(?object $class = null) {
/** @var self $class */
$class && $class->mnp = new Mnp($class->mnp);
if ($class) {
$class->mnp = new Mnp($class->mnp);
}

parent::__construct($class);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Response/SmsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
class SmsMessage extends JsonObject {
public function __construct(?object $class = null) {
/** @var self $class */
$class && $class->id = (int)$class->id;
if ($class) {
$class->id = (int)$class->id;
}

parent::__construct($class);
}
Expand Down
6 changes: 2 additions & 4 deletions src/Response/ValidateForVoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
class ValidateForVoice extends JsonObject {
public function __construct(?object $class = null) {
/** @var self $class */
if ($class) {
if (property_exists($class, 'code')) {
$class->code = (int)$class->code;
}
if ($class && property_exists($class, 'code')) {
$class->code = (int)$class->code;
}

parent::__construct($class);
Expand Down
23 changes: 23 additions & 0 deletions src/Response/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Sms77\Api\Response;

use Sms77\Api\Library\JsonObject;

/**
* @property int id
* @property string target_url
* @property string event_type
* @property string request_method
* @property string created
*/
class Webhook extends JsonObject {
public function __construct(?object $class = null) {
/** @var self $class */
if ($class) {
$class->id = (int)$class->id;
}

parent::__construct($class);
}
}
12 changes: 12 additions & 0 deletions src/Response/WebhookAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace Sms77\Api\Response;

use Sms77\Api\Library\JsonObject;

/**
* @property int id
* @property bool success
*/
class WebhookAction extends JsonObject {
}
22 changes: 22 additions & 0 deletions src/Response/Webhooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace Sms77\Api\Response;

use Sms77\Api\Library\JsonObject;

/**
* @property bool success
* @property Webhook[] hooks
*/
class Webhooks extends JsonObject {
public function __construct(?object $class = null) {
/** @var self $class */
if ($class) {
foreach ($class->hooks as $k => $v) {
$class->hooks[$k] = new Webhook($v);
}
}

parent::__construct($class);
}
}
10 changes: 3 additions & 7 deletions src/Validator/BaseValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,18 @@ public function __construct(array $parameters = []) {
$this->parameters = $parameters;
}

public function isValidUnixTimestamp(string $timestamp): bool {
return Util::isUnixTimestamp($timestamp);
}

public function isValidDate(string $date): bool {
return Util::isValidDate($date, 'Y-m-d');
}

protected function isValidBool($string): bool {
public function isValidBool($string): bool {
$string = (int)$string;

return 1 === $string || 0 === $string;
}

/** @throws InvalidOptionalArgumentException */
protected function throwOnOptionalBadType(): void {
public function throwOnOptionalBadType(): void {
$smsTypes = SmsType::values();

$type = $this->fallback('type');
Expand All @@ -47,7 +43,7 @@ protected function throwOnOptionalBadType(): void {
* @param mixed $fallback
* @return mixed
*/
protected function fallback(string $key, $fallback = null) {
public function fallback(string $key, $fallback = null) {
return $this->parameters[$key] ?? $fallback;
}
}
Loading

0 comments on commit 5bc8d47

Please sign in to comment.