├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Curl ├── Discovery │ ├── CurlDiscovery.php │ └── GuzzleHttpHelpersDiscovery.php ├── Exception │ ├── ClientException.php │ ├── NetworkException.php │ └── RequestException.php ├── HttpClient.php ├── HttpHeadersParser.php ├── RequestFactory.php ├── SmsapiHttpClient.php └── StreamFactory.php ├── Feature ├── Bag │ ├── PaginationBag.php │ └── SortBag.php ├── Blacklist │ ├── Bag │ │ ├── CreateBlacklistedPhoneNumberBag.php │ │ ├── DeleteBlacklistedPhoneNumberBag.php │ │ └── FindBlacklistedPhoneNumbersBag.php │ ├── BlacklistFeature.php │ ├── BlacklistHttpFeature.php │ └── Data │ │ ├── BlacklistedPhoneNumber.php │ │ └── BlacklistedPhoneNumberFactory.php ├── Contacts │ ├── Bag │ │ ├── CreateContactBag.php │ │ ├── DeleteContactBag.php │ │ ├── FindContactBag.php │ │ ├── FindContactsBag.php │ │ └── UpdateContactBag.php │ ├── ContactsFeature.php │ ├── ContactsHttpFeature.php │ ├── Data │ │ ├── Contact.php │ │ ├── ContactCustomField.php │ │ ├── ContactCustomFieldFactory.php │ │ ├── ContactFactory.php │ │ ├── ContactGroup.php │ │ └── ContactGroupFactory.php │ ├── Fields │ │ ├── Bag │ │ │ ├── CreateContactFieldBag.php │ │ │ ├── DeleteContactFieldBag.php │ │ │ ├── FindContactFieldOptionsBag.php │ │ │ └── UpdateContactFieldBag.php │ │ ├── ContactsFieldsFeature.php │ │ ├── ContactsFieldsHttpFeature.php │ │ └── Data │ │ │ ├── ContactField.php │ │ │ ├── ContactFieldFactory.php │ │ │ ├── ContactFieldOption.php │ │ │ └── ContactFieldOptionFactory.php │ └── Groups │ │ ├── Bag │ │ ├── AssignContactToGroupBag.php │ │ ├── CreateGroupBag.php │ │ ├── DeleteGroupBag.php │ │ ├── FindContactGroupBag.php │ │ ├── FindContactGroupsBag.php │ │ ├── FindGroupBag.php │ │ ├── UnpinContactFromGroupBag.php │ │ └── UpdateGroupBag.php │ │ ├── ContactsGroupsFeature.php │ │ ├── ContactsGroupsHttpFeature.php │ │ ├── Members │ │ ├── Bag │ │ │ ├── AddContactToGroupByQueryBag.php │ │ │ ├── FindContactInGroupBag.php │ │ │ ├── MoveContactToGroupByQueryBag.php │ │ │ ├── PinContactToGroupBag.php │ │ │ ├── UnpinContactFromGroupBag.php │ │ │ └── UnpinContactFromGroupByQueryBag.php │ │ ├── ContactsGroupsMembersFeature.php │ │ └── ContactsGroupsMembersHttpFeature.php │ │ └── Permissions │ │ ├── Bag │ │ ├── CreateGroupPermissionBag.php │ │ ├── DeleteGroupPermissionBag.php │ │ ├── FindGroupPermissionBag.php │ │ ├── FindGroupPermissionsBag.php │ │ └── UpdateGroupPermissionBag.php │ │ ├── ContactsGroupsPermissionsFeature.php │ │ ├── ContactsGroupsPermissionsHttpFeature.php │ │ └── Data │ │ ├── GroupPermission.php │ │ └── GroupPermissionFactory.php ├── Data │ ├── DataFactoryProvider.php │ ├── Points.php │ └── PointsFactory.php ├── Hlr │ ├── Bag │ │ └── SendHlrBag.php │ ├── Data │ │ ├── Hlr.php │ │ └── HlrFactory.php │ ├── HlrFeature.php │ └── HlrHttpFeature.php ├── Mfa │ ├── Bag │ │ ├── CreateMfaBag.php │ │ └── VerificationMfaBag.php │ ├── Data │ │ ├── Mfa.php │ │ └── MfaFactory.php │ ├── MfaFeature.php │ └── MfaHttpFeature.php ├── Mms │ ├── Bag │ │ └── SendMmsBag.php │ ├── Data │ │ ├── Mms.php │ │ └── MmsFactory.php │ ├── MmsFeature.php │ └── MmsHttpFeature.php ├── Ping │ ├── Data │ │ ├── Ping.php │ │ └── PingFactory.php │ ├── PingFeature.php │ └── PingHttpFeature.php ├── Profile │ ├── Data │ │ ├── Money.php │ │ ├── MoneyFactory.php │ │ ├── Profile.php │ │ ├── ProfileFactory.php │ │ ├── ProfilePrice.php │ │ ├── ProfilePriceCountry.php │ │ ├── ProfilePriceCountryFactory.php │ │ ├── ProfilePriceFactory.php │ │ ├── ProfilePriceNetwork.php │ │ └── ProfilePriceNetworkFactory.php │ ├── ProfileDefaultHttpFeatures.php │ ├── ProfileFeature.php │ ├── ProfileHttpFeature.php │ ├── SmsapiPlProfileFeature.php │ └── SmsapiPlProfileHttpFeature.php ├── ShortUrl │ ├── Bag │ │ └── CreateShortUrlLinkBag.php │ ├── Data │ │ ├── ShortUrlLink.php │ │ └── ShortUrlLinkFactory.php │ ├── ShortUlrHttpFeature.php │ └── ShortUrlFeature.php ├── Sms │ ├── Bag │ │ ├── DeleteScheduledSmssBag.php │ │ ├── ScheduleSmsBag.php │ │ ├── ScheduleSmsToGroupBag.php │ │ ├── ScheduleSmssBag.php │ │ ├── SendSmsBag.php │ │ ├── SendSmsToGroupBag.php │ │ └── SendSmssBag.php │ ├── Data │ │ ├── Sms.php │ │ ├── SmsContent.php │ │ ├── SmsContentFactory.php │ │ └── SmsFactory.php │ ├── Sendernames │ │ ├── Bag │ │ │ ├── CreateSendernameBag.php │ │ │ ├── DeleteSendernameBag.php │ │ │ ├── FindSendernameBag.php │ │ │ └── MakeSendernameDefaultBag.php │ │ ├── Data │ │ │ ├── Sendername.php │ │ │ └── SendernameFactory.php │ │ ├── SendernamesFeature.php │ │ └── SendernamesHttpFeature.php │ ├── SmsFeature.php │ └── SmsHttpFeature.php ├── Subusers │ ├── Bag │ │ ├── CreateSubuserBag.php │ │ ├── DeleteSubuserBag.php │ │ └── UpdateSubuserBag.php │ ├── Data │ │ ├── Subuser.php │ │ └── SubuserFactory.php │ ├── SubusersFeature.php │ └── SubusersHttpFeature.php └── Vms │ ├── Bag │ └── SendVmsBag.php │ ├── Data │ ├── Vms.php │ └── VmsFactory.php │ ├── VmsFeature.php │ └── VmsHttpFeature.php ├── Infrastructure ├── HttpClient │ ├── ClientException.php │ ├── Decorator │ │ ├── AcceptJsonHeaderDecorator.php │ │ ├── AuthorizationHeaderDecorator.php │ │ ├── BaseUriDecorator.php │ │ ├── CorrelationIdHeaderDecorator.php │ │ ├── HttpClientUserAgentHeaderDecorator.php │ │ └── LoggerDecorator.php │ ├── HttpClientFactory.php │ ├── NetworkException.php │ └── RequestException.php ├── Request.php ├── RequestAssembler │ └── RequestAssembler.php ├── RequestExecutor │ ├── LegacyRequestExecutor.php │ ├── RequestExecutorFactory.php │ └── RestRequestExecutor.php ├── RequestHttpMethod.php ├── RequestMapper │ ├── LegacyRequestMapper.php │ ├── Query │ │ ├── Formatter │ │ │ ├── BuiltInParametersQueryFormatter.php │ │ │ ├── ComplexParametersQueryFormatter.php │ │ │ ├── QueryFormatter.php │ │ │ └── UserParametersQueryFormatter.php │ │ └── QueryParametersData.php │ └── RestRequestMapper.php ├── ResponseHttpCode.php └── ResponseMapper │ ├── ApiErrorException.php │ ├── JsonDecode.php │ ├── JsonException.php │ ├── LegacyResponseMapper.php │ └── RestResponseMapper.php ├── Service ├── HttpDefaultFeatures.php ├── SmsapiComHttpService.php ├── SmsapiComService.php ├── SmsapiPlHttpService.php └── SmsapiPlService.php ├── SmsapiClient.php ├── SmsapiClientException.php └── SmsapiHttpClient.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 5 | 6 | ## [3.0.11] - 2024-03-27 7 | ### Added 8 | - service url scheme and host validation 9 | - service url with port support 10 | 11 | ### Changed 12 | - PSR-18 HTTP client related exceptions namespace moved 13 | 14 | ## [3.0.10] - 2024-01-16 15 | ### Added 16 | - PHP-8.3 support 17 | - `SendSmsBag::$timeRestriction` optional parameter 18 | - `SendSmssBag::$timeRestriction` optional parameter 19 | - `SendSmsToGroupBag::$timeRestriction` optional parameter 20 | - `ScheduleSmsBag::$timeRestriction` optional parameter 21 | - `ScheduleSmssBag::$timeRestriction` optional parameter 22 | - `ScheduleSmsToGroupBag::$timeRestriction` optional parameter 23 | ### Fixed 24 | - sending/scheduling smses in large amount 25 | 26 | ## [3.0.9] - 2024-01-09 27 | ### Fixed 28 | - expired MFA code verification 29 | 30 | ## [3.0.8] - 2023-10-18 31 | ### Added 32 | - `psr/http-message` v2 support 33 | 34 | ## [3.0.7] - 2022-11-21 35 | ### Fixed 36 | - dynamic property deprecations 37 | 38 | ## [3.0.6] - 2022-03-28 39 | ### Fixed 40 | - HTTP headers parsing, PSR-7 compliant 41 | 42 | ## [3.0.5] - 2022-03-23 43 | ### Fixed 44 | - HTTP headers parsing, `HttpClient` issue 45 | 46 | ## [3.0.4] - 2022-01-17 47 | ### Added 48 | - `psr/log` v2, v3 support 49 | 50 | ## [3.0.3] - 2021-07-21 51 | ### Fixed 52 | - Guzzle PSR7 incompatible URI paths, `The path of a URI with an authority must start with a slash "/" or be empty` 53 | 54 | ## [3.0.2] - 2021-01-29 55 | ### Added 56 | - PHP-8 support 57 | 58 | ## [3.0.1] - 2020-12-08 59 | ### Changed 60 | - Guzzle HTTP helpers marked as optional dependency 61 | 62 | ## [3.0.0] - 2020-11-19 63 | ### Added 64 | - PSR-18 support 65 | - PSR-17 support 66 | 67 | ### Changed 68 | - utf-8 as default encoding 69 | - `CreateContactBag::withPhone` marked as deprecated 70 | - `PingFeature::ping` returns authorization and service availability status 71 | 72 | ### Removed 73 | - built-in proxy support - use own implementation 74 | - concrete HTTP client dependency (Guzzle 6) 75 | - `PushFeature` 76 | - `SendSmsBag::setIdx` 77 | - `DeleteSmsBag` 78 | - `FindSendernamesBag` 79 | 80 | ## [2.6.1] - 2020-05-14 81 | ### Fixed 82 | - undelivered messages contact field support 83 | 84 | ## [2.6.0] - 2020-01-31 85 | ### Added 86 | - generation and verification of MFA codes implement 87 | - `MfaFeature` uses mfa endpoint 88 | 89 | ## [2.5.0] - 2020-01-14 90 | ### Added 91 | - custom contact fields support 92 | ### Changed 93 | - `PingFeature` uses ping endpoint 94 | 95 | ## [2.4.0] - 2019-12-30 96 | ### Added 97 | - `BlacklistFeature` 98 | 99 | ## [2.3.4] - 2019-10-16 100 | ### Fixed 101 | - `ApiErrorException` when error undefined 102 | 103 | ## [2.3.3] - 2019-06-18 104 | ### Added 105 | - `DeleteScheduledSmssBag` that replaces deprecated `DeleteSmsBag` 106 | 107 | ### Changed 108 | - `DeleteSmsBag` marked as deprecated 109 | 110 | ## [2.3.2] - 2019-06-17 111 | ### Fixed 112 | - `SmsFeature::deleteScheduledSms` 113 | 114 | ## [2.3.1] - 2019-05-07 115 | ### Changed 116 | - `PushFeature` marked as deprecated 117 | 118 | ## [2.3.0] - 2019-04-16 119 | ### Added 120 | - `ContactsFeature::deleteContacts` 121 | 122 | ## [2.2.0] - 2019-04-12 123 | ### Changed 124 | - `ContactsFeature::findContacts` parameter bag made optional 125 | 126 | ## [2.1.3] - 2019-04-11 127 | ### Fixed 128 | - `CreateContactBag::withPhone` 129 | 130 | ## [2.1.2] - 2019-03-25 131 | ### Added 132 | - api error exception error 133 | ### Changed 134 | - api error exception messages 135 | ### Fixed 136 | - `CreateContactBag::withPhoneNumber` method is deprecated, use `CreateContactBag::withPhone` instead 137 | 138 | ## [2.1.1] - 2019-02-19 139 | ### Fixed 140 | - sms content parts 141 | 142 | ## [2.1.0] - 2019-02-07 143 | ### Added 144 | - sending sms to many recipients at once 145 | - sms details 146 | - update subuser feature 147 | - find contact groups feature 148 | ### Fixed 149 | - sms params 150 | - sending sms with external id (idx) 151 | - decimal subuser points 152 | - marks `FindSendernamesBag` as deprecated 153 | 154 | ## [2.0.0] - 2018-10-30 155 | ### Added 156 | - support for `Ping` 157 | - basic support for `Push` 158 | - basic support for `Short Url` 159 | - new `Contacts` resources 160 | - `psr/log` support 161 | - support for `proxy servers` 162 | - support for `PHP v7` 163 | ### Changed 164 | - library is rewritten from-scratch 165 | - `user.do` is superseded by `profile` and `subusers` 166 | - `sender.do` is superseded by `sms/sendernames` 167 | - `curl` and `native` HTTP transport is superseded by `guzzlehttp/guzzle` 168 | ### Removed 169 | - `Basic` authorization 170 | - support for `PHP v5` 171 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2017 SMSAPI 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "smsapi/php-client", 3 | "description": "SMSAPI API PHP Client", 4 | "homepage": "https://github.com/smsapi/smsapi-php-client", 5 | "type": "library", 6 | "license": "Apache-2.0", 7 | "keywords": ["api client", "sms", "mms", "vms", "hlr", "url shortener", "smsapi", "smsapi.pl", "smsapi.com"], 8 | "autoload": { 9 | "psr-4": { 10 | "Smsapi\\Client\\": "src/" 11 | } 12 | }, 13 | "autoload-dev": { 14 | "psr-4": { 15 | "Smsapi\\Client\\Tests\\": "tests/" 16 | } 17 | }, 18 | "require": { 19 | "php": "^7 || ~8.0 || ~8.1 || ~8.2 || ~8.3", 20 | "ext-json": "*", 21 | "psr/log": "^1 || ^2 || ^3", 22 | "psr/http-message": "~1.0 || ~1.1 || ~2.0", 23 | "psr/http-client": "^1", 24 | "psr/http-factory": "^1" 25 | }, 26 | "require-dev": { 27 | "ext-mbstring": "*", 28 | "phpunit/phpunit": "^6 || ~8.5", 29 | "phpspec/prophecy": "^1.7", 30 | "symfony/yaml": "^3", 31 | "doctrine/instantiator": "1.0.5 || ^1.4.0", 32 | "phpdocumentor/reflection-docblock": "^4.3 || ^5.2.0", 33 | "phpdocumentor/type-resolver": "^0.5 || ^1.3.0", 34 | "guzzlehttp/psr7": "^1 || ^2", 35 | "ext-curl": "*", 36 | "roave/security-advisories": "dev-latest" 37 | }, 38 | "suggest": { 39 | "ext-curl": "To use Curl HttpClient", 40 | "guzzlehttp/psr7": "To use Curl HttpClient and HTTP message factories" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Curl/Discovery/CurlDiscovery.php: -------------------------------------------------------------------------------- 1 | prepareRequestHttpClient($request); 22 | 23 | $this->prepareRequestMethod($request, $httpClient); 24 | $this->prepareRequestBody($request, $httpClient); 25 | $this->prepareRequestHeaders($request, $httpClient); 26 | $this->prepareResponseOptions($httpClient); 27 | 28 | $response = $this->execute($request, $httpClient); 29 | 30 | $this->closeHttpClient($httpClient); 31 | 32 | return $response; 33 | } 34 | 35 | private function prepareRequestHttpClient(RequestInterface $request) 36 | { 37 | $url = strtr("{scheme}://{host}{port}{path}", [ 38 | '{scheme}' => $request->getUri()->getScheme(), 39 | '{host}' => $request->getUri()->getHost(), 40 | '{port}' => $request->getUri()->getPort() ? ':' . $request->getUri()->getPort() : '', 41 | '{path}' => $request->getRequestTarget() 42 | ]); 43 | 44 | $httpClient = curl_init($url); 45 | 46 | if ($httpClient === false) { 47 | throw NetworkException::withRequest( 48 | 'Cannot prepare HTTP client: ' . curl_error($httpClient), 49 | $request 50 | ); 51 | } 52 | 53 | return $httpClient; 54 | } 55 | 56 | private function prepareRequestMethod(RequestInterface $request, $httpClient) 57 | { 58 | curl_setopt($httpClient, CURLOPT_CUSTOMREQUEST, $request->getMethod()); 59 | } 60 | 61 | private function prepareRequestBody(RequestInterface $request, $httpClient) 62 | { 63 | curl_setopt($httpClient, CURLOPT_POSTFIELDS, (string)$request->getBody()); 64 | } 65 | 66 | private function prepareRequestHeaders(RequestInterface $request, $httpClient) 67 | { 68 | $headers = []; 69 | foreach ($request->getHeaders() as $name => $values) { 70 | $headers[] = $name . ': ' . implode(', ', $values); 71 | } 72 | 73 | curl_setopt($httpClient, CURLOPT_HTTPHEADER, $headers); 74 | } 75 | 76 | private function prepareResponseOptions($httpClient) 77 | { 78 | curl_setopt($httpClient, CURLOPT_HEADER, true); 79 | curl_setopt($httpClient, CURLOPT_RETURNTRANSFER, true); 80 | } 81 | 82 | private function execute(RequestInterface $request, $httpClient): ResponseInterface 83 | { 84 | $response = curl_exec($httpClient); 85 | 86 | if ($response === false) { 87 | throw RequestException::withRequest( 88 | 'Cannot send request: ' . curl_error($httpClient), 89 | $request 90 | ); 91 | } 92 | 93 | $responseStatus = curl_getinfo($httpClient, CURLINFO_HTTP_CODE); 94 | 95 | $headerSize = curl_getinfo($httpClient, CURLINFO_HEADER_SIZE); 96 | $headerString = substr($response, 0, $headerSize); 97 | $headers = HttpHeadersParser::parse($headerString); 98 | 99 | $body = substr($response, $headerSize); 100 | 101 | return new Response($responseStatus, $headers, $body); 102 | } 103 | 104 | private function closeHttpClient($httpClient) 105 | { 106 | curl_close($httpClient); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/Curl/HttpHeadersParser.php: -------------------------------------------------------------------------------- 1 | httpClient = new \Smsapi\Client\SmsapiHttpClient( 30 | new HttpClient(), 31 | new RequestFactory(), 32 | new StreamFactory() 33 | ); 34 | } 35 | 36 | public function smsapiPlService(string $apiToken): SmsapiPlService 37 | { 38 | return $this->httpClient()->smsapiPlService($apiToken); 39 | } 40 | 41 | public function smsapiPlServiceWithUri(string $apiToken, string $uri): SmsapiPlService 42 | { 43 | return $this->httpClient()->smsapiPlServiceWithUri($apiToken, $uri); 44 | } 45 | 46 | public function smsapiComService(string $apiToken): SmsapiComService 47 | { 48 | return $this->httpClient()->smsapiComService($apiToken); 49 | } 50 | 51 | public function smsapiComServiceWithUri(string $apiToken, string $uri): SmsapiComService 52 | { 53 | return $this->httpClient()->smsapiComServiceWithUri($apiToken, $uri); 54 | } 55 | 56 | private function httpClient(): \Smsapi\Client\SmsapiHttpClient 57 | { 58 | if ($this->logger instanceof LoggerInterface) { 59 | $this->httpClient->setLogger($this->logger); 60 | } 61 | 62 | return $this->httpClient; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Curl/StreamFactory.php: -------------------------------------------------------------------------------- 1 | offset = $offset; 17 | $this->limit = $limit; 18 | return $this; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Feature/Bag/SortBag.php: -------------------------------------------------------------------------------- 1 | phoneNumber = $phoneNumber; 22 | } 23 | } -------------------------------------------------------------------------------- /src/Feature/Blacklist/Bag/DeleteBlacklistedPhoneNumberBag.php: -------------------------------------------------------------------------------- 1 | id = $id; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Feature/Blacklist/Bag/FindBlacklistedPhoneNumbersBag.php: -------------------------------------------------------------------------------- 1 | restRequestExecutor = $restRequestExecutor; 24 | $this->blacklistedPhoneNumberFactory = $blacklistedPhoneNumberFactory; 25 | } 26 | 27 | public function createBlacklistedPhoneNumber(CreateBlacklistedPhoneNumberBag $createBlacklistedPhoneNumberBag): BlacklistedPhoneNumber 28 | { 29 | $result = $this->restRequestExecutor->create('blacklist/phone_numbers', (array)$createBlacklistedPhoneNumberBag); 30 | 31 | return $this->blacklistedPhoneNumberFactory->createFromObject($result); 32 | } 33 | 34 | public function findBlacklistedPhoneNumbers(FindBlacklistedPhoneNumbersBag $blacklistPhoneNumbersFindBag): array 35 | { 36 | $result = $this->restRequestExecutor->read('blacklist/phone_numbers', (array)$blacklistPhoneNumbersFindBag); 37 | 38 | return array_map( 39 | [$this->blacklistedPhoneNumberFactory, 'createFromObject'], 40 | $result->collection 41 | ); 42 | } 43 | 44 | public function deleteBlacklistedPhoneNumber(Bag\DeleteBlacklistedPhoneNumberBag $blacklistPhoneNumberDeleteBag) 45 | { 46 | $this->restRequestExecutor->delete('blacklist/phone_numbers/' . $blacklistPhoneNumberDeleteBag->id, []); 47 | } 48 | 49 | public function deleteBlacklistedPhoneNumbers() 50 | { 51 | $this->restRequestExecutor->delete('blacklist/phone_numbers/', []); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Feature/Blacklist/Data/BlacklistedPhoneNumber.php: -------------------------------------------------------------------------------- 1 | id = $result->id; 19 | $blacklistedPhoneNumber->phoneNumber = $result->phone_number; 20 | $blacklistedPhoneNumber->createdAt = new DateTimeImmutable($result->created_at); 21 | 22 | if ($result->expire_at) { 23 | $blacklistedPhoneNumber->expireAt = new DateTimeImmutable($result->expire_at); 24 | } 25 | 26 | return $blacklistedPhoneNumber; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Bag/CreateContactBag.php: -------------------------------------------------------------------------------- 1 | phoneNumber = $phoneNumber; 35 | return $bag; 36 | 37 | } 38 | 39 | public static function withEmail(string $email): self 40 | { 41 | $bag = new self(); 42 | $bag->email = $email; 43 | return $bag; 44 | } 45 | 46 | public function setName(string $firstName, string $lastName): self 47 | { 48 | $this->firstName = $firstName; 49 | $this->lastName = $lastName; 50 | return $this; 51 | } 52 | 53 | public function setCustomField(string $name, string $value): self 54 | { 55 | $this->$name = $value; 56 | return $this; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Bag/DeleteContactBag.php: -------------------------------------------------------------------------------- 1 | contactId = $contactId; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Bag/FindContactBag.php: -------------------------------------------------------------------------------- 1 | contactId = $contactId; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Bag/FindContactsBag.php: -------------------------------------------------------------------------------- 1 | firstName = $firstName; 30 | $this->lastName = $lastName; 31 | return $this; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Bag/UpdateContactBag.php: -------------------------------------------------------------------------------- 1 | contactId = $contactId; 29 | } 30 | 31 | public function setName(string $firstName, string $lastName): self 32 | { 33 | $this->firstName = $firstName; 34 | $this->lastName = $lastName; 35 | return $this; 36 | } 37 | 38 | public function setCustomField(string $name, string $value): self 39 | { 40 | $this->$name = $value; 41 | return $this; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Feature/Contacts/ContactsFeature.php: -------------------------------------------------------------------------------- 1 | restRequestExecutor = $restRequestExecutor; 31 | $this->dataFactoryProvider = $dataFactoryProvider; 32 | } 33 | 34 | public function findContacts(FindContactsBag $findContactsBag = null): array 35 | { 36 | $result = $this->restRequestExecutor->read('contacts', (array)$findContactsBag); 37 | 38 | return array_map( 39 | [$this->dataFactoryProvider->provideContactFactory(), 'createFromObject'], 40 | $result->collection 41 | ); 42 | } 43 | 44 | public function findContact(FindContactBag $findContactBag): Contact 45 | { 46 | $result = $this->restRequestExecutor->read('contacts/' . $findContactBag->contactId, []); 47 | 48 | return $this->dataFactoryProvider->provideContactFactory()->createFromObject($result); 49 | } 50 | 51 | public function createContact(CreateContactBag $createContactBag): Contact 52 | { 53 | $result = $this->restRequestExecutor->create('contacts', (array)$createContactBag); 54 | 55 | return $this->dataFactoryProvider->provideContactFactory()->createFromObject($result); 56 | } 57 | 58 | public function updateContact(UpdateContactBag $updateContactBag): Contact 59 | { 60 | $contactId = $updateContactBag->contactId; 61 | 62 | unset($updateContactBag->contactId); 63 | 64 | $result = $this->restRequestExecutor->update('contacts/' . $contactId, (array)$updateContactBag); 65 | 66 | return $this->dataFactoryProvider->provideContactFactory()->createFromObject($result); 67 | } 68 | 69 | public function deleteContact(DeleteContactBag $deleteContactBag) 70 | { 71 | $this->restRequestExecutor->delete('contacts/' . $deleteContactBag->contactId, []); 72 | } 73 | 74 | public function deleteContacts() 75 | { 76 | $this->restRequestExecutor->delete('contacts', []); 77 | } 78 | 79 | public function groupsFeature(): ContactsGroupsFeature 80 | { 81 | return new ContactsGroupsHttpFeature($this->restRequestExecutor, $this->dataFactoryProvider); 82 | } 83 | 84 | public function fieldsFeature(): ContactsFieldsFeature 85 | { 86 | return new ContactsFieldsHttpFeature($this->restRequestExecutor, $this->dataFactoryProvider); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Data/Contact.php: -------------------------------------------------------------------------------- 1 | name = $name; 19 | $this->value = $value; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Data/ContactCustomFieldFactory.php: -------------------------------------------------------------------------------- 1 | contactGroupFactory = $contactGroupFactory; 22 | $this->contactCustomFieldFactory = $contactCustomFieldsFactory; 23 | } 24 | 25 | public function createFromObject(stdClass $object): Contact 26 | { 27 | $contact = new Contact(); 28 | $contact->id = $object->id; 29 | $contact->dateCreated = new DateTime($object->date_created); 30 | $contact->dateUpdated = new DateTime($object->date_updated); 31 | $contact->gender = $object->gender; 32 | 33 | if (isset($object->email)) { 34 | $contact->email = $object->email; 35 | } 36 | 37 | if (isset($object->phone_number)) { 38 | $contact->phoneNumber = $object->phone_number; 39 | } 40 | 41 | if (isset($object->country)) { 42 | $contact->country = $object->country; 43 | } 44 | 45 | if (isset($object->undelivered_messages)) { 46 | $contact->undeliveredMessages = $object->undelivered_messages; 47 | } 48 | 49 | $contact->groups = array_map( 50 | [$this->contactGroupFactory, 'createFromObjectWithoutPermissions'], 51 | $object->groups 52 | ); 53 | 54 | $contact->customFields = $this->createCustomFieldsFromObject($object); 55 | 56 | return $contact; 57 | } 58 | 59 | private function createCustomFieldsFromObject(stdClass $object): array 60 | { 61 | $objectCustomFieldsProperties = array_filter( 62 | get_object_vars($object), 63 | [$this, 'isCustomFieldProperty'], 64 | ARRAY_FILTER_USE_KEY 65 | ); 66 | 67 | $customFields = []; 68 | 69 | foreach ($objectCustomFieldsProperties as $name => $value) { 70 | $customFields[] = $this->contactCustomFieldFactory->create($name, $value); 71 | } 72 | 73 | return $customFields; 74 | } 75 | 76 | public function isCustomFieldProperty(string $propertyName): bool 77 | { 78 | return !in_array($propertyName, [ 79 | 'id', 'date_created', 'date_updated', 'gender', 'email', 'phone_number', 'country', 'groups', 'undelivered_messages' 80 | ]); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Data/ContactGroup.php: -------------------------------------------------------------------------------- 1 | groupPermissionFactory = $groupPermissionFactory; 21 | } 22 | 23 | public function createFromObjectWithoutPermissions(stdClass $object): ContactGroup 24 | { 25 | $contactGroup = new ContactGroup(); 26 | $contactGroup->id = $object->id; 27 | $contactGroup->name = $object->name; 28 | $contactGroup->dateCreated = new DateTime($object->date_created); 29 | $contactGroup->dateUpdated = new DateTime($object->date_updated); 30 | $contactGroup->description = $object->description; 31 | $contactGroup->idx = $object->idx; 32 | $contactGroup->createdBy = $object->created_by; 33 | $contactGroup->contactsCount = $object->contacts_count; 34 | 35 | if ($object->contact_expire_after) { 36 | $contactGroup->contactExpireAfter = new DateTime($object->contact_expire_after); 37 | } 38 | 39 | return $contactGroup; 40 | } 41 | 42 | public function createFromObjectWithPermissions(stdClass $object): ContactGroup 43 | { 44 | $contactGroup = $this->createFromObjectWithoutPermissions($object); 45 | $contactGroup->permissions = array_map( 46 | [$this->groupPermissionFactory, 'createFromObject'], 47 | $object->permissions 48 | ); 49 | 50 | return $contactGroup; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Fields/Bag/CreateContactFieldBag.php: -------------------------------------------------------------------------------- 1 | name = $name; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Fields/Bag/DeleteContactFieldBag.php: -------------------------------------------------------------------------------- 1 | fieldId = $fieldId; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Fields/Bag/FindContactFieldOptionsBag.php: -------------------------------------------------------------------------------- 1 | fieldId = $fieldId; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Fields/Bag/UpdateContactFieldBag.php: -------------------------------------------------------------------------------- 1 | fieldId = $fieldId; 23 | $this->name = $name; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Fields/ContactsFieldsFeature.php: -------------------------------------------------------------------------------- 1 | restRequestExecutor = $restRequestExecutor; 26 | $this->dataFactoryProvider = $dataFactoryProvider; 27 | } 28 | 29 | public function findFields(): array 30 | { 31 | $result = $this->restRequestExecutor->read('contacts/fields', []); 32 | 33 | return array_map( 34 | [$this->dataFactoryProvider->provideContactFieldFactory(), 'createFromObject'], 35 | $result->collection 36 | ); 37 | } 38 | 39 | public function createField(CreateContactFieldBag $createContactFieldBag): ContactField 40 | { 41 | $result = $this->restRequestExecutor->create('contacts/fields', (array)$createContactFieldBag); 42 | 43 | return $this->dataFactoryProvider->provideContactFieldFactory()->createFromObject($result); 44 | } 45 | 46 | public function updateField(UpdateContactFieldBag $updateContactFieldBag): ContactField 47 | { 48 | $fieldId = $updateContactFieldBag->fieldId; 49 | 50 | unset($updateContactFieldBag->fieldId); 51 | 52 | $result = $this->restRequestExecutor->update( 53 | 'contacts/fields/' . $fieldId, 54 | (array)$updateContactFieldBag 55 | ); 56 | 57 | return $this->dataFactoryProvider->provideContactFieldFactory()->createFromObject($result); 58 | } 59 | 60 | public function deleteField(DeleteContactFieldBag $deleteContactFieldBag) 61 | { 62 | $fieldId = $deleteContactFieldBag->fieldId; 63 | 64 | unset($deleteContactFieldBag->fieldId); 65 | 66 | $this->restRequestExecutor->delete('contacts/fields/' . $fieldId, (array)$deleteContactFieldBag); 67 | } 68 | 69 | public function findFieldOptions(FindContactFieldOptionsBag $findContactFieldOptionsBag): array 70 | { 71 | $endpointPath = 'contacts/fields/' . $findContactFieldOptionsBag->fieldId . '/options'; 72 | $result = $this->restRequestExecutor->read($endpointPath, []); 73 | 74 | return array_map( 75 | [$this->dataFactoryProvider->provideContactFieldOptionFactory(), 'createFromObject'], 76 | $result->collection 77 | ); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Fields/Data/ContactField.php: -------------------------------------------------------------------------------- 1 | id = $object->id; 18 | $contactField->name = $object->name; 19 | $contactField->type = $object->type; 20 | 21 | return $contactField; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Fields/Data/ContactFieldOption.php: -------------------------------------------------------------------------------- 1 | name = $object->name; 18 | $contactFieldOption->value = $object->value; 19 | 20 | return $contactFieldOption; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Bag/AssignContactToGroupBag.php: -------------------------------------------------------------------------------- 1 | contactId = $contactId; 23 | $this->groupId = $groupId; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Bag/CreateGroupBag.php: -------------------------------------------------------------------------------- 1 | name = $name; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Bag/DeleteGroupBag.php: -------------------------------------------------------------------------------- 1 | groupId = $groupId; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Bag/FindContactGroupBag.php: -------------------------------------------------------------------------------- 1 | contactId = $contactId; 23 | $this->groupId = $groupId; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Bag/FindContactGroupsBag.php: -------------------------------------------------------------------------------- 1 | contactId = $contactId; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Bag/FindGroupBag.php: -------------------------------------------------------------------------------- 1 | groupId = $groupId; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Bag/UnpinContactFromGroupBag.php: -------------------------------------------------------------------------------- 1 | contactId = $contactId; 23 | $this->groupId = $groupId; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Bag/UpdateGroupBag.php: -------------------------------------------------------------------------------- 1 | groupId = $groupId; 26 | $this->name = $name; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/ContactsGroupsFeature.php: -------------------------------------------------------------------------------- 1 | restRequestExecutor = $restRequestExecutor; 34 | $this->dataFactoryProvider = $dataFactoryProvider; 35 | } 36 | 37 | public function createGroup(CreateGroupBag $createGroupBag): ContactGroup 38 | { 39 | $result = $this->restRequestExecutor->create('contacts/groups', (array)$createGroupBag); 40 | 41 | return $this->dataFactoryProvider 42 | ->provideContactGroupFactory() 43 | ->createFromObjectWithPermissions($result); 44 | } 45 | 46 | public function deleteGroup(DeleteGroupBag $deleteGroupBag) 47 | { 48 | $groupId = $deleteGroupBag->groupId; 49 | unset($deleteGroupBag->groupId); 50 | $this->restRequestExecutor->delete('contacts/groups/' . $groupId, (array)$deleteGroupBag); 51 | } 52 | 53 | public function findGroup(FindGroupBag $findGroupBag): ContactGroup 54 | { 55 | $result = $this->restRequestExecutor->read('contacts/groups/' . $findGroupBag->groupId, []); 56 | 57 | return $this->dataFactoryProvider 58 | ->provideContactGroupFactory() 59 | ->createFromObjectWithPermissions($result); 60 | } 61 | 62 | public function updateGroup(UpdateGroupBag $updateGroupBag): ContactGroup 63 | { 64 | $groupId = $updateGroupBag->groupId; 65 | 66 | unset($updateGroupBag->groupId); 67 | 68 | $result = $this->restRequestExecutor->update('contacts/groups/' . $groupId, (array)$updateGroupBag); 69 | 70 | return $this->dataFactoryProvider 71 | ->provideContactGroupFactory() 72 | ->createFromObjectWithPermissions($result); 73 | } 74 | 75 | public function assignContactToGroup(AssignContactToGroupBag $assignContactToGroupBag): array 76 | { 77 | $result = $this->restRequestExecutor->update( 78 | 'contacts/' . $assignContactToGroupBag->contactId . '/groups/' . $assignContactToGroupBag->groupId, 79 | [] 80 | ); 81 | return array_map( 82 | [$this->dataFactoryProvider->provideContactGroupFactory(), 'createFromObjectWithPermissions'], 83 | $result->collection 84 | ); 85 | } 86 | 87 | public function findContactGroup(FindContactGroupBag $findContactGroupBag): ContactGroup 88 | { 89 | $result = $this->restRequestExecutor->read( 90 | 'contacts/' . $findContactGroupBag->contactId . '/groups/' . $findContactGroupBag->groupId, 91 | [] 92 | ); 93 | 94 | return $this->dataFactoryProvider 95 | ->provideContactGroupFactory() 96 | ->createFromObjectWithPermissions($result); 97 | } 98 | 99 | public function findContactGroups(FindContactGroupsBag $findContactGroupsBag): array 100 | { 101 | $result = $this->restRequestExecutor->read( 102 | 'contacts/' . $findContactGroupsBag->contactId . '/groups', 103 | [] 104 | ); 105 | return array_map( 106 | [$this->dataFactoryProvider->provideContactGroupFactory(), 'createFromObjectWithPermissions'], 107 | $result->collection 108 | ); 109 | } 110 | 111 | public function unpinContactFromGroup(UnpinContactFromGroupBag $unpinContactFromGroupBag) 112 | { 113 | $this->restRequestExecutor->delete( 114 | 'contacts/' . $unpinContactFromGroupBag->contactId . '/groups/' . $unpinContactFromGroupBag->groupId, 115 | [] 116 | ); 117 | } 118 | 119 | public function findGroups(): array 120 | { 121 | $result = $this->restRequestExecutor->read( 122 | 'contacts/groups/', 123 | [] 124 | ); 125 | 126 | return array_map( 127 | [$this->dataFactoryProvider->provideContactGroupFactory(), 'createFromObjectWithPermissions'], 128 | $result->collection 129 | ); 130 | } 131 | 132 | public function membersFeature(): ContactsGroupsMembersFeature 133 | { 134 | return new ContactsGroupsMembersHttpFeature( 135 | $this->restRequestExecutor, 136 | $this->dataFactoryProvider->provideContactFactory() 137 | ); 138 | } 139 | 140 | public function permissionsFeature(): ContactsGroupsPermissionsFeature 141 | { 142 | return new ContactsGroupsPermissionsHttpFeature( 143 | $this->restRequestExecutor, 144 | $this->dataFactoryProvider->provideGroupPermissionFactory() 145 | ); 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Members/Bag/AddContactToGroupByQueryBag.php: -------------------------------------------------------------------------------- 1 | id = $id; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Members/Bag/FindContactInGroupBag.php: -------------------------------------------------------------------------------- 1 | contactId = $contactId; 23 | $this->groupId = $groupId; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Members/Bag/MoveContactToGroupByQueryBag.php: -------------------------------------------------------------------------------- 1 | id = $id; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Members/Bag/PinContactToGroupBag.php: -------------------------------------------------------------------------------- 1 | contactId = $contactId; 23 | $this->groupId = $groupId; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Members/Bag/UnpinContactFromGroupBag.php: -------------------------------------------------------------------------------- 1 | contactId = $contactId; 23 | $this->groupId = $groupId; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Members/Bag/UnpinContactFromGroupByQueryBag.php: -------------------------------------------------------------------------------- 1 | id = $id; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Members/ContactsGroupsMembersFeature.php: -------------------------------------------------------------------------------- 1 | restRequestExecutor = $restRequestExecutor; 33 | $this->contactFactory = $contactFactory; 34 | } 35 | 36 | /** 37 | * @param AddContactToGroupByQueryBag $addContactToGroupByQueryBag 38 | * @throws SmsapiClientException 39 | */ 40 | public function addContactToGroupByQuery(AddContactToGroupByQueryBag $addContactToGroupByQueryBag) 41 | { 42 | $groupId = $addContactToGroupByQueryBag->id; 43 | unset($addContactToGroupByQueryBag->id); 44 | $this->restRequestExecutor->create( 45 | 'contacts/groups/' . $groupId . '/members', 46 | (array)$addContactToGroupByQueryBag 47 | ); 48 | } 49 | 50 | /** 51 | * @param FindContactInGroupBag $findContactInGroupBag 52 | * @return Contact 53 | * @throws SmsapiClientException 54 | */ 55 | public function findContactInGroup(FindContactInGroupBag $findContactInGroupBag): Contact 56 | { 57 | $result = $this->restRequestExecutor->read( 58 | sprintf( 59 | 'contacts/groups/%s/members/%s', 60 | $findContactInGroupBag->groupId, 61 | $findContactInGroupBag->contactId 62 | ), 63 | [] 64 | ); 65 | return $this->contactFactory->createFromObject($result); 66 | } 67 | 68 | /** 69 | * @param MoveContactToGroupByQueryBag $moveContactToGroupByQueryBag 70 | * @throws SmsapiClientException 71 | */ 72 | public function moveContactToGroupByQuery(MoveContactToGroupByQueryBag $moveContactToGroupByQueryBag) 73 | { 74 | $groupId = $moveContactToGroupByQueryBag->id; 75 | unset($moveContactToGroupByQueryBag->id); 76 | $this->restRequestExecutor->update( 77 | 'contacts/groups/' . $groupId . '/members', 78 | (array)$moveContactToGroupByQueryBag 79 | ); 80 | } 81 | 82 | /** 83 | * @param PinContactToGroupBag $pinContactToGroupBag 84 | * @return Contact 85 | * @throws SmsapiClientException 86 | */ 87 | public function pinContactToGroup(PinContactToGroupBag $pinContactToGroupBag): Contact 88 | { 89 | $result = $this->restRequestExecutor->update( 90 | 'contacts/groups/' . $pinContactToGroupBag->groupId . '/members/' . $pinContactToGroupBag->contactId, 91 | [] 92 | ); 93 | return $this->contactFactory->createFromObject($result); 94 | } 95 | 96 | /** 97 | * @param UnpinContactFromGroupByQueryBag $unpinContactFromGroupByQueryBag 98 | * @throws SmsapiClientException 99 | */ 100 | public function unpinContactFromGroupByQuery(UnpinContactFromGroupByQueryBag $unpinContactFromGroupByQueryBag) 101 | { 102 | $groupId = $unpinContactFromGroupByQueryBag->id; 103 | unset($unpinContactFromGroupByQueryBag->id); 104 | $this->restRequestExecutor->delete( 105 | 'contacts/groups/' . $groupId . '/members', 106 | (array)$unpinContactFromGroupByQueryBag 107 | ); 108 | } 109 | 110 | /** 111 | * @param UnpinContactFromGroupBag $unpinContactFromGroupBag 112 | * @throws SmsapiClientException 113 | */ 114 | public function unpinContactFromGroup(UnpinContactFromGroupBag $unpinContactFromGroupBag) 115 | { 116 | $this->restRequestExecutor->delete( 117 | sprintf( 118 | 'contacts/groups/%s/members/%s', 119 | $unpinContactFromGroupBag->groupId, 120 | $unpinContactFromGroupBag->contactId 121 | ), 122 | [] 123 | ); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Permissions/Bag/CreateGroupPermissionBag.php: -------------------------------------------------------------------------------- 1 | groupId = $groupId; 26 | $this->username = $username; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Permissions/Bag/DeleteGroupPermissionBag.php: -------------------------------------------------------------------------------- 1 | groupId = $groupId; 23 | $this->username = $username; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Permissions/Bag/FindGroupPermissionBag.php: -------------------------------------------------------------------------------- 1 | groupId = $groupId; 23 | $this->username = $username; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Permissions/Bag/FindGroupPermissionsBag.php: -------------------------------------------------------------------------------- 1 | groupId = $groupId; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Permissions/Bag/UpdateGroupPermissionBag.php: -------------------------------------------------------------------------------- 1 | groupId = $groupId; 26 | $this->username = $username; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Permissions/ContactsGroupsPermissionsFeature.php: -------------------------------------------------------------------------------- 1 | restRequestExecutor = $restRequestExecutor; 34 | $this->groupPermissionFactory = $groupPermissionFactory; 35 | } 36 | 37 | /** 38 | * @param CreateGroupPermissionBag $createGroupPermissionBag 39 | * @return GroupPermission 40 | * @throws SmsapiClientException 41 | */ 42 | public function createPermission(CreateGroupPermissionBag $createGroupPermissionBag): GroupPermission 43 | { 44 | $groupId = $createGroupPermissionBag->groupId; 45 | $username = $createGroupPermissionBag->username; 46 | unset($createGroupPermissionBag->groupId); 47 | unset($createGroupPermissionBag->username); 48 | $result = $this->restRequestExecutor->create( 49 | 'contacts/groups/' . $groupId . '/permissions/' . $username, 50 | (array)$createGroupPermissionBag 51 | ); 52 | return $this->groupPermissionFactory->createFromObject($result); 53 | } 54 | 55 | /** 56 | * @param DeleteGroupPermissionBag $deleteGroupPermissionBag 57 | * @throws SmsapiClientException 58 | */ 59 | public function deletePermission(DeleteGroupPermissionBag $deleteGroupPermissionBag) 60 | { 61 | $this->restRequestExecutor->delete( 62 | sprintf( 63 | 'contacts/groups/%s/permissions/%s', 64 | $deleteGroupPermissionBag->groupId, 65 | $deleteGroupPermissionBag->username 66 | ), 67 | [] 68 | ); 69 | } 70 | 71 | /** 72 | * @param UpdateGroupPermissionBag $updateGroupPermissionBag 73 | * @return GroupPermission 74 | * @throws SmsapiClientException 75 | */ 76 | public function updatePermission(UpdateGroupPermissionBag $updateGroupPermissionBag): GroupPermission 77 | { 78 | $groupId = $updateGroupPermissionBag->groupId; 79 | $username = $updateGroupPermissionBag->username; 80 | unset($updateGroupPermissionBag->groupId); 81 | unset($updateGroupPermissionBag->username); 82 | $result = $this->restRequestExecutor->update( 83 | sprintf('contacts/groups/%s/permissions/%s', $groupId, $username), 84 | (array)$updateGroupPermissionBag 85 | ); 86 | return $this->groupPermissionFactory->createFromObject($result); 87 | } 88 | 89 | /** 90 | * @param FindGroupPermissionBag $findGroupPermissionBag 91 | * @return GroupPermission 92 | * @throws SmsapiClientException 93 | */ 94 | public function findPermission(FindGroupPermissionBag $findGroupPermissionBag): GroupPermission 95 | { 96 | $result = $this->restRequestExecutor->read( 97 | sprintf( 98 | 'contacts/groups/%s/permissions/%s', 99 | $findGroupPermissionBag->groupId, 100 | $findGroupPermissionBag->username 101 | ), 102 | [] 103 | ); 104 | return $this->groupPermissionFactory->createFromObject($result); 105 | } 106 | 107 | /** 108 | * @param FindGroupPermissionsBag $findGroupPermissionsBag 109 | * @return GroupPermission[] 110 | * @throws SmsapiClientException 111 | */ 112 | public function findPermissions(FindGroupPermissionsBag $findGroupPermissionsBag): array 113 | { 114 | $result = $this->restRequestExecutor->read( 115 | 'contacts/groups/' . $findGroupPermissionsBag->groupId . '/permissions', 116 | [] 117 | ); 118 | return array_map( 119 | [$this->groupPermissionFactory, 'createFromObject'], 120 | $result->collection 121 | ); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /src/Feature/Contacts/Groups/Permissions/Data/GroupPermission.php: -------------------------------------------------------------------------------- 1 | username = $object->username; 18 | $permission->groupId = $object->group_id; 19 | $permission->write = $object->write; 20 | $permission->read = $object->read; 21 | $permission->send = $object->send; 22 | 23 | return $permission; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Feature/Data/DataFactoryProvider.php: -------------------------------------------------------------------------------- 1 | provideContactGroupFactory(), $this->provideConntactCustomFieldFactory()); 91 | } 92 | 93 | public function provideContactGroupFactory(): ContactGroupFactory 94 | { 95 | return new ContactGroupFactory($this->provideGroupPermissionFactory()); 96 | } 97 | 98 | public function provideGroupPermissionFactory(): GroupPermissionFactory 99 | { 100 | return new GroupPermissionFactory(); 101 | } 102 | 103 | public function provideContactFieldFactory(): ContactFieldFactory 104 | { 105 | return new ContactFieldFactory(); 106 | } 107 | 108 | public function provideContactFieldOptionFactory(): ContactFieldOptionFactory 109 | { 110 | return new ContactFieldOptionFactory(); 111 | } 112 | 113 | public function provideBlacklistedPhoneNumberFactory(): BlacklistedPhoneNumberFactory 114 | { 115 | return new BlacklistedPhoneNumberFactory(); 116 | } 117 | 118 | public function provideConntactCustomFieldFactory(): ContactCustomFieldFactory 119 | { 120 | return new ContactCustomFieldFactory(); 121 | } 122 | 123 | public function providePingFactory(): PingFactory 124 | { 125 | return new PingFactory(); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/Feature/Data/Points.php: -------------------------------------------------------------------------------- 1 | fromAccount = $object->from_account; 18 | $points->perMonth = $object->per_month; 19 | 20 | return $points; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Feature/Hlr/Bag/SendHlrBag.php: -------------------------------------------------------------------------------- 1 | number = $number; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Feature/Hlr/Data/Hlr.php: -------------------------------------------------------------------------------- 1 | status = $result->status; 18 | $hlr->number = $result->number; 19 | $hlr->id = $result->id; 20 | $hlr->price = (float)$result->price; 21 | 22 | return $hlr; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Feature/Hlr/HlrFeature.php: -------------------------------------------------------------------------------- 1 | legacyRequestExecutor = $legacyRequestExecutor; 29 | $this->hlrFactory = $hlrFactory; 30 | } 31 | 32 | /** 33 | * @param SendHlrBag $sendHlrBag 34 | * @return Hlr 35 | * @throws SmsapiClientException 36 | */ 37 | public function sendHlr(SendHlrBag $sendHlrBag): Hlr 38 | { 39 | return $this->hlrFactory->createFromObject($this->makeRequest($sendHlrBag)); 40 | } 41 | 42 | /** 43 | * @param object $data 44 | * @return stdClass 45 | * @throws SmsapiClientException 46 | */ 47 | private function makeRequest($data): stdClass 48 | { 49 | return $this->legacyRequestExecutor->request('hlr.do', (array)$data); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Feature/Mfa/Bag/CreateMfaBag.php: -------------------------------------------------------------------------------- 1 | phoneNumber = $phoneNumber; 24 | } 25 | 26 | public static function notFast(string $phonenumber): self 27 | { 28 | $createMfaBag = new self($phonenumber); 29 | $createMfaBag->fast = false; 30 | 31 | return $createMfaBag; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Feature/Mfa/Bag/VerificationMfaBag.php: -------------------------------------------------------------------------------- 1 | code = $code; 25 | $this->phoneNumber = $phoneNumber; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Feature/Mfa/Data/Mfa.php: -------------------------------------------------------------------------------- 1 | id = $object->id; 18 | $mfa->code = $object->code; 19 | $mfa->phoneNumber = $object->phone_number; 20 | $mfa->from = $object->from; 21 | 22 | return $mfa; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Feature/Mfa/MfaFeature.php: -------------------------------------------------------------------------------- 1 | restRequestExecutor = $restRequestExecutor; 25 | $this->mfaFactory = $mfaFactory; 26 | } 27 | 28 | /** 29 | * @throws SmsapiClientException 30 | */ 31 | public function generateMfa(CreateMfaBag $createMfaBag): Mfa 32 | { 33 | $result = $this->restRequestExecutor->create('mfa/codes', (array)$createMfaBag); 34 | return $this->mfaFactory->createFromObject($result); 35 | } 36 | 37 | /** 38 | * @throws SmsapiClientException 39 | */ 40 | public function verifyMfa(VerificationMfaBag $verificationMfaBag) 41 | { 42 | $this->restRequestExecutor->create('mfa/codes/verifications', (array)$verificationMfaBag); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Feature/Mms/Bag/SendMmsBag.php: -------------------------------------------------------------------------------- 1 | to = $receiver; 26 | $this->subject = $subject; 27 | $this->smil = $smil; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Feature/Mms/Data/Mms.php: -------------------------------------------------------------------------------- 1 | id = $object->id; 18 | $mms->points = (float)$object->points; 19 | $mms->status = $object->status; 20 | $mms->dateSent = new DateTime('@' . $object->date_sent); 21 | $mms->number = $object->number; 22 | 23 | return $mms; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Feature/Mms/MmsFeature.php: -------------------------------------------------------------------------------- 1 | legacyRequestExecutor = $legacyRequestExecutor; 23 | $this->mmsFactory = $mmsFactory; 24 | } 25 | 26 | /** 27 | * @param SendMmsBag $sendMmsBag 28 | * @return Mms 29 | * @throws SmsapiClientException 30 | */ 31 | public function sendMms(SendMmsBag $sendMmsBag): Mms 32 | { 33 | $object = current($this->legacyRequestExecutor->request('mms.do', (array)$sendMmsBag)->list); 34 | 35 | return $this->mmsFactory->createFromObject($object); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Feature/Ping/Data/Ping.php: -------------------------------------------------------------------------------- 1 | authorized = $result->authorized; 15 | $ping->unavailable = $result->unavailable; 16 | 17 | return $ping; 18 | } 19 | } -------------------------------------------------------------------------------- /src/Feature/Ping/PingFeature.php: -------------------------------------------------------------------------------- 1 | restRequestExecutor = $restRequestExecutor; 21 | $this->pingFactory = $pingFactory; 22 | } 23 | 24 | public function ping(): Ping 25 | { 26 | $result = $this->restRequestExecutor->read('ping', []); 27 | 28 | return $this->pingFactory->createFromObject($result); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Feature/Profile/Data/Money.php: -------------------------------------------------------------------------------- 1 | amount = $object->amount; 19 | $money->currency = $object->currency; 20 | 21 | return $money; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Feature/Profile/Data/Profile.php: -------------------------------------------------------------------------------- 1 | name = $object->name; 17 | $profile->username = $object->username; 18 | $profile->email = $object->email; 19 | $profile->phoneNumber = $object->phone_number; 20 | $profile->paymentType = $object->payment_type; 21 | $profile->userType = $object->user_type; 22 | 23 | if (isset($object->points)) { 24 | $profile->points = $object->points; 25 | } 26 | 27 | return $profile; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Feature/Profile/Data/ProfilePrice.php: -------------------------------------------------------------------------------- 1 | name = $object->name; 20 | $profilePriceCountry->mcc = $object->mcc; 21 | 22 | return $profilePriceCountry; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Feature/Profile/Data/ProfilePriceFactory.php: -------------------------------------------------------------------------------- 1 | countryFactory = $countryFactory; 28 | $this->networkFactory = $networkFactory; 29 | $this->moneyFactory = $moneyFactory; 30 | } 31 | 32 | public function createFromObject(stdClass $object): ProfilePrice 33 | { 34 | $profilePrice = new ProfilePrice(); 35 | 36 | $profilePrice->type = $object->type; 37 | $profilePrice->price = $this->moneyFactory->createFromObject($object->price); 38 | $profilePrice->country = $this->countryFactory->createFromObject($object->country); 39 | $profilePrice->network = $this->networkFactory->createFromObject($object->network); 40 | 41 | if ($object->changed_at) { 42 | $profilePrice->changedAt = DateTime::createFromFormat('Y-m-d', $object->changed_at); 43 | } 44 | 45 | return $profilePrice; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Feature/Profile/Data/ProfilePriceNetwork.php: -------------------------------------------------------------------------------- 1 | name = $object->name; 20 | $profilePriceNetwork->mnc = $object->mnc; 21 | 22 | return $profilePriceNetwork; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Feature/Profile/ProfileDefaultHttpFeatures.php: -------------------------------------------------------------------------------- 1 | dataFactoryProvider 30 | ->provideProfileFactory() 31 | ->createFromObject($this->restRequestExecutor->read('profile', [])); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Feature/Profile/ProfileFeature.php: -------------------------------------------------------------------------------- 1 | restRequestExecutor = $restRequestExecutor; 24 | $this->dataFactoryProvider = $dataFactoryProvider; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Feature/Profile/SmsapiPlProfileFeature.php: -------------------------------------------------------------------------------- 1 | restRequestExecutor = $restRequestExecutor; 23 | $this->dataFactoryProvider = $dataFactoryProvider; 24 | } 25 | 26 | public function findProfilePrices(): array 27 | { 28 | $result = $this->restRequestExecutor->read('profile/prices', []); 29 | return array_map( 30 | [$this->dataFactoryProvider->provideProfilePriceFactory(), 'createFromObject'], 31 | $result->collection 32 | ); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Feature/ShortUrl/Bag/CreateShortUrlLinkBag.php: -------------------------------------------------------------------------------- 1 | url = $url; 25 | return $bag; 26 | } 27 | 28 | public static function withFile(string $file): self 29 | { 30 | $bag = new self(); 31 | $bag->file = $file; 32 | return $bag; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Feature/ShortUrl/Data/ShortUrlLink.php: -------------------------------------------------------------------------------- 1 | id = $object->id; 20 | $shortUrlLink->name = $object->name; 21 | $shortUrlLink->url = $object->url; 22 | $shortUrlLink->shortUrl = $object->short_url; 23 | $shortUrlLink->filename = $object->filename; 24 | $shortUrlLink->type = $object->type; 25 | $shortUrlLink->expire = new DateTime($object->expire); 26 | $shortUrlLink->hits = $object->hits; 27 | $shortUrlLink->hitsUnique = $object->hits_unique; 28 | $shortUrlLink->description = $object->description; 29 | return $shortUrlLink; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Feature/ShortUrl/ShortUlrHttpFeature.php: -------------------------------------------------------------------------------- 1 | restRequestExecutor = $restRequestExecutor; 27 | $this->factory = $factory; 28 | } 29 | 30 | /** 31 | * @inheritdoc 32 | */ 33 | public function createShortUrlLink(CreateShortUrlLinkBag $createShortUrlLink): ShortUrlLink 34 | { 35 | $result = $this->restRequestExecutor->create('short_url/links', (array)$createShortUrlLink); 36 | return $this->factory->createFromObject($result); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Feature/ShortUrl/ShortUrlFeature.php: -------------------------------------------------------------------------------- 1 | smsIds = $smsIds; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Feature/Sms/Bag/ScheduleSmsBag.php: -------------------------------------------------------------------------------- 1 | date = $scheduleAt; 45 | $bag->to = $receiver; 46 | $bag->message = $message; 47 | 48 | return $bag; 49 | } 50 | 51 | public static function withTemplateName(DateTimeInterface $scheduleAt, string $receiver, string $templateName): self 52 | { 53 | $bag = new self(); 54 | $bag->date = $scheduleAt; 55 | $bag->to = $receiver; 56 | $bag->template = $templateName; 57 | 58 | return $bag; 59 | } 60 | 61 | public function setParams(array $params): self 62 | { 63 | foreach ($params as $index => $indexParam) { 64 | $this->{'param' . $index} = $indexParam; 65 | } 66 | 67 | return $this; 68 | } 69 | 70 | public function setExternalId(string $idx, bool $checkIdx = null): self 71 | { 72 | $this->idx = [$idx]; 73 | $this->checkIdx = $checkIdx; 74 | 75 | return $this; 76 | } 77 | 78 | /** 79 | * @deprecated 80 | * @see ScheduleSmsBag::setExternalId() 81 | */ 82 | public function setIdx(array $idx, bool $checkIdx = null): self 83 | { 84 | $this->idx = $idx; 85 | $this->checkIdx = $checkIdx; 86 | 87 | return $this; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/Feature/Sms/Bag/ScheduleSmsToGroupBag.php: -------------------------------------------------------------------------------- 1 | date = $scheduleAt; 42 | $bag->group = $group; 43 | $bag->message = $message; 44 | 45 | return $bag; 46 | } 47 | 48 | public static function withTemplateName(DateTimeInterface $scheduleAt, string $group, string $templateName): self 49 | { 50 | $bag = new self(); 51 | $bag->date = $scheduleAt; 52 | $bag->group = $group; 53 | $bag->template = $templateName; 54 | 55 | return $bag; 56 | } 57 | 58 | public function setExternalId(string $idx, bool $checkIdx = null): self 59 | { 60 | $this->idx = [$idx]; 61 | $this->checkIdx = $checkIdx; 62 | 63 | return $this; 64 | } 65 | 66 | /** 67 | * @deprecated 68 | * @see ScheduleSmsToGroupBag::setExternalId() 69 | */ 70 | public function setIdx(array $idx, bool $checkIdx = null): self 71 | { 72 | $this->idx = $idx; 73 | $this->checkIdx = $checkIdx; 74 | 75 | return $this; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Feature/Sms/Bag/ScheduleSmssBag.php: -------------------------------------------------------------------------------- 1 | date = $scheduleAt; 46 | $bag->to = $receivers; 47 | $bag->message = $message; 48 | 49 | return $bag; 50 | } 51 | 52 | public static function withTemplateName(DateTimeInterface $scheduleAt, array $receivers, string $templateName): self 53 | { 54 | $bag = new self(); 55 | $bag->date = $scheduleAt; 56 | $bag->to = $receivers; 57 | $bag->template = $templateName; 58 | 59 | return $bag; 60 | } 61 | 62 | public function setParams(array $params): self 63 | { 64 | foreach ($params as $index => $indexParams) { 65 | $this->{'param' . $index} = $indexParams; 66 | } 67 | 68 | return $this; 69 | } 70 | 71 | public function setExternalId(array $idx, bool $checkIdx = null): self 72 | { 73 | $this->idx = $idx; 74 | $this->checkIdx = $checkIdx; 75 | 76 | return $this; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/Feature/Sms/Bag/SendSmsBag.php: -------------------------------------------------------------------------------- 1 | to = $receiver; 42 | $bag->message = $message; 43 | 44 | return $bag; 45 | } 46 | 47 | public static function withTemplateName(string $receiver, string $template): self 48 | { 49 | $bag = new self(); 50 | $bag->to = $receiver; 51 | $bag->template = $template; 52 | 53 | return $bag; 54 | } 55 | 56 | public function setParams(array $params): self 57 | { 58 | foreach ($params as $index => $indexParam) { 59 | $this->{'param' . $index} = $indexParam; 60 | } 61 | 62 | return $this; 63 | } 64 | 65 | public function setExternalId(string $idx, bool $checkIdx = null): self 66 | { 67 | $this->idx = [$idx]; 68 | $this->checkIdx = $checkIdx; 69 | 70 | return $this; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/Feature/Sms/Bag/SendSmsToGroupBag.php: -------------------------------------------------------------------------------- 1 | group = $group; 37 | $bag->message = $message; 38 | 39 | return $bag; 40 | } 41 | 42 | public static function withTemplateName(string $group, string $templateName): self 43 | { 44 | $bag = new self(); 45 | $bag->group = $group; 46 | $bag->template = $templateName; 47 | 48 | return $bag; 49 | } 50 | 51 | public function setExternalId(string $idx, bool $checkIdx = null): self 52 | { 53 | $this->idx = [$idx]; 54 | $this->checkIdx = $checkIdx; 55 | 56 | return $this; 57 | } 58 | 59 | /** 60 | * @deprecated 61 | * @see SendSmsToGroupBag::setExternalId() 62 | */ 63 | public function setIdx(array $idx, bool $checkIdx = null): self 64 | { 65 | $this->idx = $idx; 66 | $this->checkIdx = $checkIdx; 67 | 68 | return $this; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Feature/Sms/Bag/SendSmssBag.php: -------------------------------------------------------------------------------- 1 | to = $receivers; 43 | $bag->message = $message; 44 | 45 | return $bag; 46 | } 47 | 48 | public static function withTemplateName(array $receivers, string $template): self 49 | { 50 | $bag = new self(); 51 | $bag->to = $receivers; 52 | $bag->template = $template; 53 | 54 | return $bag; 55 | } 56 | 57 | public function setParams(array $params): self 58 | { 59 | foreach ($params as $index => $indexParams) { 60 | $this->{'param' . $index} = $indexParams; 61 | } 62 | 63 | return $this; 64 | } 65 | 66 | public function setExternalId(array $idx, bool $checkIdx = null): self 67 | { 68 | $this->idx = $idx; 69 | $this->checkIdx = $checkIdx; 70 | 71 | return $this; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/Feature/Sms/Data/Sms.php: -------------------------------------------------------------------------------- 1 | message = $message; 17 | $smsDetails->length = $length; 18 | $smsDetails->parts = $parts; 19 | 20 | return $smsDetails; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Feature/Sms/Data/SmsFactory.php: -------------------------------------------------------------------------------- 1 | id = $object->id; 18 | $sms->points = (float)$object->points; 19 | $sms->status = $object->status; 20 | $sms->dateSent = new DateTime('@' . $object->date_sent); 21 | $sms->number = $object->number; 22 | $sms->idx = $object->idx; 23 | 24 | return $sms; 25 | } 26 | 27 | public function createFromObjectWithDetails(stdClass $object, string $message, int $length, $parts): Sms 28 | { 29 | $parts = (int)$parts; 30 | 31 | $sms = $this->createFromObject($object); 32 | 33 | $sms->content = (new SmsContentFactory())->create($message, $length, $parts); 34 | 35 | return $sms; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Feature/Sms/Sendernames/Bag/CreateSendernameBag.php: -------------------------------------------------------------------------------- 1 | sender = $sender; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Feature/Sms/Sendernames/Bag/DeleteSendernameBag.php: -------------------------------------------------------------------------------- 1 | sender = $sender; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Feature/Sms/Sendernames/Bag/FindSendernameBag.php: -------------------------------------------------------------------------------- 1 | sender = $sender; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Feature/Sms/Sendernames/Bag/MakeSendernameDefaultBag.php: -------------------------------------------------------------------------------- 1 | sender = $sender; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Feature/Sms/Sendernames/Data/Sendername.php: -------------------------------------------------------------------------------- 1 | status = $object->status; 18 | $sendername->createdAt = new DateTime($object->created_at); 19 | $sendername->isDefault = $object->is_default; 20 | $sendername->sender = $object->sender; 21 | 22 | return $sendername; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Feature/Sms/Sendernames/SendernamesFeature.php: -------------------------------------------------------------------------------- 1 | restRequestExecutor = $restRequestExecutor; 26 | $this->sendernameFactory = $sendernameFactory; 27 | } 28 | 29 | /** 30 | * @param FindSendernameBag $findSendernameBag 31 | * @return Sendername 32 | * @throws SmsapiClientException 33 | */ 34 | public function findSendername(FindSendernameBag $findSendernameBag): Sendername 35 | { 36 | $result = $this->restRequestExecutor->read( 37 | sprintf('sms/sendernames/%s', $findSendernameBag->sender), 38 | [] 39 | ); 40 | 41 | return $this->sendernameFactory->createFromObject($result); 42 | } 43 | 44 | public function findSendernames(): array 45 | { 46 | $result = $this->restRequestExecutor->read('sms/sendernames', []); 47 | 48 | return array_map([$this->sendernameFactory, 'createFromObject'], $result->collection); 49 | } 50 | 51 | /** 52 | * @param CreateSendernameBag $createSendernameBag 53 | * @return Sendername 54 | * @throws SmsapiClientException 55 | */ 56 | public function createSendername(CreateSendernameBag $createSendernameBag): Sendername 57 | { 58 | $result = $this->restRequestExecutor->create('sms/sendernames', (array)$createSendernameBag); 59 | 60 | return $this->sendernameFactory->createFromObject($result); 61 | } 62 | 63 | /** 64 | * @param DeleteSendernameBag $deleteSendernameBag 65 | * @return void 66 | * @throws SmsapiClientException 67 | */ 68 | public function deleteSendername(DeleteSendernameBag $deleteSendernameBag) 69 | { 70 | $this->restRequestExecutor->delete(sprintf('sms/sendernames/%s', $deleteSendernameBag->sender), []); 71 | } 72 | 73 | /** 74 | * @param MakeSendernameDefaultBag $makeSendernameDefault 75 | * @return void 76 | * @throws SmsapiClientException 77 | */ 78 | public function makeSendernameDefault(MakeSendernameDefaultBag $makeSendernameDefault) 79 | { 80 | $this->restRequestExecutor->create( 81 | sprintf('sms/sendernames/%s/commands/make_default', $makeSendernameDefault->sender), 82 | [] 83 | ); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/Feature/Sms/SmsFeature.php: -------------------------------------------------------------------------------- 1 | externalHttpClient = $externalHttpClient; 37 | $this->requestExecutorFactory = $requestExecutorFactory; 38 | $this->dataFactoryProvider = $dataFactoryProvider; 39 | } 40 | 41 | public function sendernameFeature(): SendernamesFeature 42 | { 43 | return new SendernamesHttpFeature( 44 | $this->requestExecutorFactory->createRestRequestExecutor($this->externalHttpClient), 45 | $this->dataFactoryProvider->provideSendernameFactory() 46 | ); 47 | } 48 | 49 | public function sendSms(SendSmsBag $sendSmsBag): Sms 50 | { 51 | $sendSmsBag->details = true; 52 | 53 | $response = $this->makeRequest($sendSmsBag); 54 | 55 | return $this->dataFactoryProvider->provideSmsFactory()->createFromObjectWithDetails( 56 | current($response->list), 57 | $response->message, 58 | $response->length, 59 | $response->parts 60 | ); 61 | } 62 | 63 | /** 64 | * @param SendSmsBag $sendSmsBag 65 | * @return Sms 66 | * @throws SmsapiClientException 67 | */ 68 | public function sendFlashSms(SendSmsBag $sendSmsBag): Sms 69 | { 70 | $sendSmsBag->flash = true; 71 | $sendSmsBag->details = true; 72 | 73 | $response = $this->makeRequest($sendSmsBag); 74 | 75 | return $this->dataFactoryProvider->provideSmsFactory()->createFromObjectWithDetails( 76 | current($response->list), 77 | $response->message, 78 | $response->length, 79 | $response->parts 80 | ); 81 | } 82 | 83 | /** 84 | * @param SendSmsToGroupBag $sendSmsToGroupBag 85 | * @return array 86 | * @throws SmsapiClientException 87 | */ 88 | public function sendSmsToGroup(SendSmsToGroupBag $sendSmsToGroupBag): array 89 | { 90 | return array_map( 91 | [$this->dataFactoryProvider->provideSmsFactory(), 'createFromObject'], 92 | $this->makeRequest($sendSmsToGroupBag)->list 93 | ); 94 | } 95 | 96 | /** 97 | * @param SendSmsToGroupBag $sendSmsToGroupBag 98 | * @return array 99 | * @throws SmsapiClientException 100 | */ 101 | public function sendFlashSmsToGroup(SendSmsToGroupBag $sendSmsToGroupBag): array 102 | { 103 | $sendSmsToGroupBag->flash = true; 104 | 105 | return array_map( 106 | [$this->dataFactoryProvider->provideSmsFactory(), 'createFromObject'], 107 | $this->makeRequest($sendSmsToGroupBag)->list 108 | ); 109 | } 110 | 111 | public function sendSmss(SendSmssBag $sendSmssBag): array 112 | { 113 | $sendSmssBag->to = implode(',', $sendSmssBag->to); 114 | 115 | return array_map( 116 | [$this->dataFactoryProvider->provideSmsFactory(), 'createFromObject'], 117 | $this->makeRequest($sendSmssBag)->list 118 | ); 119 | } 120 | 121 | public function sendFlashSmss(SendSmssBag $sendSmssBag): array 122 | { 123 | $sendSmssBag->to = implode(',', $sendSmssBag->to); 124 | 125 | return array_map( 126 | [$this->dataFactoryProvider->provideSmsFactory(), 'createFromObject'], 127 | $this->makeRequest($sendSmssBag)->list 128 | ); 129 | } 130 | 131 | /** 132 | * @param ScheduleSmsBag $scheduleSmsBag 133 | * @return Sms 134 | * @throws SmsapiClientException 135 | */ 136 | public function scheduleSms(ScheduleSmsBag $scheduleSmsBag): Sms 137 | { 138 | $scheduleSmsBag->dateValidate = true; 139 | $scheduleSmsBag->details = true; 140 | 141 | $response = $this->makeRequest($scheduleSmsBag); 142 | 143 | return $this->dataFactoryProvider->provideSmsFactory()->createFromObjectWithDetails( 144 | current($response->list), 145 | $response->message, 146 | $response->length, 147 | $response->parts 148 | ); 149 | } 150 | 151 | public function scheduleSmss(ScheduleSmssBag $scheduleSmssBag): array 152 | { 153 | $scheduleSmssBag->to = implode(',', $scheduleSmssBag->to); 154 | $scheduleSmssBag->dateValidate = true; 155 | 156 | return array_map( 157 | [$this->dataFactoryProvider->provideSmsFactory(), 'createFromObject'], 158 | $this->makeRequest($scheduleSmssBag)->list 159 | ); 160 | } 161 | 162 | /** 163 | * @param ScheduleSmsBag $scheduleSmsBag 164 | * @return Sms 165 | * @throws SmsapiClientException 166 | */ 167 | public function scheduleFlashSms(ScheduleSmsBag $scheduleSmsBag): Sms 168 | { 169 | $scheduleSmsBag->dateValidate = true; 170 | $scheduleSmsBag->flash = true; 171 | 172 | return $this->dataFactoryProvider 173 | ->provideSmsFactory() 174 | ->createFromObject(current($this->makeRequest($scheduleSmsBag)->list)); 175 | } 176 | 177 | /** 178 | * @param ScheduleSmsToGroupBag $scheduleSmsToGroupBag 179 | * @return array 180 | * @throws SmsapiClientException 181 | */ 182 | public function scheduleSmsToGroup(ScheduleSmsToGroupBag $scheduleSmsToGroupBag): array 183 | { 184 | $scheduleSmsToGroupBag->dateValidate = true; 185 | 186 | return array_map( 187 | [$this->dataFactoryProvider->provideSmsFactory(), 'createFromObject'], 188 | $this->makeRequest($scheduleSmsToGroupBag)->list 189 | ); 190 | } 191 | 192 | /** 193 | * @param ScheduleSmsToGroupBag $sendSmsToGroupBag 194 | * @return array 195 | * @throws SmsapiClientException 196 | */ 197 | public function scheduleFlashSmsToGroup(ScheduleSmsToGroupBag $sendSmsToGroupBag): array 198 | { 199 | $sendSmsToGroupBag->dateValidate = true; 200 | $sendSmsToGroupBag->flash = true; 201 | 202 | return array_map( 203 | [$this->dataFactoryProvider->provideSmsFactory(), 'createFromObject'], 204 | $this->makeRequest($sendSmsToGroupBag)->list 205 | ); 206 | } 207 | 208 | /** 209 | * @param DeleteScheduledSmssBag $deleteScheduledSmsBag 210 | * @throws SmsapiClientException 211 | */ 212 | public function deleteScheduledSms(DeleteScheduledSmssBag $deleteScheduledSmsBag) 213 | { 214 | $deleteScheduledSmsBag->schDel = implode(',', $deleteScheduledSmsBag->smsIds); 215 | unset($deleteScheduledSmsBag->smsIds); 216 | $this->makeRequest($deleteScheduledSmsBag); 217 | } 218 | 219 | /** 220 | * @param $data 221 | * @return stdClass 222 | * @throws SmsapiClientException 223 | */ 224 | private function makeRequest($data): stdClass 225 | { 226 | return $this->requestExecutorFactory 227 | ->createLegacyRequestExecutor($this->externalHttpClient) 228 | ->request('sms.do', (array)$data); 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /src/Feature/Subusers/Bag/CreateSubuserBag.php: -------------------------------------------------------------------------------- 1 | null, 19 | 'password' => null, 20 | 'api_password' => null, 21 | ]; 22 | 23 | /** 24 | * @var array|null 25 | */ 26 | public $points = [ 27 | 'from_account' => null, 28 | 'per_month' => null, 29 | ]; 30 | 31 | public function __construct(string $username, string $panelPassword) 32 | { 33 | $this->credentials['username'] = $username; 34 | $this->credentials['password'] = $panelPassword; 35 | } 36 | 37 | public function setPoints(float $fromAccount, float $perMonth) 38 | { 39 | $this->points = ['from_account' => $fromAccount, 'per_month' => $perMonth]; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Feature/Subusers/Bag/DeleteSubuserBag.php: -------------------------------------------------------------------------------- 1 | id = $id; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Feature/Subusers/Bag/UpdateSubuserBag.php: -------------------------------------------------------------------------------- 1 | id = $id; 23 | } 24 | 25 | public function setPassword(string $panelPassword) 26 | { 27 | $this->credentials['password'] = $panelPassword; 28 | } 29 | 30 | public function setApiPassword(string $apiPassword) 31 | { 32 | $this->credentials['api_password'] = $apiPassword; 33 | } 34 | 35 | public function setPoints(float $fromAccount, float $perMonth) 36 | { 37 | $this->points = ['from_account' => $fromAccount, 'per_month' => $perMonth]; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Feature/Subusers/Data/Subuser.php: -------------------------------------------------------------------------------- 1 | subUserPointsFactory = $subUserPointsFactory; 20 | } 21 | 22 | public function createFromObject(stdClass $object): Subuser 23 | { 24 | $subuser = new Subuser(); 25 | $subuser->id = $object->id; 26 | $subuser->username = $object->username; 27 | $subuser->active = $object->active; 28 | $subuser->description = $object->description; 29 | $subuser->points = $this->subUserPointsFactory->createFromObject($object->points); 30 | 31 | return $subuser; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Feature/Subusers/SubusersFeature.php: -------------------------------------------------------------------------------- 1 | restRequestExecutor = $restRequestExecutor; 25 | $this->subuserFactory = $subuserFactory; 26 | } 27 | 28 | /** 29 | * @param CreateSubuserBag $createSubuser 30 | * @return Subuser 31 | * @throws SmsapiClientException 32 | */ 33 | public function createSubuser(CreateSubuserBag $createSubuser): Subuser 34 | { 35 | $result = $this->restRequestExecutor->create('subusers', (array)$createSubuser); 36 | 37 | return $this->subuserFactory->createFromObject($result); 38 | } 39 | 40 | /** 41 | * @return array 42 | * @throws SmsapiClientException 43 | */ 44 | public function findSubusers(): array 45 | { 46 | $result = $this->restRequestExecutor->read('subusers', []); 47 | 48 | return array_map([$this->subuserFactory, 'createFromObject'], $result->collection); 49 | } 50 | 51 | /** 52 | * @param DeleteSubuserBag $deleteSubuser 53 | * @throws SmsapiClientException 54 | */ 55 | public function deleteSubuser(DeleteSubuserBag $deleteSubuser) 56 | { 57 | $this->restRequestExecutor->delete(sprintf('subusers/%s', $deleteSubuser->id), []); 58 | } 59 | 60 | public function updateSubuser(UpdateSubuserBag $updateSubuser): Subuser 61 | { 62 | $subuserId = $updateSubuser->id; 63 | unset($updateSubuser->id); 64 | 65 | $result = $this->restRequestExecutor->update(sprintf('subusers/%s', $subuserId), (array)$updateSubuser); 66 | 67 | return $this->subuserFactory->createFromObject($result); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Feature/Vms/Bag/SendVmsBag.php: -------------------------------------------------------------------------------- 1 | to = $receiver; 29 | $this->tts = $text; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Feature/Vms/Data/Vms.php: -------------------------------------------------------------------------------- 1 | id = $object->id; 19 | $vms->points = (float)$object->points; 20 | $vms->status = $object->status; 21 | $vms->dateSent = new DateTime('@' . $object->date_sent); 22 | $vms->number = $object->number; 23 | 24 | return $vms; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Feature/Vms/VmsFeature.php: -------------------------------------------------------------------------------- 1 | legacyRequestExecutor = $legacyRequestExecutor; 29 | $this->vmsFactory = $vmsFactory; 30 | } 31 | 32 | /** 33 | * @param SendVmsBag $sendVmsBag 34 | * @return Vms 35 | * @throws SmsapiClientException 36 | */ 37 | public function sendVms(SendVmsBag $sendVmsBag): Vms 38 | { 39 | return $this->vmsFactory->createFromObject(current($this->makeRequest($sendVmsBag)->list)); 40 | } 41 | 42 | /** 43 | * @param $data 44 | * @return stdClass 45 | * @throws SmsapiClientException 46 | */ 47 | private function makeRequest($data): stdClass 48 | { 49 | return $this->legacyRequestExecutor->request('vms.do', (array)$data); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Infrastructure/HttpClient/ClientException.php: -------------------------------------------------------------------------------- 1 | request = $request; 22 | 23 | return $exception; 24 | } 25 | 26 | public function getRequest(): RequestInterface 27 | { 28 | return $this->request; 29 | } 30 | } -------------------------------------------------------------------------------- /src/Infrastructure/HttpClient/Decorator/AcceptJsonHeaderDecorator.php: -------------------------------------------------------------------------------- 1 | client = $client; 21 | } 22 | 23 | public function sendRequest(RequestInterface $request): ResponseInterface 24 | { 25 | $request = $this->addAcceptJsonHeader($request); 26 | 27 | return $this->client->sendRequest($request); 28 | } 29 | 30 | private function addAcceptJsonHeader(RequestInterface $request): RequestInterface 31 | { 32 | return $request->withHeader('Accept', 'application/json'); 33 | } 34 | } -------------------------------------------------------------------------------- /src/Infrastructure/HttpClient/Decorator/AuthorizationHeaderDecorator.php: -------------------------------------------------------------------------------- 1 | client = $client; 22 | $this->apiToken = $apiToken; 23 | } 24 | 25 | public function sendRequest(RequestInterface $request): ResponseInterface 26 | { 27 | $request = $this->addAuthorizationHeader($request); 28 | 29 | return $this->client->sendRequest($request); 30 | } 31 | 32 | private function addAuthorizationHeader(RequestInterface $request): RequestInterface 33 | { 34 | return $request->withHeader('Authorization', 'Bearer ' . $this->apiToken); 35 | } 36 | } -------------------------------------------------------------------------------- /src/Infrastructure/HttpClient/Decorator/BaseUriDecorator.php: -------------------------------------------------------------------------------- 1 | client = $client; 23 | $this->baseUri = $baseUri; 24 | } 25 | 26 | public function sendRequest(RequestInterface $request): ResponseInterface 27 | { 28 | $request = $this->prependBaseUri($request); 29 | 30 | return $this->client->sendRequest($request); 31 | } 32 | 33 | private function prependBaseUri(RequestInterface $request): RequestInterface 34 | { 35 | $uri = $request->getUri(); 36 | 37 | if (!filter_var($this->baseUri, FILTER_VALIDATE_URL)) { 38 | throw RequestException::withRequest("Invalid Base URI", $request); 39 | } 40 | 41 | $baseUriParts = parse_url($this->baseUri); 42 | 43 | $scheme = $baseUriParts['scheme'] ?? ''; 44 | $host = $baseUriParts['host'] ?? ''; 45 | $port = $baseUriParts['port'] ?? null; 46 | $basePath = $baseUriParts['path'] ?? ''; 47 | $basePath = rtrim($basePath, '/'); 48 | 49 | $uri = $uri->withPath($basePath . '/' . $uri->getPath()); 50 | $uri = $uri->withPort($port); 51 | $uri = $uri->withHost($host); 52 | $uri = $uri->withScheme($scheme); 53 | 54 | return $request->withUri($uri); 55 | } 56 | } -------------------------------------------------------------------------------- /src/Infrastructure/HttpClient/Decorator/CorrelationIdHeaderDecorator.php: -------------------------------------------------------------------------------- 1 | client = $client; 21 | } 22 | 23 | public function sendRequest(RequestInterface $request): ResponseInterface 24 | { 25 | $request = $this->addXRequestIdHeader($request); 26 | 27 | return $this->client->sendRequest($request); 28 | } 29 | 30 | private function addXRequestIdHeader(RequestInterface $request): RequestInterface 31 | { 32 | return $request->withHeader('X-Request-Id', $this->generateRequestId()); 33 | } 34 | 35 | private function generateRequestId(): string 36 | { 37 | return bin2hex(random_bytes(12)); 38 | } 39 | } -------------------------------------------------------------------------------- /src/Infrastructure/HttpClient/Decorator/HttpClientUserAgentHeaderDecorator.php: -------------------------------------------------------------------------------- 1 | client = $client; 22 | } 23 | 24 | public function sendRequest(RequestInterface $request): ResponseInterface 25 | { 26 | $request = $this->addUserAgentHeader($request); 27 | 28 | return $this->client->sendRequest($request); 29 | } 30 | 31 | private function addUserAgentHeader(RequestInterface $request): RequestInterface 32 | { 33 | return $request->withHeader('User-Agent', $this->createUserAgent()); 34 | } 35 | 36 | private function createUserAgent(): string 37 | { 38 | return sprintf( 39 | 'smsapi/php-client:%s;php:%s', 40 | SmsapiClient::VERSION, 41 | PHP_VERSION 42 | ); 43 | } 44 | } -------------------------------------------------------------------------------- /src/Infrastructure/HttpClient/Decorator/LoggerDecorator.php: -------------------------------------------------------------------------------- 1 | client = $client; 23 | $this->logger = $logger; 24 | } 25 | 26 | public function sendRequest(RequestInterface $request): ResponseInterface 27 | { 28 | $this->logger->info('Request', [ 29 | 'request' => $request, 30 | 'method' => $request->getMethod(), 31 | 'uri' => $request->getUri(), 32 | 'headers' => $request->getHeaders(), 33 | 'body' => $request->getBody()->getContents(), 34 | ]); 35 | 36 | $response = $this->client->sendRequest($request); 37 | 38 | $this->logger->info('Response', [ 39 | 'response' => $response, 40 | 'headers' => $response->getHeaders(), 41 | 'body' => $response->getBody()->getContents(), 42 | ]); 43 | 44 | return $response; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Infrastructure/HttpClient/HttpClientFactory.php: -------------------------------------------------------------------------------- 1 | logger = new NullLogger(); 29 | $this->apiToken = $apiToken; 30 | $this->uri = $uri; 31 | } 32 | 33 | public function createClient(ClientInterface $externalClient): ClientInterface 34 | { 35 | $client = new LoggerDecorator($externalClient, $this->logger); 36 | $client = new BaseUriDecorator($client, $this->uri); 37 | $client = new AuthorizationHeaderDecorator($client, $this->apiToken); 38 | $client = new HttpClientUserAgentHeaderDecorator($client); 39 | $client = new CorrelationIdHeaderDecorator($client); 40 | 41 | return $client; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Infrastructure/HttpClient/NetworkException.php: -------------------------------------------------------------------------------- 1 | method = $method; 28 | $this->uri = $uri; 29 | $this->body = $body; 30 | $this->headers = []; 31 | } 32 | 33 | public function getMethod(): string 34 | { 35 | return $this->method; 36 | } 37 | 38 | public function getUri(): string 39 | { 40 | return $this->uri; 41 | } 42 | 43 | public function getBody(): string 44 | { 45 | return $this->body; 46 | } 47 | 48 | public function getHeaders(): array 49 | { 50 | return $this->headers; 51 | } 52 | 53 | public function withHeader(string $headerName, string $headerValue): self 54 | { 55 | $this->headers[$headerName] = $headerValue; 56 | return $this; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Infrastructure/RequestAssembler/RequestAssembler.php: -------------------------------------------------------------------------------- 1 | requestFactory = $requestFactory; 23 | $this->streamFactory = $streamFactory; 24 | } 25 | 26 | public function assemble(Request $requestDTO): RequestInterface 27 | { 28 | $request = $this->requestFactory->createRequest( 29 | $requestDTO->getMethod(), 30 | $requestDTO->getUri() 31 | ); 32 | 33 | foreach ($requestDTO->getHeaders() as $header => $value) { 34 | $request = $request->withHeader($header, $value); 35 | } 36 | 37 | $request = $request->withBody($this->streamFactory->createStream($requestDTO->getBody())); 38 | 39 | return $request; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Infrastructure/RequestExecutor/LegacyRequestExecutor.php: -------------------------------------------------------------------------------- 1 | requestMapper = $requestMapper; 34 | $this->client = $client; 35 | $this->legacyResponseMapper = $legacyResponseMapper; 36 | $this->requestFactory = $requestFactory; 37 | $this->streamFactory = $streamFactory; 38 | } 39 | 40 | public function request(string $path, array $builtInParameters, array $userParameters = []): stdClass 41 | { 42 | $request = $this->requestMapper->map($path, $builtInParameters, $userParameters); 43 | 44 | $assembledRequest = (new RequestAssembler($this->requestFactory, $this->streamFactory))->assemble($request); 45 | 46 | $response = $this->client->sendRequest($assembledRequest); 47 | 48 | return $this->legacyResponseMapper->map($response); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Infrastructure/RequestExecutor/RequestExecutorFactory.php: -------------------------------------------------------------------------------- 1 | logger = new NullLogger(); 39 | $this->queryFormatter = new ComplexParametersQueryFormatter(); 40 | $this->jsonDecode = new JsonDecode(); 41 | $this->httpClientFactory = $httpClientFactory; 42 | $this->requestFactory = $requestFactory; 43 | $this->streamFactory = $streamFactory; 44 | } 45 | 46 | public function createRestRequestExecutor(ClientInterface $externalClient): RestRequestExecutor 47 | { 48 | $restRequestMapper = new RestRequestMapper($this->queryFormatter); 49 | $restResponseMapper = new RestResponseMapper($this->jsonDecode); 50 | $restResponseMapper->setLogger($this->logger); 51 | 52 | return new RestRequestExecutor( 53 | $restRequestMapper, 54 | $this->createHttpClient($externalClient), 55 | $restResponseMapper, 56 | $this->requestFactory, 57 | $this->streamFactory 58 | ); 59 | } 60 | 61 | public function createLegacyRequestExecutor(ClientInterface $externalClient): LegacyRequestExecutor 62 | { 63 | $legacyRequestMapper = new LegacyRequestMapper($this->queryFormatter); 64 | $legacyResponseMapper = new LegacyResponseMapper($this->jsonDecode); 65 | $legacyResponseMapper->setLogger($this->logger); 66 | 67 | return new LegacyRequestExecutor( 68 | $legacyRequestMapper, 69 | $this->createHttpClient($externalClient), 70 | $legacyResponseMapper, 71 | $this->requestFactory, 72 | $this->streamFactory 73 | ); 74 | } 75 | 76 | private function createHttpClient(ClientInterface $externalHttpClient): ClientInterface 77 | { 78 | $this->httpClientFactory->setLogger($this->logger); 79 | return $this->httpClientFactory->createClient($externalHttpClient); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/Infrastructure/RequestExecutor/RestRequestExecutor.php: -------------------------------------------------------------------------------- 1 | requestMapper = $requestMapper; 35 | $this->client = $client; 36 | $this->restResponseMapper = $restResponseMapper; 37 | $this->requestFactory = $requestFactory; 38 | $this->streamFactory = $streamFactory; 39 | } 40 | 41 | public function create(string $path, array $builtInParameters, array $userParameters = []): stdClass 42 | { 43 | $request = $this->requestMapper->mapCreate($path, $builtInParameters, $userParameters); 44 | 45 | return $this->sendRequestAndMapResponse($request); 46 | } 47 | 48 | public function read(string $path, array $builtInParameters, array $userParameters = []): stdClass 49 | { 50 | $request = $this->requestMapper->mapRead($path, $builtInParameters, $userParameters); 51 | 52 | return $this->sendRequestAndMapResponse($request); 53 | } 54 | 55 | public function delete(string $path, array $builtInParameters, array $userParameters = []) 56 | { 57 | $this->sendRequestAndMapResponse($this->requestMapper->mapDelete($path, $builtInParameters, $userParameters)); 58 | } 59 | 60 | public function update(string $path, array $builtInParameters, array $userParameters = []): stdClass 61 | { 62 | $request = $this->requestMapper->mapUpdate($path, $builtInParameters, $userParameters); 63 | 64 | return $this->sendRequestAndMapResponse($request); 65 | } 66 | 67 | public function info(string $path, array $builtInParameters, array $userParameters = []): stdClass 68 | { 69 | $request = $this->requestMapper->mapInfo($path, $builtInParameters, $userParameters); 70 | 71 | return $this->sendRequestAndMapResponse($request); 72 | } 73 | 74 | private function sendRequestAndMapResponse(Request $request): stdClass 75 | { 76 | $assembledRequest = (new RequestAssembler($this->requestFactory, $this->streamFactory))->assemble($request); 77 | 78 | $response = $this->client->sendRequest($assembledRequest); 79 | 80 | return $this->restResponseMapper->map($response); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/Infrastructure/RequestHttpMethod.php: -------------------------------------------------------------------------------- 1 | queryFormatter = $queryFormatter; 22 | } 23 | 24 | public function map(string $path, array $builtInParameters, array $userParameters = []): Request 25 | { 26 | $request = $this->createRequest(RequestHttpMethod::POST, $path, $builtInParameters, $userParameters); 27 | 28 | return $request->withHeader('Content-Type', 'application/x-www-form-urlencoded'); 29 | } 30 | 31 | private function createRequest( 32 | string $method, 33 | string $path, 34 | array $builtInParameters, 35 | array $userParameters 36 | ): Request { 37 | $parameters = new QueryParametersData(['format' => 'json'] + $builtInParameters, $userParameters); 38 | 39 | return new Request($method, $path, $this->queryFormatter->format($parameters)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Infrastructure/RequestMapper/Query/Formatter/BuiltInParametersQueryFormatter.php: -------------------------------------------------------------------------------- 1 | builtInParameters; 17 | } 18 | 19 | protected function formatParameterName(string $parameterName): string 20 | { 21 | return strtolower(preg_replace('#[A-Z]#', '_$0', $parameterName)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Infrastructure/RequestMapper/Query/Formatter/ComplexParametersQueryFormatter.php: -------------------------------------------------------------------------------- 1 | builtInParametersQueryFormatter = new BuiltInParametersQueryFormatter(); 24 | $this->userParametersQueryFormatter = new UserParametersQueryFormatter(); 25 | } 26 | 27 | public function format(QueryParametersData $parameters): string 28 | { 29 | $query = ''; 30 | 31 | if (!empty($parameters->builtInParameters)) { 32 | $query = $this->builtInParametersQueryFormatter->format($parameters); 33 | } 34 | 35 | if (!empty($parameters->userParameters)) { 36 | if (!empty($query)) { 37 | $query .= '&'; 38 | } 39 | $query .= $this->userParametersQueryFormatter->format($parameters); 40 | } 41 | 42 | return $query; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Infrastructure/RequestMapper/Query/Formatter/QueryFormatter.php: -------------------------------------------------------------------------------- 1 | getQueryParameters($parameters); 18 | $values = []; 19 | foreach ($data as $key => $value) { 20 | if ($value instanceof DateTimeInterface) { 21 | $value = $value->format('c'); 22 | } 23 | $values[$this->formatParameterName($key)] = $value; 24 | } 25 | 26 | return http_build_query($values, '', '&'); 27 | } 28 | 29 | abstract protected function getQueryParameters(QueryParametersData $parameters): array; 30 | 31 | abstract protected function formatParameterName(string $parameterName): string; 32 | } 33 | -------------------------------------------------------------------------------- /src/Infrastructure/RequestMapper/Query/Formatter/UserParametersQueryFormatter.php: -------------------------------------------------------------------------------- 1 | userParameters; 18 | } 19 | 20 | protected function formatParameterName(string $parameterName): string 21 | { 22 | return $parameterName; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Infrastructure/RequestMapper/Query/QueryParametersData.php: -------------------------------------------------------------------------------- 1 | builtInParameters = $builtInParameters; 22 | $this->userParameters = $userParameters; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Infrastructure/RequestMapper/RestRequestMapper.php: -------------------------------------------------------------------------------- 1 | queryFormatter = $queryFormatter; 21 | } 22 | 23 | public function mapCreate(string $path, array $builtInParameters, array $userParameters = []): Request 24 | { 25 | $request = $this->createRequest(RequestHttpMethod::POST, $path, $builtInParameters, $userParameters); 26 | 27 | return $request->withHeader('Content-Type', 'application/x-www-form-urlencoded'); 28 | } 29 | 30 | public function mapRead(string $path, array $builtInParameters, array $userParameters = []): Request 31 | { 32 | $path .= $this->createPathQuery($builtInParameters, $userParameters); 33 | 34 | return $this->createRequest(RequestHttpMethod::GET, $path, []); 35 | } 36 | 37 | public function mapDelete(string $path, array $builtInParameters, array $userParameters = []): Request 38 | { 39 | $path .= $this->createPathQuery($builtInParameters, $userParameters); 40 | 41 | return $this->createRequest(RequestHttpMethod::DELETE, $path, []); 42 | } 43 | 44 | public function mapUpdate(string $path, array $builtInParameters, array $userParameters = []): Request 45 | { 46 | $request = $this->createRequest(RequestHttpMethod::PUT, $path, $builtInParameters, $userParameters); 47 | 48 | return $request->withHeader('Content-Type', 'application/x-www-form-urlencoded'); 49 | } 50 | 51 | public function mapInfo(string $path, array $builtInParameters, array $userParameters): Request 52 | { 53 | $path .= $this->createPathQuery($builtInParameters, $userParameters); 54 | 55 | return $this->createRequest(RequestHttpMethod::HEAD, $path, []); 56 | } 57 | 58 | private function createPathQuery(array $builtInParameters, array $userParameters): string 59 | { 60 | $pathQuery = ''; 61 | if ($builtInParameters || $userParameters) { 62 | $parameters = new QueryParametersData($builtInParameters, $userParameters); 63 | $pathQuery .= '?' . $this->queryFormatter->format($parameters); 64 | } 65 | 66 | return $pathQuery; 67 | } 68 | 69 | private function createRequest( 70 | string $method, 71 | string $path, 72 | array $builtInParameters, 73 | array $userParameters = [] 74 | ): Request { 75 | $parameters = new QueryParametersData($builtInParameters, $userParameters); 76 | 77 | return new Request($method, $path, $this->queryFormatter->format($parameters)); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/Infrastructure/ResponseHttpCode.php: -------------------------------------------------------------------------------- 1 | error = $error; 20 | 21 | return $exception; 22 | } 23 | 24 | public static function withStatusCode($statusCode): self 25 | { 26 | return new self('Api error', $statusCode); 27 | } 28 | 29 | public static function withMessageAndError(string $message, int $error): self 30 | { 31 | $exception = new self($message); 32 | $exception->error = (string)$error; 33 | 34 | return $exception; 35 | } 36 | 37 | public static function withMessageAndStatusCode(string $message, int $statusCode): self 38 | { 39 | return new self($message, $statusCode); 40 | } 41 | 42 | public function getError(): string 43 | { 44 | return $this->error; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Infrastructure/ResponseMapper/JsonDecode.php: -------------------------------------------------------------------------------- 1 | json = $json; 20 | } 21 | 22 | public function getJson(): string 23 | { 24 | return $this->json; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Infrastructure/ResponseMapper/LegacyResponseMapper.php: -------------------------------------------------------------------------------- 1 | logger = new NullLogger(); 25 | $this->jsonDecode = $jsonDecode; 26 | } 27 | 28 | public function map(ResponseInterface $response): stdClass 29 | { 30 | $statusCode = $response->getStatusCode(); 31 | $contents = $response->getBody()->__toString(); 32 | 33 | if ($statusCode === ResponseHttpCode::OK) { 34 | $object = $this->jsonDecode->decode($contents); 35 | 36 | $this->logger->info('Decoded response', ['response' => $object]); 37 | 38 | if (isset($object->message, $object->error)) { 39 | throw ApiErrorException::withMessageAndError($object->message, $object->error); 40 | } 41 | 42 | return $object; 43 | } 44 | 45 | throw ApiErrorException::withStatusCode($statusCode); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Infrastructure/ResponseMapper/RestResponseMapper.php: -------------------------------------------------------------------------------- 1 | logger = new NullLogger(); 25 | $this->jsonDecode = $jsonDecode; 26 | } 27 | 28 | public function map(ResponseInterface $response): stdClass 29 | { 30 | $statusCode = $response->getStatusCode(); 31 | $contents = $response->getBody()->__toString(); 32 | 33 | if (in_array($statusCode, [ResponseHttpCode::OK, ResponseHttpCode::CREATED])) { 34 | $object = $this->jsonDecode->decode($contents); 35 | 36 | $this->logDecodedResponse($object); 37 | 38 | return $object; 39 | } elseif (in_array($statusCode, [ResponseHttpCode::ACCEPTED, ResponseHttpCode::NO_CONTENT])) { 40 | return new stdClass(); 41 | } elseif ($statusCode == ResponseHttpCode::SERVICE_UNAVAILABLE) { 42 | throw ApiErrorException::withMessageAndStatusCode('Service unavailable', $statusCode); 43 | } elseif ($statusCode == ResponseHttpCode::REQUEST_TIMEOUT) { 44 | throw ApiErrorException::withMessageAndStatusCode('Request timed out', $statusCode); 45 | } elseif ($contents) { 46 | $object = $this->jsonDecode->decode($contents); 47 | 48 | $this->logDecodedResponse($object); 49 | 50 | if (isset($object->message, $object->error)) { 51 | throw ApiErrorException::withMessageErrorAndStatusCode($object->message, $object->error, $statusCode); 52 | } 53 | } 54 | 55 | throw ApiErrorException::withStatusCode($statusCode); 56 | } 57 | 58 | private function logDecodedResponse(stdClass $object) 59 | { 60 | $this->logger->info('Decoded response', ['response' => $object]); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Service/HttpDefaultFeatures.php: -------------------------------------------------------------------------------- 1 | requestExecutorFactory->createRestRequestExecutor($this->externalHttpClient); 39 | $pingFactory = $this->dataFactoryProvider->providePingFactory(); 40 | 41 | return new PingHttpFeature($restRequestExecutor, $pingFactory); 42 | } 43 | 44 | public function smsFeature(): SmsFeature 45 | { 46 | return new SmsHttpFeature( 47 | $this->externalHttpClient, 48 | $this->requestExecutorFactory, 49 | $this->dataFactoryProvider 50 | ); 51 | } 52 | 53 | public function mfaFeature(): MfaFeature 54 | { 55 | $restRequestExecutor = $this->requestExecutorFactory->createRestRequestExecutor($this->externalHttpClient); 56 | $mfaFactory = $this->dataFactoryProvider->provideMfaFactory(); 57 | 58 | return new MfaHttpFeature($restRequestExecutor, $mfaFactory); 59 | } 60 | 61 | public function hlrFeature(): HlrFeature 62 | { 63 | return new HlrHttpFeature( 64 | $this->requestExecutorFactory->createLegacyRequestExecutor($this->externalHttpClient), 65 | $this->dataFactoryProvider->provideHlrFactory() 66 | ); 67 | } 68 | 69 | public function subusersFeature(): SubusersFeature 70 | { 71 | return new SubusersHttpFeature( 72 | $this->requestExecutorFactory->createRestRequestExecutor($this->externalHttpClient), 73 | $this->dataFactoryProvider->provideSubuserFactory() 74 | ); 75 | } 76 | 77 | public function shortUrlFeature(): ShortUrlFeature 78 | { 79 | return new ShortUlrHttpFeature( 80 | $this->requestExecutorFactory->createRestRequestExecutor($this->externalHttpClient), 81 | $this->dataFactoryProvider->provideShortUrlLinkFactory() 82 | ); 83 | } 84 | 85 | public function contactsFeature(): ContactsFeature 86 | { 87 | return new ContactsHttpFeature( 88 | $this->requestExecutorFactory->createRestRequestExecutor($this->externalHttpClient), 89 | $this->dataFactoryProvider 90 | ); 91 | } 92 | 93 | public function blacklistFeature(): BlacklistFeature 94 | { 95 | return new BlacklistHttpFeature( 96 | $this->requestExecutorFactory->createRestRequestExecutor($this->externalHttpClient), 97 | $this->dataFactoryProvider->provideBlacklistedPhoneNumberFactory() 98 | ); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/Service/SmsapiComHttpService.php: -------------------------------------------------------------------------------- 1 | externalHttpClient = $externalHttpClient; 30 | $this->requestExecutorFactory = $requestExecutorFactory; 31 | $this->dataFactoryProvider = $dataFactoryProvider; 32 | } 33 | 34 | public function profileFeature(): ProfileFeature 35 | { 36 | return new ProfileHttpFeature( 37 | $this->requestExecutorFactory->createRestRequestExecutor($this->externalHttpClient), 38 | $this->dataFactoryProvider 39 | ); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Service/SmsapiComService.php: -------------------------------------------------------------------------------- 1 | externalHttpClient = $externalHttpClient; 34 | $this->requestExecutorFactory = $requestExecutorFactory; 35 | $this->dataFactoryProvider = $dataFactoryProvider; 36 | } 37 | 38 | public function mmsFeature(): MmsFeature 39 | { 40 | return new MmsHttpFeature( 41 | $this->requestExecutorFactory->createLegacyRequestExecutor($this->externalHttpClient), 42 | $this->dataFactoryProvider->provideMmsFactory() 43 | ); 44 | } 45 | 46 | public function vmsFeature(): VmsFeature 47 | { 48 | return new VmsHttpFeature( 49 | $this->requestExecutorFactory->createLegacyRequestExecutor($this->externalHttpClient), 50 | $this->dataFactoryProvider->provideVmsFactory() 51 | ); 52 | } 53 | 54 | public function profileFeature(): SmsapiPlProfileFeature 55 | { 56 | return new SmsapiPlProfileHttpFeature( 57 | $this->requestExecutorFactory->createRestRequestExecutor($this->externalHttpClient), 58 | $this->dataFactoryProvider 59 | ); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Service/SmsapiPlService.php: -------------------------------------------------------------------------------- 1 | httpClient = $httpClient; 41 | $this->requestFactory = $requestFactory; 42 | $this->dataFactoryProvider = new DataFactoryProvider(); 43 | $this->logger = new NullLogger(); 44 | $this->streamFactory = $streamFactory; 45 | } 46 | 47 | public function smsapiPlService(string $apiToken): SmsapiPlService 48 | { 49 | return $this->smsapiPlServiceWithUri($apiToken, $this->smsapiPlUri); 50 | } 51 | 52 | public function smsapiPlServiceWithUri(string $apiToken, string $uri): SmsapiPlService 53 | { 54 | return new SmsapiPlHttpService( 55 | $this->httpClient, 56 | $this->createRequestExecutorFactory($apiToken, $uri), 57 | $this->dataFactoryProvider 58 | ); 59 | } 60 | 61 | public function smsapiComService(string $apiToken): SmsapiComService 62 | { 63 | return $this->smsapiComServiceWithUri($apiToken, $this->smsapiComUri); 64 | } 65 | 66 | public function smsapiComServiceWithUri(string $apiToken, string $uri): SmsapiComService 67 | { 68 | return new SmsapiComHttpService( 69 | $this->httpClient, 70 | $this->createRequestExecutorFactory($apiToken, $uri), 71 | $this->dataFactoryProvider 72 | ); 73 | } 74 | 75 | private function createRequestExecutorFactory(string $apiToken, string $uri): RequestExecutorFactory 76 | { 77 | $httpClientFactory = new HttpClientFactory($apiToken, $uri); 78 | $requestExecutorFactory = new RequestExecutorFactory($httpClientFactory, $this->requestFactory, $this->streamFactory); 79 | $requestExecutorFactory->setLogger($this->logger); 80 | 81 | return $requestExecutorFactory; 82 | } 83 | } 84 | --------------------------------------------------------------------------------