|
3 | 3 | namespace Sms77\Api;
|
4 | 4 |
|
5 | 5 | use Sms77\Api\Constant\ContactsConstants;
|
| 6 | +use Sms77\Api\Constant\HooksConstants; |
6 | 7 | use Sms77\Api\Exception\InvalidOptionalArgumentException;
|
7 | 8 | use Sms77\Api\Exception\InvalidRequiredArgumentException;
|
8 | 9 | use Sms77\Api\Exception\UnexpectedApiResponseException;
|
|
22 | 23 | use Sms77\Api\Response\Status;
|
23 | 24 | use Sms77\Api\Response\ValidateForVoice;
|
24 | 25 | use Sms77\Api\Response\Voice;
|
| 26 | +use Sms77\Api\Response\WebhookAction; |
| 27 | +use Sms77\Api\Response\Webhooks; |
25 | 28 | use Sms77\Api\Validator\AnalyticsValidator;
|
26 | 29 | use Sms77\Api\Validator\ContactsValidator;
|
| 30 | +use Sms77\Api\Validator\HooksValidator; |
27 | 31 | use Sms77\Api\Validator\LookupValidator;
|
28 | 32 | use Sms77\Api\Validator\PricingValidator;
|
29 | 33 | use Sms77\Api\Validator\SmsValidator;
|
@@ -90,6 +94,59 @@ private function contacts(string $action, array $options = []) {
|
90 | 94 | return $this->$method('contacts', $options);
|
91 | 95 | }
|
92 | 96 |
|
| 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 | + |
93 | 150 | /**
|
94 | 151 | * @param bool $json
|
95 | 152 | * @return string|Contact[]
|
|
0 commit comments