Skip to content

Commit

Permalink
Add NoGatewayAvailableException. resolve overtrue#25
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Jul 2, 2017
1 parent a6ae934 commit d9ada0d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
19 changes: 19 additions & 0 deletions src/Exceptions/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the overtrue/easy-sms.
* (c) overtrue <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Overtrue\EasySms\Exceptions;

/**
* Class Exception.
*
* @author overtrue <[email protected]>
*/
class Exception extends \Exception
{
}
5 changes: 3 additions & 2 deletions src/Exceptions/GatewayErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

namespace Overtrue\EasySms\Exceptions;

use Exception;

/**
* Class GatewayErrorException.
*/
class GatewayErrorException extends Exception
{
/**
* @var array
*/
public $raw = [];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
/**
* Class InvalidArgumentException.
*/
class InvalidArgumentException extends \InvalidArgumentException
class InvalidArgumentException extends Exception
{
}
38 changes: 38 additions & 0 deletions src/Exceptions/NoGatewayAvailableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the overtrue/easy-sms.
* (c) overtrue <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Overtrue\EasySms\Exceptions;

use Throwable;

/**
* Class NoGatewayAvailableException.
*
* @author overtrue <[email protected]>
*/
class NoGatewayAvailableException extends Exception
{
/**
* @var array
*/
public $results = [];

/**
* NoGatewayAvailableException constructor.
*
* @param array $results
* @param int $code
* @param \Throwable|null $previous
*/
public function __construct(array $results = [], $code = 0, Throwable $previous = null)
{
$this->results = $results;
parent::__construct('All the gateways have failed.', $code, $previous);
}
}
15 changes: 12 additions & 3 deletions src/Messenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Overtrue\EasySms\Contracts\MessageInterface;
use Overtrue\EasySms\Exceptions\GatewayErrorException;
use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
use Overtrue\EasySms\Support\Config;

/**
Expand Down Expand Up @@ -44,6 +45,8 @@ public function __construct(EasySms $easySms)
* @param array $gateways
*
* @return array
*
* @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
*/
public function send($to, $message, array $gateways = [])
{
Expand All @@ -61,12 +64,14 @@ public function send($to, $message, array $gateways = [])
$strategyAppliedGateways = $this->easySms->strategy()->apply($gateways);

$results = [];
$hasSucceed = false;
foreach ($strategyAppliedGateways as $gateway) {
try {
$results[$gateway] = [
'status' => self::STATUS_SUCCESS,
'result' => $this->easySms->gateway($gateway)->send($to, $message, new Config($gateways[$gateway])),
];
'status' => self::STATUS_SUCCESS,
'result' => $this->easySms->gateway($gateway)->send($to, $message, new Config($gateways[$gateway])),
];
$hasSucceed = true;
break;
} catch (GatewayErrorException $e) {
$results[$gateway] = [
Expand All @@ -77,6 +82,10 @@ public function send($to, $message, array $gateways = [])
}
}

if (!$hasSucceed) {
throw new NoGatewayAvailableException($results);
}

return $results;
}

Expand Down

0 comments on commit d9ada0d

Please sign in to comment.