Skip to content

Commit

Permalink
增加异常处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaoming committed Dec 23, 2019
1 parent b6f54ef commit 2122ab8
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 14 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 lushaoming
Copyright (c) 2019 Bryce

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 10 additions & 3 deletions src/Bryce/OpenApi/BaseApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class BaseApi
{
protected $params = [];
protected $clientKey;
protected $clientKey = '';

public function __construct()
{
Expand All @@ -21,15 +21,22 @@ public function __construct()
];
}

public function setClientId($clientId)
public function setClientId(string $clientId)
{
$this->params['client_id'] = $clientId;
}

public function setClientKey($key)
public function setClientKey(string $key)
{
$this->clientKey = $key;
}

public function setDebug(bool $debug)
{
$this->params['debug'] = (int)$debug;
}




}
14 changes: 14 additions & 0 deletions src/Bryce/OpenApi/Common/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Define all error codes
* @author Bryce<lushaoming6@gmail.com>
* @date 2019/12/13
*/
namespace Bryce\OpenApi\Common;

class Error
{
const _OK = 0;
const _SIGNATURE_ERROR = -401;
const _MISSING_PARAM = -402;
}
11 changes: 9 additions & 2 deletions src/Bryce/OpenApi/Common/Tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
namespace Bryce\OpenApi\Common;

use Bryce\OpenApi\Exception\SignatureException;

class Tools
{

Expand Down Expand Up @@ -39,14 +41,16 @@ public static function createNonce($length = 16): string


/**
* @param array $data Data involved in signing
* @param string $key Client secret, must be kept secret
* @param array $data Data involved in signing
* @param string $key Client secret, must be kept secret
* @return string
* @throws SignatureException
* @author Bryce<lushaoming6@gmail.com>
* @date 2019/12/12
*/
public static function sign(array $data, string $key): string
{
if (empty($key)) throw new SignatureException('Signature error, missing key', Error::_SIGNATURE_ERROR);
if (isset($data['sign'])) unset($data['sign']);

ksort($data);
Expand Down Expand Up @@ -85,12 +89,14 @@ public static function checkCommonParams(array $params): bool
*/
public static function checkSmsParams($params = []): bool
{
if (!self::checkCommonParams($params)) return false;
if (!isset($params['mobile'])) return false;
return true;
}

public static function checkSmsVerifyData($params = []): bool
{
if (!self::checkCommonParams($params)) return false;
if (!isset($params['mobile']) || !isset($params['code'])) return false;

return true;
Expand All @@ -104,6 +110,7 @@ public static function checkSmsVerifyData($params = []): bool
*/
public static function checkEmailParams($params = []): bool
{
if (!self::checkCommonParams($params)) return false;
if (!isset($params['to']) || !isset($params['subject']) || !isset($params['body'])) return false;
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bryce/OpenApi/Exception/MailException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Class ${NAME}
* Class MailException
* @author Bryce<lushaoming6@gmail.com>
* @date 2019/12/12
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Bryce/OpenApi/Exception/SignatureException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Class SignatureException
* @author Bryce<lushaoming6@gmail.com>
* @date 2019/12/13
*/
namespace Bryce\OpenApi\Exception;

class SignatureException extends \Exception
{

}
26 changes: 24 additions & 2 deletions src/Bryce/OpenApi/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,24 @@
namespace Bryce\OpenApi;

use Bryce\OpenApi\Common\Config;
use Bryce\OpenApi\Common\Error;
use Bryce\OpenApi\Common\Tools;
use Bryce\OpenApi\Exception\MailException;

class Mailer extends BaseApi
{
public function send(string $to, string $subject, string $body, $from = null)
/**
* @param string $to
* @param string $subject
* @param string $body
* @param string $from
* @return bool
* @throws MailException
* @throws Exception\SignatureException
* @author Bryce<lushaoming6@gmail.com>
* @date 2019/12/13
*/
public function send(string $to, string $subject, string $body, string $from = null)
{
$this->params['to'] = $to;
$this->params['subject'] = $subject;
Expand All @@ -20,10 +33,19 @@ public function send(string $to, string $subject, string $body, $from = null)

$this->params['sign'] = Tools::sign($this->params, $this->clientKey);

if (!Tools::checkEmailParams($this->params)) {
throw new MailException('Missing required parameters, please check the interface documentation', Error::_MISSING_PARAM);
}

$res = Tools::curlPost(Config::CONFIGS['send_email_url'], $this->params);

$res = Tools::dealCurlResult($res);

return $res->status === 0;
if ($res->status == Error::_OK) {
return true;
} else {
throw new MailException($res->msg, $res->status);
return false;
}
}
}
40 changes: 35 additions & 5 deletions src/Bryce/OpenApi/Sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@
namespace Bryce\OpenApi;

use Bryce\OpenApi\Common\Config;
use Bryce\OpenApi\Common\Error;
use Bryce\OpenApi\Common\Tools;
use Bryce\OpenApi\Exception\SmsException;

class Sms extends BaseApi
{
/**
* Send a text message.
* One phone number can only send one message in one minute and five messages in one day
* Only support Chinese phone number (+86)
* @param string $mobile
* @return bool
* @throws Exception\SignatureException
* @throws SmsException
* @author Bryce<lushaoming6@gmail.com>
* @date 2019/12/14
*/
public function send(string $mobile)
{
$this->params['mobile'] = $mobile;
Expand All @@ -21,21 +34,38 @@ public function send(string $mobile)

$res = Tools::dealCurlResult($res);

return $res->status === 0;
if ($res->status == Error::_OK) {
return true;
} else {
throw new SmsException($res->msg, $res->status);
}
}

public function verifyCode(string $mobile, $code)
/**
* If the code does not expire, you can verify it multiple times during the validity period.
* So you should verify whether the code is reused.
* @param string $mobile
* @param int $code
* @return bool
* @throws Exception\SignatureException
* @throws SmsException
* @author Bryce<lushaoming6@gmail.com>
* @date 2019/12/14
*/
public function verifyCode(string $mobile, int $code)
{
$this->params['mobile'] = $mobile;
$this->params['code'] = $code;

$this->params['sign'] = Tools::sign($this->params, $this->clientKey);
var_dump($this->params);
$res = Tools::curlPost(Config::CONFIGS['verify_sms_code_url'], $this->params);

var_dump($res);
$res = Tools::dealCurlResult($res);

return $res->status === 0;
if ($res->status == Error::_OK) {
return true;
} else {
throw new SmsException($res->msg, $res->status);
}
}
}

0 comments on commit 2122ab8

Please sign in to comment.