From 3c8b552de0bf29a03ad997dc34736be2c75792f2 Mon Sep 17 00:00:00 2001 From: overtrue Date: Sat, 18 Aug 2018 13:12:36 +0800 Subject: [PATCH] Move formatGateways to EasySMS --- src/EasySms.php | 100 ++++++++++++++++++++++++++++++---------------- src/Messenger.php | 42 +------------------ 2 files changed, 68 insertions(+), 74 deletions(-) diff --git a/src/EasySms.php b/src/EasySms.php index 9910f76..4c7da1e 100644 --- a/src/EasySms.php +++ b/src/EasySms.php @@ -86,43 +86,13 @@ public function send($to, $message, array $gateways = []) { $to = $this->formatPhoneNumber($to); $message = $this->formatMessage($message); + $gateways = empty($gateways) ? $message->getGateways() : $gateways; - return $this->getMessenger()->send($to, $message, $gateways); - } - - /** - * @param string|\Overtrue\EasySms\Contracts\PhoneNumberInterface $number - * - * @return \Overtrue\EasySms\PhoneNumber - */ - protected function formatPhoneNumber($number) - { - if ($number instanceof PhoneNumberInterface) { - return $number; - } - - return new PhoneNumber(trim($number)); - } - - /** - * @param array|string|\Overtrue\EasySms\Contracts\MessageInterface $message - * - * @return \Overtrue\EasySms\Contracts\MessageInterface - */ - protected function formatMessage($message) - { - if (!($message instanceof MessageInterface)) { - if (!is_array($message)) { - $message = [ - 'content' => strval($message), - 'template' => strval($message), - ]; - } - - $message = new Message($message); + if (empty($gateways)) { + $gateways = $this->config->get('default.gateways', []); } - return $message; + return $this->getMessenger()->send($to, $message, $this->formatGateways($gateways)); } /** @@ -311,4 +281,66 @@ protected function callCustomCreator($gateway) { return call_user_func($this->customCreators[$gateway], $this->config->get("gateways.{$gateway}", [])); } + + /** + * @param string|\Overtrue\EasySms\Contracts\PhoneNumberInterface $number + * + * @return \Overtrue\EasySms\PhoneNumber + */ + protected function formatPhoneNumber($number) + { + if ($number instanceof PhoneNumberInterface) { + return $number; + } + + return new PhoneNumber(trim($number)); + } + + /** + * @param array|string|\Overtrue\EasySms\Contracts\MessageInterface $message + * + * @return \Overtrue\EasySms\Contracts\MessageInterface + */ + protected function formatMessage($message) + { + if (!($message instanceof MessageInterface)) { + if (!is_array($message)) { + $message = [ + 'content' => strval($message), + 'template' => strval($message), + ]; + } + + $message = new Message($message); + } + + return $message; + } + + /** + * @param array $gateways + * + * @return array + * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException + */ + protected function formatGateways(array $gateways) + { + $formatted = []; + + foreach ($gateways as $gateway => $setting) { + if (is_int($gateway) && is_string($setting)) { + $gateway = $setting; + $setting = []; + } + + $formatted[$gateway] = $setting; + $globalSettings = $this->config->get("gateways.{$gateway}", []); + + if (is_string($gateway) && !empty($globalSettings) && is_array($setting)) { + $formatted[$gateway] = array_merge($globalSettings, $setting); + } + } + + return $this->strategy()->apply($formatted); + } } diff --git a/src/Messenger.php b/src/Messenger.php index 3d287cd..bf46010 100644 --- a/src/Messenger.php +++ b/src/Messenger.php @@ -49,25 +49,14 @@ public function __construct(EasySms $easySms) * * @return array * - * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException * @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException */ public function send(PhoneNumberInterface $to, MessageInterface $message, array $gateways = []) { - if (empty($gateways)) { - $gateways = $message->getGateways(); - } - - if (empty($gateways)) { - $gateways = $this->easySms->getConfig()->get('default.gateways', []); - } - - $gateways = $this->formatGateways($gateways); - $strategyAppliedGateways = $this->easySms->strategy()->apply($gateways); - $results = []; $isSuccessful = false; - foreach ($strategyAppliedGateways as $gateway) { + + foreach ($gateways as $gateway) { try { $results[$gateway] = [ 'gateway' => $gateway, @@ -98,31 +87,4 @@ public function send(PhoneNumberInterface $to, MessageInterface $message, array return $results; } - - /** - * @param array $gateways - * - * @return array - */ - protected function formatGateways(array $gateways) - { - $formatted = []; - $config = $this->easySms->getConfig(); - - foreach ($gateways as $gateway => $setting) { - if (is_int($gateway) && is_string($setting)) { - $gateway = $setting; - $setting = []; - } - - $formatted[$gateway] = $setting; - $globalSetting = $config->get("gateways.{$gateway}", []); - - if (is_string($gateway) && !empty($globalSetting) && is_array($setting)) { - $formatted[$gateway] = array_merge($globalSetting, $setting); - } - } - - return $formatted; - } }