This commit is contained in:
zhouyangyang 2022-04-10 00:14:41 +08:00
parent cb8f67ad9b
commit c6dab6b2a5
5 changed files with 39 additions and 36 deletions

View File

@ -31,7 +31,7 @@ class ConfigProvider
'publish' => [ 'publish' => [
[ [
'id' => 'config', 'id' => 'config',
'description' => 'The config for hyperf-ext/sms.', 'description' => 'The config for zyimm/sms.',
'source' => __DIR__ . '/../publish/sms.php', 'source' => __DIR__ . '/../publish/sms.php',
'destination' => BASE_PATH . '/config/autoload/sms.php', 'destination' => BASE_PATH . '/config/autoload/sms.php',
], ],

View File

@ -1,13 +1,7 @@
<?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\Events; namespace Zyimm\Sms\Events;
use Zyimm\Sms\Contracts\SmsableInterface; use Zyimm\Sms\Contracts\SmsableInterface;
@ -19,7 +13,7 @@ class SmsMessageSending
* *
* @var SmsableInterface * @var SmsableInterface
*/ */
public $smsable; public SmsableInterface $smsable;
/** /**
* Create a new event instance. * Create a new event instance.

View File

@ -13,7 +13,7 @@ class StrategicallySendMessageException extends RuntimeException
public function __construct($message, Throwable $throwable) public function __construct($message, Throwable $throwable)
{ {
parent::__construct($message, 0); parent::__construct($message, $throwable->getCode());
$this->appendStack($throwable); $this->appendStack($throwable);
} }

View File

@ -8,31 +8,37 @@ declare(strict_types=1);
* @contact eric@zhu.email * @contact eric@zhu.email
* @license https://github.com/hyperf-ext/sms/blob/master/LICENSE * @license https://github.com/hyperf-ext/sms/blob/master/LICENSE
*/ */
namespace Zyimm\Sms; namespace Zyimm\Sms;
use Hyperf\Utils\ApplicationContext; use Hyperf\Utils\ApplicationContext;
use Zyimm\Sms\Contract\HasMobileNumber; use HyperfExt\Contract\HasMobileNumber;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Zyimm\Sms\Contracts\MobileNumberInterface;
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;
class PendingSms class PendingSms
{ {
/** /**
* The "to" recipient of the message. * The "to" recipient of the message.
* *
* @var \Zyimm\Sms\Contracts\MobileNumberInterface * @var MobileNumberInterface
*/ */
protected $to; protected MobileNumberInterface $to;
/** /**
* @var \Zyimm\Sms\Contracts\SmsManagerInterface * @var SmsManagerInterface
*/ */
protected $manger; protected SmsManagerInterface $manger;
/** /**
* @var \Zyimm\Sms\Contracts\SenderInterface * @var SenderInterface
*/ */
protected $sender; protected SenderInterface $sender;
public function __construct(SmsManagerInterface $manger) public function __construct(SmsManagerInterface $manger)
{ {
@ -42,13 +48,13 @@ class PendingSms
/** /**
* Set the recipients of the message. * Set the recipients of the message.
* *
* @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
* @return $this * @return $this
* @throws InvalidMobileNumberException
*/ */
public function to($number, $defaultRegion = null) public function to($number, $defaultRegion = null): PendingSms
{ {
$number = $number instanceof HasMobileNumber ? $number->getMobileNumber() : $number; $number = $number instanceof HasMobileNumber ? $number->getMobileNumber() : $number;
@ -60,9 +66,12 @@ class PendingSms
/** /**
* Set the sender of the SMS message. * Set the sender of the SMS message.
* *
* @param string $name
* @return $this * @return $this
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
public function sender(string $name) public function sender(string $name): PendingSms
{ {
$this->sender = ApplicationContext::getContainer()->get(SmsManagerInterface::class)->get($name); $this->sender = ApplicationContext::getContainer()->get(SmsManagerInterface::class)->get($name);
@ -77,6 +86,18 @@ class PendingSms
return $this->manger->sendNow($this->fill($smsable)); return $this->manger->sendNow($this->fill($smsable));
} }
/**
* Populate the SMS message with the addresses.
*/
protected function fill(SmsableInterface $smsable): SmsableInterface
{
$smsable->to($this->to);
if ($this->sender) {
$smsable->sender($this->sender->getName());
}
return $smsable;
}
/** /**
* Send a new SMS message instance. * Send a new SMS message instance.
* *
@ -102,16 +123,4 @@ class PendingSms
{ {
return $this->manger->later($this->fill($smsable), $delay, $queue); return $this->manger->later($this->fill($smsable), $delay, $queue);
} }
/**
* Populate the SMS message with the addresses.
*/
protected function fill(SmsableInterface $smsable): SmsableInterface
{
$smsable->to($this->to);
if ($this->sender) {
$smsable->sender($this->sender->getName());
}
return $smsable;
}
} }

View File

@ -16,9 +16,9 @@ use Zyimm\Sms\Contracts\SmsableInterface;
class QueuedSmsableJob extends Job class QueuedSmsableJob extends Job
{ {
/** /**
* @var \Zyimm\Sms\Contracts\SmsableInterface * @var SmsableInterface
*/ */
public $smsable; public SmsableInterface $smsable;
public function __construct(SmsableInterface $smsable) public function __construct(SmsableInterface $smsable)
{ {