Skip to content

Commit

Permalink
Add and register handler send cloud.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Li authored and Bill Li committed Feb 19, 2018
1 parent ddee89a commit 9b0491a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
93 changes: 93 additions & 0 deletions src/Handlers/Sendcloud.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace AnomalyLab\LuminousSMS\Handlers;

use AnomalyLab\LuminousSMS\Exceptions\HandlerBadException;
use AnomalyLab\LuminousSMS\Contracts\MessagerInterface;
use AnomalyLab\LuminousSMS\Support\Arrays;
use AnomalyLab\LuminousSMS\Support\Stringy;

/**
* Class Sendcloud
*
* @link https://anomaly.ink
* @author Anomaly lab, Inc <[email protected]>
* @author Bill Li <[email protected]>
* @package AnomalyLab\LuminousSMS\Handlers\Sendcloud
*/
class Sendcloud extends Handler
{
use HasHttpRequest;

const REQUEST_TEMPLATE = 'http://www.sendcloud.net/smsapi/%s';

/**
* The handler name.
*
* @var string
*/
protected $name = 'sendcloud';

/**
* Seed message.
*
* The current drive service providers to implement push information content.
*
* @param \AnomalyLab\LuminousSMS\Contracts\MessagerInterface $messager
*
* @return array
*
* @throws \AnomalyLab\LuminousSMS\Exceptions\HandlerBadException;
*/
public function send(MessagerInterface $messager) : array
{
$params = [
'smsUser' => Arrays::get($this->config, 'sms_user'),
'templateId' => $message->getTemplate($this),
'phone' => is_array($to) ? implode(',', $to) : $to,
'vars' => $this->formatTemplateVars($message->getData($this)),
];

if ( $config->get('timestamp', false) )
{
$params['timestamp'] = time() * 1000;
}

$params['signature'] = $this->sign($params, Arrays::get($this->config, 'sms_key'));
$result = $this->post(sprintf(self::REQUEST_TEMPLATE, 'send'), $params);

if ( !$result['result'] )
{
throw new HandlerBadException($result['message'], $result['statusCode'], $result);
}
return $result;
}

/**
* @param array $vars
*
* @return string
*/
protected function formatTemplateVars(array $vars) : string
{
$formatted = [];
foreach ( $vars as $key => $value )
{
$formatted[sprintf('%%%s%%', trim($key, '%'))] = $value;
}

return json_encode($formatted, JSON_FORCE_OBJECT);
}

/**
* @param array $params
* @param string $key
*
* @return string
*/
protected function sign($params, $key)
{
ksort($params);
return md5(sprintf('%s&%s&%s', $key, urldecode(http_build_query($params)), $key));
}
}
4 changes: 3 additions & 1 deletion src/LuminousSms.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Handlers\Yunpian,
Handlers\Juhe,
Handlers\Alidayu,
Handlers\Sendcloud,
Handlers\Baidu
};

Expand Down Expand Up @@ -41,7 +42,8 @@ class LuminousSMS
'yunpian' => Yunpian::class,
'alidayu' => Alidayu::class,
'baidu' => Baidu::class,
'juhe' => Juhe::class
'juhe' => Juhe::class,
'sendcloud' => Sendcloud::class
];

/**
Expand Down

0 comments on commit 9b0491a

Please sign in to comment.