From 4a5d781e45a9bd7dd7dff4012c369f7ddff2b5cd Mon Sep 17 00:00:00 2001 From: Bill li Date: Thu, 21 Jun 2018 10:23:18 +0800 Subject: [PATCH 1/3] formatting --- src/Qcold/Qcloud.php | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/Qcold/Qcloud.php b/src/Qcold/Qcloud.php index 3500691..5ddb03c 100644 --- a/src/Qcold/Qcloud.php +++ b/src/Qcold/Qcloud.php @@ -3,6 +3,7 @@ namespace Ofcold\LuminousSMS\Qcold; use Ofcold\LuminousSMS\Handlers; +use Ofcold\LuminousSMS\Exceptions\MethodNotFoundException; /** * Class Qcloud @@ -52,6 +53,20 @@ class Qcloud extends Handlers ] ]; + /** + * The Signature instance. + * + * @var Signature + */ + protected static $signature; + + /** + * The Template instance. + * + * @var Templatye + */ + protected static $template; + /** * Send SMS to send. * @@ -63,28 +78,27 @@ class Qcloud extends Handlers */ public function send() : array { - return (new Sender($this)) - ->render(); + return Sender::render($this); } /** * Get the SignaTure instance. * - * @param string|null $method + * @param string $method * @param array $attributes * * @return mixed */ - public function getSignature(?string $method = null, ...$attributes) + public function getSignature(string $method, ...$attributes) { - $instance = new SignaTure($this); + $instance = static::$signature ?: new SignaTure($this); - if ( $method && method_exists($instance, $method) ) + if ( method_exists($instance, $method) ) { return $instance->$method(...$attributes); } - return $instance; + throw new MethodNotFoundException("Add {$method} does not exist object SignaTure"); } /** @@ -97,13 +111,13 @@ public function getSignature(?string $method = null, ...$attributes) */ public function getTemplate(?string $method = null, ...$attributes) { - $instance = new Template($this); + $instance = static::$template ?: new Template($this); - if ( $method && method_exists($instance, $method) ) + if ( method_exists($instance, $method) ) { return $instance->$method(...$attributes); } - return $instance; + throw new MethodNotFoundException("Add {$method} does not exist object Template"); } } \ No newline at end of file From bd7e3c941189021175bf6fca84a42f21cfa27db0 Mon Sep 17 00:00:00 2001 From: Bill li Date: Thu, 21 Jun 2018 10:23:28 +0800 Subject: [PATCH 2/3] update Sender.php --- src/Qcold/Sender.php | 61 ++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/src/Qcold/Sender.php b/src/Qcold/Sender.php index 734246e..b261006 100644 --- a/src/Qcold/Sender.php +++ b/src/Qcold/Sender.php @@ -2,6 +2,7 @@ namespace Ofcold\LuminousSMS\Qcold; +use Ofcold\LuminousSMS\Results; use Ofcold\LuminousSMS\Helpers; use Ofcold\LuminousSMS\Exceptions\HandlerBadException; @@ -21,46 +22,29 @@ */ class Sender { - /** - * The qcloud instance. - * - * @var Qcloud - */ - protected $qcloud; - - /** - * Create an a new Sender. - * - * @param Qcloud $qcloud - */ - public function __construct(Qcloud $qcloud) - { - $this->qcloud = $qcloud; - } - /** * Sender SMS * * @return mixed */ - public function render() + public static function render(Qcloud $qcloud) { - $method = method_exists($this, $this->qcloud->getMessage()->getType()) - ? $this->qcloud->getMessage()->getType() + $method = method_exists(__CLASS__, $qcloud->getMessage()->getType()) + ? $qcloud->getMessage()->getType() : 'text'; - $params = $this->$method(); + $params = static::$method($qcloud); // Set SMS flag. - if ( $sign = $this->qcloud->getMessage()->getSign() ) + if ( $sign = $qcloud->getMessage()->getSign() ) { $params['sign'] = $sign; } // Set the full mobile phone. $params['tel'] = [ - 'nationcode' => $this->qcloud->getMessage()->getCode(), - 'mobile' => $this->qcloud->getMessage()->getMobilePhone() + 'nationcode' => $qcloud->getMessage()->getCode(), + 'mobile' => $qcloud->getMessage()->getMobilePhone() ]; $params['time'] = time(); @@ -68,15 +52,15 @@ public function render() $random = Helpers::random(10); - $params['sig'] = $this->createSign($params, $random); + $params['sig'] = static::createSign($params, $random, $qcloud); - return Results::render($this->qcloud->request( + return Results::render($qcloud->request( 'post', sprintf( '%s%s?sdkappid=%s&random=%s', Qcloud::REQUEST_URL, - Qcloud::REQUEST_METHOD[$this->qcloud->getMessage()->getType()], - $this->qcloud->getConfig('app_id'), + Qcloud::REQUEST_METHOD[$qcloud->getMessage()->getType()], + $qcloud->getConfig('app_id'), $random ), [ @@ -94,13 +78,15 @@ public function render() * * Text SMS request body. * + * @param Qcloud $qcloud + * * @return array */ - protected function text() : array + protected static function text(Qcloud $qcloud) : array { return [ - 'type' => (int)($this->qcloud->getMessage()->getType() !== 'text'), - 'msg' => $this->qcloud->getMessage()->getContent(), + 'type' => (int)($qcloud->getMessage()->getType() !== 'text'), + 'msg' => $qcloud->getMessage()->getContent(), 'extend' => '' ]; } @@ -110,18 +96,20 @@ protected function text() : array * * Voice SMS request body. * + * @param Qcloud $qcloud + * * @return array */ - protected function voice() : array + protected static function voice(Qcloud $qcloud) : array { return [ - 'promptfile' => $this->qcloud->getMessage()->getContent(), + 'promptfile' => $qcloud->getMessage()->getContent(), 'prompttype' => 2, 'playtimes' => 2 ]; } - public function templateId() + public static function templateId(Qcloud $qcloud) { } @@ -131,10 +119,11 @@ public function templateId() * * @param array $params * @param string $random + * @param Qcloud $qcloud * * @return string */ - protected function createSign(array $params, string $random) : string + protected static function createSign(array $params, string $random, Qcloud $qcloud) : string { ksort($params); @@ -142,7 +131,7 @@ protected function createSign(array $params, string $random) : string 'sha256', sprintf( 'appkey=%s&random=%s&time=%s&mobile=%s', - $this->qcloud->getConfig('app_key'), + $qcloud->getConfig('app_key'), $random, $params['time'], $params['tel']['mobile'] From 8e58c69f03f9a72e8f59292aae26fa1dc43f401b Mon Sep 17 00:00:00 2001 From: Bill li Date: Thu, 21 Jun 2018 10:23:35 +0800 Subject: [PATCH 3/3] test --- test | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test b/test index 7b56344..d0c9483 100644 --- a/test +++ b/test @@ -12,15 +12,16 @@ $sms = new LuminousSMS(include __DIR__ . '/resources/config/sms.php'); // ------------------------------------------------------------------------------------------------- // Send Message. -// $result = $sms->sender(function($messager) { -// $messager -// ->setMobilePhone('18898726543') -// ->setType(Ofcold\LuminousSMS\Contracts\MessageInterface::VOICE_MESSAGE) -// ->setContent('您的验证码是{code}, 验证码将在2分钟后失效!请及时使用。') -// ->setPaserData(['code' => rand(1000, 9999)]); -// }); +$result = $sms->sender(function($messager) { + $messager + ->setMobilePhone('18898726543') + ->setType(Ofcold\LuminousSMS\Contracts\MessageInterface::VOICE_MESSAGE) + ->setContent('您的验证码是{code}, 验证码将在2分钟后失效!请及时使用。') + ->setPaserData(['code' => rand(1000, 9999)]); + var_dump($messager); +}); -// var_dump($result); +var_dump($result); // ------------------------------------------------------------------------------------------------- @@ -34,6 +35,6 @@ $sms = new LuminousSMS(include __DIR__ . '/resources/config/sms.php'); // Edit //$resuts = $sms->createHandler('qcloud')->getSignature('edit', '150986', '你是哪里来'); // -$resuts = $sms->createHandler('qcloud')->getSignature('remove', 150987); +// $resuts = $sms->createHandler('qcloud')->getSignature('remove', 150987); -var_dump($resuts); \ No newline at end of file +// var_dump($resuts); \ No newline at end of file