Skip to content

Commit

Permalink
Move formatGateways to EasySMS
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Aug 18, 2018
1 parent 2136a97 commit 3c8b552
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 74 deletions.
100 changes: 66 additions & 34 deletions src/EasySms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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);
}
}
42 changes: 2 additions & 40 deletions src/Messenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 3c8b552

Please sign in to comment.