This commit is contained in:
zhouyangyang 2022-04-10 16:46:07 +08:00
parent c6dab6b2a5
commit aeb3d5a743
5 changed files with 52 additions and 26 deletions

View File

@ -451,7 +451,7 @@ Sms::to($request->user())
如果你希望你的短信类始终使用队列,您可以给短信消息类实现 `Zyimm\Contract\ShouldQueue` 接口,现在即使你调用了 `send` 方法,短信依旧使用队列的方式发送。另外,如果需要将短信推送到指定队列,可以设置在短信消息类中设置 `queue` 属性。 如果你希望你的短信类始终使用队列,您可以给短信消息类实现 `Zyimm\Contract\ShouldQueue` 接口,现在即使你调用了 `send` 方法,短信依旧使用队列的方式发送。另外,如果需要将短信推送到指定队列,可以设置在短信消息类中设置 `queue` 属性。
```php ```php
use Zyimm\Contract\ShouldQueue; use HyperfExt\Contract\ShouldQueue;
use zyimm\Sms\Smsable; use zyimm\Sms\Smsable;
class VerificationCode extends Smsable implements ShouldQueue class VerificationCode extends Smsable implements ShouldQueue

View File

@ -10,7 +10,7 @@ declare(strict_types=1);
*/ */
namespace %NAMESPACE%; namespace %NAMESPACE%;
use Zyimm\Sms\Contract\ShouldQueue; use HyperfExt\Contract\ShouldQueue;;
use Zyimm\Sms\Contracts\SenderInterface; use Zyimm\Sms\Contracts\SenderInterface;
use Zyimm\Sms\Smsable; use Zyimm\Sms\Smsable;

View File

@ -1,17 +1,12 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
/**
* This file is part of hyperf-ext/sms.
*
* @link https://github.com/hyperf-ext/sms
* @contact eric@zhu.email
* @license https://github.com/hyperf-ext/sms/blob/master/LICENSE
*/
namespace Zyimm\Sms\Exceptions; namespace Zyimm\Sms\Exceptions;
use InvalidArgumentException; use InvalidArgumentException;
class InvalidMobileNumberException extends InvalidArgumentException class InvalidMobileNumberException extends InvalidArgumentException
{ {
} }

View File

@ -12,6 +12,8 @@ namespace Zyimm\Sms;
use Hyperf\Utils\ApplicationContext; use Hyperf\Utils\ApplicationContext;
use HyperfExt\Contract\HasMailAddress; use HyperfExt\Contract\HasMailAddress;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Zyimm\Sms\Contracts\SmsManagerInterface; use Zyimm\Sms\Contracts\SmsManagerInterface;
/** /**
@ -19,6 +21,10 @@ use Zyimm\Sms\Contracts\SmsManagerInterface;
*/ */
class Sms class Sms
{ {
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public static function __callStatic(string $method, array $args) public static function __callStatic(string $method, array $args)
{ {
$instance = static::getManager(); $instance = static::getManager();
@ -26,11 +32,19 @@ class Sms
return $instance->{$method}(...$args); return $instance->{$method}(...$args);
} }
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public static function sender(string $name): PendingSms public static function sender(string $name): PendingSms
{ {
return (new PendingSms(static::getManager()))->sender($name); return (new PendingSms(static::getManager()))->sender($name);
} }
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected static function getManager() protected static function getManager()
{ {
return ApplicationContext::getContainer()->get(SmsManagerInterface::class); return ApplicationContext::getContainer()->get(SmsManagerInterface::class);

View File

@ -1,20 +1,18 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
/**
* This file is part of hyperf-ext/sms.
*
* @link https://github.com/hyperf-ext/sms
* @contact eric@zhu.email
* @license https://github.com/hyperf-ext/sms/blob/master/LICENSE
*/
namespace Zyimm\Sms; namespace Zyimm\Sms;
use Hyperf\Contract\ConfigInterface; use Hyperf\Contract\ConfigInterface;
use Zyimm\Sms\Contract\ShouldQueue; use HyperfExt\Contract\HasMobileNumber;
use HyperfExt\Contract\ShouldQueue;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Zyimm\Sms\Contracts\SenderInterface; use Zyimm\Sms\Contracts\SenderInterface;
use Zyimm\Sms\Contracts\SmsableInterface; use Zyimm\Sms\Contracts\SmsableInterface;
use Zyimm\Sms\Contracts\SmsManagerInterface; use Zyimm\Sms\Contracts\SmsManagerInterface;
use Zyimm\Sms\Exceptions\InvalidMobileNumberException;
use Zyimm\Sms\Exceptions\StrategicallySendMessageException; use Zyimm\Sms\Exceptions\StrategicallySendMessageException;
use InvalidArgumentException; use InvalidArgumentException;
use LogicException; use LogicException;
@ -26,16 +24,16 @@ class SmsManager implements SmsManagerInterface
/** /**
* The container instance. * The container instance.
* *
* @var \Psr\Container\ContainerInterface * @var ContainerInterface
*/ */
protected $container; protected ContainerInterface $container;
/** /**
* The array of resolved senders. * The array of resolved senders.
* *
* @var \Zyimm\Sms\Contracts\SenderInterface[] * @var SenderInterface[]
*/ */
protected $senders = []; protected array $senders = [];
/** /**
* The config instance. * The config instance.
@ -46,6 +44,10 @@ class SmsManager implements SmsManagerInterface
/** /**
* Create a new Mail manager instance. * Create a new Mail manager instance.
*
* @param ContainerInterface $container
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container)
{ {
@ -81,9 +83,18 @@ class SmsManager implements SmsManagerInterface
throw $exception; throw $exception;
} }
/**
* send
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function send(SmsableInterface $smsable) public function send(SmsableInterface $smsable)
{ {
if ($smsable instanceof ShouldQueue) { if ($smsable instanceof ShouldQueue) {
/**
* @var Smsable|ShouldQueue $smsable
*/
return $smsable->queue(); return $smsable->queue();
} }
@ -101,24 +112,30 @@ class SmsManager implements SmsManagerInterface
} }
/** /**
* @param \Zyimm\Sms\Contract\HasMobileNumber|string $number * @param HasMobileNumber|string $number
* @param null|int|string $defaultRegion * @param null|int|string $defaultRegion
* *
* @throws \Zyimm\Sms\Exceptions\InvalidMobileNumberException * @throws InvalidMobileNumberException
*/ */
public function to($number, $defaultRegion = null): PendingSms public function to($number, $defaultRegion = null): PendingSms
{ {
return (new PendingSms($this))->to($number, $defaultRegion); return (new PendingSms($this))->to($number, $defaultRegion);
} }
/**
* applyStrategy
*
* @param SmsableInterface $smsable
* @return array
*/
protected function applyStrategy(SmsableInterface $smsable): array protected function applyStrategy(SmsableInterface $smsable): array
{ {
$senders = (is_array($smsable->senders) && count($smsable->senders) > 0) $senders = (is_array($smsable->senders) && count($smsable->senders) > 0)
? $smsable->senders ? $smsable->senders
: ( : (
is_array($this->config['default']['senders']) is_array($this->config['default']['senders'])
? $this->config['default']['senders'] ? $this->config['default']['senders']
: [$this->config['default']['senders']] : [$this->config['default']['senders']]
); );
if (empty($senders)) { if (empty($senders)) {