From 5697a451bf3f8bea93f965439824c74694e9c71c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 9 Sep 2024 15:42:24 +0200 Subject: [PATCH 1/4] feat: get all codes --- src/Utopia/Messaging/Adapter/SMS/GEOSMS/CallingCode.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Utopia/Messaging/Adapter/SMS/GEOSMS/CallingCode.php b/src/Utopia/Messaging/Adapter/SMS/GEOSMS/CallingCode.php index 35e8fe0d..1a2f4047 100644 --- a/src/Utopia/Messaging/Adapter/SMS/GEOSMS/CallingCode.php +++ b/src/Utopia/Messaging/Adapter/SMS/GEOSMS/CallingCode.php @@ -573,6 +573,11 @@ class CallingCode self::ZIMBABWE => true, ]; + public static function getAll(): array + { + return array_keys(self::CODES); + } + public static function fromPhoneNumber(string $number): ?string { $digits = str_replace(['+', ' ', '(', ')', '-'], '', $number); From c1c4c2d720c8e9512d0e706544998726c4c00118 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 10 Sep 2024 10:11:46 +0200 Subject: [PATCH 2/4] fix: improve --- composer.lock | 91 +-- src/Utopia/Messaging/Adapter.php | 25 - src/Utopia/Messaging/Adapter/SMS/GEOSMS.php | 125 ---- .../Adapter/SMS/GEOSMS/CallingCode.php | 600 ----------------- .../Adapter/SMS/Util/CallingCode.php | 601 ++++++++++++++++++ 5 files changed, 647 insertions(+), 795 deletions(-) delete mode 100644 src/Utopia/Messaging/Adapter/SMS/GEOSMS.php delete mode 100644 src/Utopia/Messaging/Adapter/SMS/GEOSMS/CallingCode.php create mode 100644 src/Utopia/Messaging/Adapter/SMS/Util/CallingCode.php diff --git a/composer.lock b/composer.lock index 85062ad6..e7accb70 100644 --- a/composer.lock +++ b/composer.lock @@ -172,20 +172,20 @@ }, { "name": "symfony/polyfill-mbstring", - "version": "v1.29.0", + "version": "v1.31.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "9773676c8a1bb1f8d4340a62efe641cf76eda7ec" + "reference": "85181ba99b2345b0ef10ce42ecac37612d9fd341" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/9773676c8a1bb1f8d4340a62efe641cf76eda7ec", - "reference": "9773676c8a1bb1f8d4340a62efe641cf76eda7ec", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/85181ba99b2345b0ef10ce42ecac37612d9fd341", + "reference": "85181ba99b2345b0ef10ce42ecac37612d9fd341", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.2" }, "provide": { "ext-mbstring": "*" @@ -232,7 +232,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.29.0" + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.31.0" }, "funding": [ { @@ -248,7 +248,7 @@ "type": "tidelift" } ], - "time": "2024-01-29T20:11:03+00:00" + "time": "2024-09-09T11:45:10+00:00" } ], "packages-dev": [ @@ -320,16 +320,16 @@ }, { "name": "myclabs/deep-copy", - "version": "1.11.1", + "version": "1.12.0", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "7284c22080590fb39f2ffa3e9057f10a4ddd0e0c" + "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/7284c22080590fb39f2ffa3e9057f10a4ddd0e0c", - "reference": "7284c22080590fb39f2ffa3e9057f10a4ddd0e0c", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", + "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", "shasum": "" }, "require": { @@ -337,11 +337,12 @@ }, "conflict": { "doctrine/collections": "<1.6.8", - "doctrine/common": "<2.13.3 || >=3,<3.2.2" + "doctrine/common": "<2.13.3 || >=3 <3.2.2" }, "require-dev": { "doctrine/collections": "^1.6.8", "doctrine/common": "^2.13.3 || ^3.2.2", + "phpspec/prophecy": "^1.10", "phpunit/phpunit": "^7.5.20 || ^8.5.23 || ^9.5.13" }, "type": "library", @@ -367,7 +368,7 @@ ], "support": { "issues": "https://github.com/myclabs/DeepCopy/issues", - "source": "https://github.com/myclabs/DeepCopy/tree/1.11.1" + "source": "https://github.com/myclabs/DeepCopy/tree/1.12.0" }, "funding": [ { @@ -375,20 +376,20 @@ "type": "tidelift" } ], - "time": "2023-03-08T13:26:56+00:00" + "time": "2024-06-12T14:39:25+00:00" }, { "name": "nikic/php-parser", - "version": "v5.0.2", + "version": "v5.1.0", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "139676794dc1e9231bf7bcd123cfc0c99182cb13" + "reference": "683130c2ff8c2739f4822ff7ac5c873ec529abd1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/139676794dc1e9231bf7bcd123cfc0c99182cb13", - "reference": "139676794dc1e9231bf7bcd123cfc0c99182cb13", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/683130c2ff8c2739f4822ff7ac5c873ec529abd1", + "reference": "683130c2ff8c2739f4822ff7ac5c873ec529abd1", "shasum": "" }, "require": { @@ -399,7 +400,7 @@ }, "require-dev": { "ircmaxell/php-yacc": "^0.0.7", - "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0" + "phpunit/phpunit": "^9.0" }, "bin": [ "bin/php-parse" @@ -431,9 +432,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.0.2" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.1.0" }, - "time": "2024-03-05T20:51:40+00:00" + "time": "2024-07-01T20:03:41+00:00" }, { "name": "phar-io/manifest", @@ -617,32 +618,32 @@ }, { "name": "phpunit/php-code-coverage", - "version": "10.1.14", + "version": "10.1.16", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "e3f51450ebffe8e0efdf7346ae966a656f7d5e5b" + "reference": "7e308268858ed6baedc8704a304727d20bc07c77" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/e3f51450ebffe8e0efdf7346ae966a656f7d5e5b", - "reference": "e3f51450ebffe8e0efdf7346ae966a656f7d5e5b", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/7e308268858ed6baedc8704a304727d20bc07c77", + "reference": "7e308268858ed6baedc8704a304727d20bc07c77", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-xmlwriter": "*", - "nikic/php-parser": "^4.18 || ^5.0", + "nikic/php-parser": "^4.19.1 || ^5.1.0", "php": ">=8.1", - "phpunit/php-file-iterator": "^4.0", - "phpunit/php-text-template": "^3.0", - "sebastian/code-unit-reverse-lookup": "^3.0", - "sebastian/complexity": "^3.0", - "sebastian/environment": "^6.0", - "sebastian/lines-of-code": "^2.0", - "sebastian/version": "^4.0", - "theseer/tokenizer": "^1.2.0" + "phpunit/php-file-iterator": "^4.1.0", + "phpunit/php-text-template": "^3.0.1", + "sebastian/code-unit-reverse-lookup": "^3.0.0", + "sebastian/complexity": "^3.2.0", + "sebastian/environment": "^6.1.0", + "sebastian/lines-of-code": "^2.0.2", + "sebastian/version": "^4.0.1", + "theseer/tokenizer": "^1.2.3" }, "require-dev": { "phpunit/phpunit": "^10.1" @@ -654,7 +655,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-main": "10.1-dev" + "dev-main": "10.1.x-dev" } }, "autoload": { @@ -683,7 +684,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/10.1.14" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/10.1.16" }, "funding": [ { @@ -691,7 +692,7 @@ "type": "github" } ], - "time": "2024-03-12T15:33:41+00:00" + "time": "2024-08-22T04:31:57+00:00" }, { "name": "phpunit/php-file-iterator", @@ -1207,16 +1208,16 @@ }, { "name": "sebastian/comparator", - "version": "5.0.1", + "version": "5.0.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "2db5010a484d53ebf536087a70b4a5423c102372" + "reference": "2d3e04c3b4c1e84a5e7382221ad8883c8fbc4f53" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/2db5010a484d53ebf536087a70b4a5423c102372", - "reference": "2db5010a484d53ebf536087a70b4a5423c102372", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/2d3e04c3b4c1e84a5e7382221ad8883c8fbc4f53", + "reference": "2d3e04c3b4c1e84a5e7382221ad8883c8fbc4f53", "shasum": "" }, "require": { @@ -1227,7 +1228,7 @@ "sebastian/exporter": "^5.0" }, "require-dev": { - "phpunit/phpunit": "^10.3" + "phpunit/phpunit": "^10.4" }, "type": "library", "extra": { @@ -1272,7 +1273,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/comparator/issues", "security": "https://github.com/sebastianbergmann/comparator/security/policy", - "source": "https://github.com/sebastianbergmann/comparator/tree/5.0.1" + "source": "https://github.com/sebastianbergmann/comparator/tree/5.0.2" }, "funding": [ { @@ -1280,7 +1281,7 @@ "type": "github" } ], - "time": "2023-08-14T13:18:12+00:00" + "time": "2024-08-12T06:03:08+00:00" }, { "name": "sebastian/complexity", @@ -2018,5 +2019,5 @@ "platform-overrides": { "php": "8.2" }, - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.6.0" } diff --git a/src/Utopia/Messaging/Adapter.php b/src/Utopia/Messaging/Adapter.php index 250fbf39..d12a07c3 100644 --- a/src/Utopia/Messaging/Adapter.php +++ b/src/Utopia/Messaging/Adapter.php @@ -3,7 +3,6 @@ namespace Utopia\Messaging; use Exception; -use libphonenumber\PhoneNumberUtil; abstract class Adapter { @@ -251,28 +250,4 @@ protected function requestMulti( return $responses; } - - - /** - * @param string $phone - * @return int|null - * @throws Exception - */ - public function getCountryCode(string $phone): ?int - { - if (empty($phone)) { - throw new Exception('$phone cannot be empty.'); - } - - $helper = PhoneNumberUtil::getInstance(); - - try { - return $helper - ->parse($phone) - ->getCountryCode(); - - } catch (\Throwable $th) { - throw new Exception("Error parsing phone: " . $th->getMessage()); - } - } } diff --git a/src/Utopia/Messaging/Adapter/SMS/GEOSMS.php b/src/Utopia/Messaging/Adapter/SMS/GEOSMS.php deleted file mode 100644 index b1955ca5..00000000 --- a/src/Utopia/Messaging/Adapter/SMS/GEOSMS.php +++ /dev/null @@ -1,125 +0,0 @@ - - */ - protected array $localAdapters = []; - - public function __construct(SMSAdapter $defaultAdapter) - { - $this->defaultAdapter = $defaultAdapter; - } - - public function getName(): string - { - return static::NAME; - } - - public function getMaxMessagesPerRequest(): int - { - return PHP_INT_MAX; - } - - public function setLocal(string $callingCode, SMSAdapter $adapter): self - { - $this->localAdapters[$callingCode] = $adapter; - - return $this; - } - - /** - * @return array - */ - protected function filterCallingCodesByAdapter(SMSAdapter $adapter): array - { - $result = []; - - foreach ($this->localAdapters as $callingCode => $localAdapter) { - if ($localAdapter === $adapter) { - $result[] = $callingCode; - } - } - - return $result; - } - - /** - * @return array>}> - */ - protected function process(SMS $message): array - { - $results = []; - $recipients = $message->getTo(); - - do { - [$nextRecipients, $nextAdapter] = $this->getNextRecipientsAndAdapter($recipients); - - try { - $results[$nextAdapter->getName()] = $nextAdapter->send( - new SMS( - to: $nextRecipients, - content: $message->getContent(), - from: $message->getFrom(), - attachments: $message->getAttachments() - ) - ); - } catch (\Exception $e) { - $results[$nextAdapter->getName()] = [ - 'type' => 'error', - 'message' => $e->getMessage(), - ]; - } - - $recipients = \array_diff($recipients, $nextRecipients); - } while (count($recipients) > 0); - - return $results; - } - - /** - * @param array $recipients - * @return array|SMSAdapter> - */ - protected function getNextRecipientsAndAdapter(array $recipients): array - { - $nextRecipients = []; - $nextAdapter = null; - - foreach ($recipients as $recipient) { - $adapter = $this->getAdapterByPhoneNumber($recipient); - - if ($nextAdapter === null || $adapter === $nextAdapter) { - $nextAdapter = $adapter; - $nextRecipients[] = $recipient; - } - } - - return [$nextRecipients, $nextAdapter]; - } - - protected function getAdapterByPhoneNumber(?string $phoneNumber): SMSAdapter - { - $callingCode = CallingCode::fromPhoneNumber($phoneNumber); - if (empty($callingCode)) { - return $this->defaultAdapter; - } - - if (isset($this->localAdapters[$callingCode])) { - return $this->localAdapters[$callingCode]; - } - - return $this->defaultAdapter; - } -} diff --git a/src/Utopia/Messaging/Adapter/SMS/GEOSMS/CallingCode.php b/src/Utopia/Messaging/Adapter/SMS/GEOSMS/CallingCode.php deleted file mode 100644 index 1a2f4047..00000000 --- a/src/Utopia/Messaging/Adapter/SMS/GEOSMS/CallingCode.php +++ /dev/null @@ -1,600 +0,0 @@ - true, - self::ANDORRA => true, - self::ANGOLA => true, - self::ARGENTINA => true, - self::ARMENIA => true, - self::ARUBA => true, - self::AUSTRALIA => true, - self::AUSTRIA => true, - self::AZERBAIJAN => true, - self::BAHRAIN => true, - self::BANGLADESH => true, - self::BELARUS => true, - self::BELGIUM => true, - self::BELIZE => true, - self::BENIN => true, - self::BHUTAN => true, - self::BOLIVIA => true, - self::BOSNIA_HERZEGOVINA => true, - self::BOTSWANA => true, - self::BRAZIL => true, - self::BRUNEI => true, - self::BULGARIA => true, - self::BURKINA_FASO => true, - self::BURUNDI => true, - self::CAMBODIA => true, - self::CAMEROON => true, - self::CAPE_VERDE_ISLANDS => true, - self::CENTRAL_AFRICAN_REPUBLIC => true, - self::CHILE => true, - self::CHINA => true, - self::COLOMBIA => true, - self::COMOROS_AND_MAYOTTE => true, - self::CONGO => true, - self::COOK_ISLANDS => true, - self::COSTA_RICA => true, - self::CROATIA => true, - self::CUBA => true, - self::CYPRUS => true, - self::CZECH_REPUBLIC => true, - self::DENMARK => true, - self::DJIBOUTI => true, - self::ECUADOR => true, - self::EGYPT => true, - self::EL_SALVADOR => true, - self::EQUATORIAL_GUINEA => true, - self::ERITREA => true, - self::ESTONIA => true, - self::ETHIOPIA => true, - self::FALKLAND_ISLANDS => true, - self::FAROE_ISLANDS => true, - self::FIJI => true, - self::FINLAND => true, - self::FRANCE => true, - self::FRENCH_GUIANA => true, - self::FRENCH_POLYNESIA => true, - self::GABON => true, - self::GAMBIA => true, - self::GEORGIA => true, - self::GERMANY => true, - self::GHANA => true, - self::GIBRALTAR => true, - self::GREECE => true, - self::GREENLAND => true, - self::GUADELOUPE => true, - self::GUAM => true, - self::GUATEMALA => true, - self::GUINEA => true, - self::GUINEA_BISSAU => true, - self::GUYANA => true, - self::HAITI => true, - self::HONDURAS => true, - self::HONG_KONG => true, - self::HUNGARY => true, - self::ICELAND => true, - self::INDIA => true, - self::INDONESIA => true, - self::IRAN => true, - self::IRAQ => true, - self::IRELAND => true, - self::ISRAEL => true, - self::ITALY => true, - self::JAPAN => true, - self::JORDAN => true, - self::KENYA => true, - self::KIRIBATI => true, - self::NORTH_KOREA => true, - self::SOUTH_KOREA => true, - self::KUWAIT => true, - self::KYRGYZSTAN => true, - self::LAOS => true, - self::LATVIA => true, - self::LEBANON => true, - self::LESOTHO => true, - self::LIBERIA => true, - self::LIBYA => true, - self::LIECHTENSTEIN => true, - self::LITHUANIA => true, - self::LUXEMBOURG => true, - self::MACAO => true, - self::MACEDONIA => true, - self::MADAGASCAR => true, - self::MALAWI => true, - self::MALAYSIA => true, - self::MALDIVES => true, - self::MALI => true, - self::MALTA => true, - self::MARSHALL_ISLANDS => true, - self::MARTINIQUE => true, - self::MAURITANIA => true, - self::MEXICO => true, - self::MICRONESIA => true, - self::MOLDOVA => true, - self::MONACO => true, - self::MONGOLIA => true, - self::MOROCCO => true, - self::MOZAMBIQUE => true, - self::MYANMAR => true, - self::NAMIBIA => true, - self::NAURU => true, - self::NEPAL => true, - self::NETHERLANDS => true, - self::NEW_CALEDONIA => true, - self::NEW_ZEALAND => true, - self::NICARAGUA => true, - self::NIGER => true, - self::NIGERIA => true, - self::NIUE => true, - self::NORFOLK_ISLANDS => true, - self::NORTHERN_MARIANA_ISLANDS => true, - self::NORWAY => true, - self::OMAN => true, - self::PALAU => true, - self::PANAMA => true, - self::PAPUA_NEW_GUINEA => true, - self::PARAGUAY => true, - self::PERU => true, - self::PHILIPPINES => true, - self::POLAND => true, - self::PORTUGAL => true, - self::QATAR => true, - self::REUNION => true, - self::ROMANIA => true, - self::RUSSIA_KAZAKHSTAN_UZBEKISTAN_TURKMENISTAN_AND_TAJIKSTAN => true, - self::RWANDA => true, - self::SAN_MARINO => true, - self::SAO_TOME_AND_PRINCIPE => true, - self::SAUDI_ARABIA => true, - self::SENEGAL => true, - self::SERBIA => true, - self::SEYCHELLES => true, - self::SIERRA_LEONE => true, - self::SINGAPORE => true, - self::SLOVAK_REPUBLIC => true, - self::SLOVENIA => true, - self::SOLOMON_ISLANDS => true, - self::SOMALIA => true, - self::SOUTH_AFRICA => true, - self::SPAIN => true, - self::SRI_LANKA => true, - self::ST_HELENA => true, - self::SUDAN => true, - self::SURINAME => true, - self::SWAZILAND => true, - self::SWEDEN => true, - self::SWITZERLAND => true, - self::SYRIA => true, - self::TAIWAN => true, - self::THAILAND => true, - self::TOGO => true, - self::TONGA => true, - self::TUNISIA => true, - self::TURKEY => true, - self::TUVALU => true, - self::UGANDA => true, - self::UKRAINE => true, - self::UNITED_ARAB_EMIRATES => true, - self::UNITED_KINGDOM => true, - self::URUGUAY => true, - self::NORTH_AMERICA => true, - self::VANUATU => true, - self::VENEZUELA => true, - self::VIETNAM => true, - self::WALLIS_AND_FUTUNA => true, - self::YEMEN => true, - self::ZAMBIA => true, - self::ZANZIBAR => true, - self::ZIMBABWE => true, - ]; - - public static function getAll(): array - { - return array_keys(self::CODES); - } - - public static function fromPhoneNumber(string $number): ?string - { - $digits = str_replace(['+', ' ', '(', ')', '-'], '', $number); - - // Remove international call prefix, usually `00` or `011` - // https://en.wikipedia.org/wiki/List_of_international_call_prefixes - $digits = preg_replace('/^00|^011/', '', $digits); - - // Prefixes can be 3, 2, or 1 digits long - // Attempt to match the longest first - foreach ([3, 2, 1] as $length) { - $code = substr($digits, 0, $length); - if (isset(self::CODES[$code])) { - return $code; - } - } - - return null; - } -} diff --git a/src/Utopia/Messaging/Adapter/SMS/Util/CallingCode.php b/src/Utopia/Messaging/Adapter/SMS/Util/CallingCode.php new file mode 100644 index 00000000..660f2d79 --- /dev/null +++ b/src/Utopia/Messaging/Adapter/SMS/Util/CallingCode.php @@ -0,0 +1,601 @@ + true, + self::ANDORRA => true, + self::ANGOLA => true, + self::ARGENTINA => true, + self::ARMENIA => true, + self::ARUBA => true, + self::AUSTRALIA => true, + self::AUSTRIA => true, + self::AZERBAIJAN => true, + self::BAHRAIN => true, + self::BANGLADESH => true, + self::BELARUS => true, + self::BELGIUM => true, + self::BELIZE => true, + self::BENIN => true, + self::BHUTAN => true, + self::BOLIVIA => true, + self::BOSNIA_HERZEGOVINA => true, + self::BOTSWANA => true, + self::BRAZIL => true, + self::BRUNEI => true, + self::BULGARIA => true, + self::BURKINA_FASO => true, + self::BURUNDI => true, + self::CAMBODIA => true, + self::CAMEROON => true, + self::CAPE_VERDE_ISLANDS => true, + self::CENTRAL_AFRICAN_REPUBLIC => true, + self::CHILE => true, + self::CHINA => true, + self::COLOMBIA => true, + self::COMOROS_AND_MAYOTTE => true, + self::CONGO => true, + self::COOK_ISLANDS => true, + self::COSTA_RICA => true, + self::CROATIA => true, + self::CUBA => true, + self::CYPRUS => true, + self::CZECH_REPUBLIC => true, + self::DENMARK => true, + self::DJIBOUTI => true, + self::ECUADOR => true, + self::EGYPT => true, + self::EL_SALVADOR => true, + self::EQUATORIAL_GUINEA => true, + self::ERITREA => true, + self::ESTONIA => true, + self::ETHIOPIA => true, + self::FALKLAND_ISLANDS => true, + self::FAROE_ISLANDS => true, + self::FIJI => true, + self::FINLAND => true, + self::FRANCE => true, + self::FRENCH_GUIANA => true, + self::FRENCH_POLYNESIA => true, + self::GABON => true, + self::GAMBIA => true, + self::GEORGIA => true, + self::GERMANY => true, + self::GHANA => true, + self::GIBRALTAR => true, + self::GREECE => true, + self::GREENLAND => true, + self::GUADELOUPE => true, + self::GUAM => true, + self::GUATEMALA => true, + self::GUINEA => true, + self::GUINEA_BISSAU => true, + self::GUYANA => true, + self::HAITI => true, + self::HONDURAS => true, + self::HONG_KONG => true, + self::HUNGARY => true, + self::ICELAND => true, + self::INDIA => true, + self::INDONESIA => true, + self::IRAN => true, + self::IRAQ => true, + self::IRELAND => true, + self::ISRAEL => true, + self::ITALY => true, + self::JAPAN => true, + self::JORDAN => true, + self::KENYA => true, + self::KIRIBATI => true, + self::NORTH_KOREA => true, + self::SOUTH_KOREA => true, + self::KUWAIT => true, + self::KYRGYZSTAN => true, + self::LAOS => true, + self::LATVIA => true, + self::LEBANON => true, + self::LESOTHO => true, + self::LIBERIA => true, + self::LIBYA => true, + self::LIECHTENSTEIN => true, + self::LITHUANIA => true, + self::LUXEMBOURG => true, + self::MACAO => true, + self::MACEDONIA => true, + self::MADAGASCAR => true, + self::MALAWI => true, + self::MALAYSIA => true, + self::MALDIVES => true, + self::MALI => true, + self::MALTA => true, + self::MARSHALL_ISLANDS => true, + self::MARTINIQUE => true, + self::MAURITANIA => true, + self::MEXICO => true, + self::MICRONESIA => true, + self::MOLDOVA => true, + self::MONACO => true, + self::MONGOLIA => true, + self::MOROCCO => true, + self::MOZAMBIQUE => true, + self::MYANMAR => true, + self::NAMIBIA => true, + self::NAURU => true, + self::NEPAL => true, + self::NETHERLANDS => true, + self::NEW_CALEDONIA => true, + self::NEW_ZEALAND => true, + self::NICARAGUA => true, + self::NIGER => true, + self::NIGERIA => true, + self::NIUE => true, + self::NORFOLK_ISLANDS => true, + self::NORTH_AMERICA => true, + self::NORTHERN_MARIANA_ISLANDS => true, + self::NORWAY => true, + self::OMAN => true, + self::PALAU => true, + self::PANAMA => true, + self::PAPUA_NEW_GUINEA => true, + self::PARAGUAY => true, + self::PERU => true, + self::PHILIPPINES => true, + self::POLAND => true, + self::PORTUGAL => true, + self::QATAR => true, + self::REUNION => true, + self::ROMANIA => true, + self::RUSSIA_KAZAKHSTAN_UZBEKISTAN_TURKMENISTAN_AND_TAJIKSTAN => true, + self::RWANDA => true, + self::SAN_MARINO => true, + self::SAO_TOME_AND_PRINCIPE => true, + self::SAUDI_ARABIA => true, + self::SENEGAL => true, + self::SERBIA => true, + self::SEYCHELLES => true, + self::SIERRA_LEONE => true, + self::SINGAPORE => true, + self::SLOVAK_REPUBLIC => true, + self::SLOVENIA => true, + self::SOLOMON_ISLANDS => true, + self::SOMALIA => true, + self::SOUTH_AFRICA => true, + self::SPAIN => true, + self::SRI_LANKA => true, + self::ST_HELENA => true, + self::SUDAN => true, + self::SURINAME => true, + self::SWAZILAND => true, + self::SWEDEN => true, + self::SWITZERLAND => true, + self::SYRIA => true, + self::TAIWAN => true, + self::THAILAND => true, + self::TOGO => true, + self::TONGA => true, + self::TUNISIA => true, + self::TURKEY => true, + self::TUVALU => true, + self::UGANDA => true, + self::UKRAINE => true, + self::UNITED_ARAB_EMIRATES => true, + self::UNITED_KINGDOM => true, + self::URUGUAY => true, + self::VANUATU => true, + self::VENEZUELA => true, + self::VIETNAM => true, + self::WALLIS_AND_FUTUNA => true, + self::YEMEN => true, + self::ZAMBIA => true, + self::ZANZIBAR => true, + self::ZIMBABWE => true, + ]; + + public static function getAll(): array + { + return array_keys(self::CODES); + } + + /** + * @param string $phone + * @return int|null + * @throws Exception + */ + public function getCallingCode(string $phone): ?int + { + if (empty($phone)) { + throw new Exception('$phone cannot be empty.'); + } + + $helper = PhoneNumberUtil::getInstance(); + + try { + return $helper + ->parse($phone) + ->getCountryCode(); + + } catch (\Throwable $th) { + throw new Exception("Error parsing phone: " . $th->getMessage()); + } + } +} From 028039edc40248fc28434dbcdc7f5c3b52a0996f Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 10 Sep 2024 10:16:25 +0200 Subject: [PATCH 3/4] test: fixes --- .../Adapter/SMS/Util/CallingCode.php | 2 +- .../Adapter/SMS/GEOSMS/CallingCodeTest.php | 2 +- tests/Messaging/Adapter/SMS/GEOSMSTest.php | 114 ------------------ 3 files changed, 2 insertions(+), 116 deletions(-) delete mode 100644 tests/Messaging/Adapter/SMS/GEOSMSTest.php diff --git a/src/Utopia/Messaging/Adapter/SMS/Util/CallingCode.php b/src/Utopia/Messaging/Adapter/SMS/Util/CallingCode.php index 660f2d79..b3bf5270 100644 --- a/src/Utopia/Messaging/Adapter/SMS/Util/CallingCode.php +++ b/src/Utopia/Messaging/Adapter/SMS/Util/CallingCode.php @@ -581,7 +581,7 @@ public static function getAll(): array * @return int|null * @throws Exception */ - public function getCallingCode(string $phone): ?int + public static function fromPhoneNumber(string $phone): ?int { if (empty($phone)) { throw new Exception('$phone cannot be empty.'); diff --git a/tests/Messaging/Adapter/SMS/GEOSMS/CallingCodeTest.php b/tests/Messaging/Adapter/SMS/GEOSMS/CallingCodeTest.php index cd1321be..41cc085f 100644 --- a/tests/Messaging/Adapter/SMS/GEOSMS/CallingCodeTest.php +++ b/tests/Messaging/Adapter/SMS/GEOSMS/CallingCodeTest.php @@ -2,7 +2,7 @@ namespace Utopia\Tests\Adapter\SMS\GEOSMS; -use Utopia\Messaging\Adapter\SMS\GEOSMS\CallingCode; +use Utopia\Messaging\Adapter\SMS\Util\CallingCode; use Utopia\Tests\Adapter\Base; class CallingCodeTest extends Base diff --git a/tests/Messaging/Adapter/SMS/GEOSMSTest.php b/tests/Messaging/Adapter/SMS/GEOSMSTest.php deleted file mode 100644 index 82633336..00000000 --- a/tests/Messaging/Adapter/SMS/GEOSMSTest.php +++ /dev/null @@ -1,114 +0,0 @@ -createMock(SMSAdapter::class); - $defaultAdapterMock->method('getName')->willReturn('default'); - $defaultAdapterMock->method('send')->willReturn(['results' => [['status' => 'success']]]); - - $adapter = new GEOSMS($defaultAdapterMock); - - $to = ['+11234567890']; - $from = 'Sender'; - - $message = new SMS( - to: $to, - content: 'Test Content', - from: $from - ); - - $result = $adapter->send($message); - - $this->assertEquals(1, count($result)); - $this->assertEquals('success', $result['default']['results'][0]['status']); - } - - public function testSendSMSUsingLocalAdapter(): void - { - $defaultAdapterMock = $this->createMock(SMSAdapter::class); - $localAdapterMock = $this->createMock(SMSAdapter::class); - $localAdapterMock->method('getName')->willReturn('local'); - $localAdapterMock->method('send')->willReturn(['results' => [['status' => 'success']]]); - - $adapter = new GEOSMS($defaultAdapterMock); - $adapter->setLocal(CallingCode::INDIA, $localAdapterMock); - - $to = ['+911234567890']; - $from = 'Sender'; - - $message = new SMS( - to: $to, - content: 'Test Content', - from: $from - ); - - $result = $adapter->send($message); - - $this->assertEquals(1, count($result)); - $this->assertEquals('success', $result['local']['results'][0]['status']); - } - - public function testSendSMSUsingLocalAdapterAndDefault(): void - { - $defaultAdapterMock = $this->createMock(SMSAdapter::class); - $defaultAdapterMock->method('getName')->willReturn('default'); - $defaultAdapterMock->method('send')->willReturn(['results' => [['status' => 'success']]]); - $localAdapterMock = $this->createMock(SMSAdapter::class); - $localAdapterMock->method('getName')->willReturn('local'); - $localAdapterMock->method('send')->willReturn(['results' => [['status' => 'success']]]); - - $adapter = new GEOSMS($defaultAdapterMock); - $adapter->setLocal(CallingCode::INDIA, $localAdapterMock); - - $to = ['+911234567890', '+11234567890']; - $from = 'Sender'; - - $message = new SMS( - to: $to, - content: 'Test Content', - from: $from - ); - - $result = $adapter->send($message); - - $this->assertEquals(2, count($result)); - $this->assertEquals('success', $result['local']['results'][0]['status']); - $this->assertEquals('success', $result['default']['results'][0]['status']); - } - - public function testSendSMSUsingGroupedLocalAdapter(): void - { - $defaultAdapterMock = $this->createMock(SMSAdapter::class); - $localAdapterMock = $this->createMock(SMSAdapter::class); - $localAdapterMock->method('getName')->willReturn('local'); - $localAdapterMock->method('send')->willReturn(['results' => [['status' => 'success']]]); - - $adapter = new GEOSMS($defaultAdapterMock); - $adapter->setLocal(CallingCode::INDIA, $localAdapterMock); - $adapter->setLocal(CallingCode::NORTH_AMERICA, $localAdapterMock); - - $to = ['+911234567890', '+11234567890']; - $from = 'Sender'; - - $message = new SMS( - to: $to, - content: 'Test Content', - from: $from - ); - - $result = $adapter->send($message); - - $this->assertEquals(1, count($result)); - $this->assertEquals('success', $result['local']['results'][0]['status']); - } -} From f5ca94c9d38dcd16932a1280b0da97831bc86c50 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 10 Sep 2024 10:20:05 +0200 Subject: [PATCH 4/4] fix tests --- tests/Messaging/Adapter/SMS/SMSTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Messaging/Adapter/SMS/SMSTest.php b/tests/Messaging/Adapter/SMS/SMSTest.php index 9ce9058e..848b034a 100644 --- a/tests/Messaging/Adapter/SMS/SMSTest.php +++ b/tests/Messaging/Adapter/SMS/SMSTest.php @@ -32,7 +32,5 @@ public function testSendSMS(): void $this->assertEquals('POST', $smsRequest['method']); $this->assertEquals('+987654321', $smsRequest['data']['from']); $this->assertEquals('+123456789', $smsRequest['data']['to']); - $this->assertEquals(98, $sender->getCountryCode($smsRequest['data']['from'])); - $this->assertEquals(1, $sender->getCountryCode($smsRequest['data']['to'])); } }