Skip to content

Commit

Permalink
Exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Jul 10, 2015
1 parent 85701f7 commit f558ccf
Show file tree
Hide file tree
Showing 34 changed files with 497 additions and 177 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ sftp-config.json
composer.lock
*.jpg
.subsplit
.idea
.idea
.php_cs.cache
2 changes: 1 addition & 1 deletion .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

$finder = Symfony\CS\Finder\DefaultFinder::create()
->in(__DIR__ . '/app/')
->in(__DIR__ . '/src/')
;

return Symfony\CS\Config\Config::create()
Expand Down
73 changes: 33 additions & 40 deletions src/EasyWechat/Card/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@

namespace EasyWeChat\Card;

use EasyWeChat\Cache\Manager as Cache;
use EasyWeChat\Core\Http;
use EasyWeChat\Support\Collection;
use EasyWeChat\Support\Arr;

/**
* 卡券.
* Class Card.
*/
class Card
{
/**
* Http对象
* Http client.
*
* @var Http
*/
Expand Down Expand Up @@ -78,27 +80,19 @@ class Card
const API_TICKET = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card';

/**
* constructor.
* Constructor.
*
* <pre>
* $config:
*
* array(
* 'app_id' => YOUR_APPID, // string mandatory;
* 'secret' => YOUR_SECRET, // string mandatory;
* )
* </pre>
*
* @param array $config configuration array
* @param Http $http
* @param Cache $cache
*/
public function __construct(array $config)
public function __construct(Http $http, Cache $cache)
{
$this->http = new Http(new AccessToken($config));
$this->cache = new Cache($config['app_id']);
$this->http = $http->setExpectedException('EasyWeChat\Card\CardHttpException');
$this->cache = $cache;
}

/**
* 获取jsticket.
* Get JSSDK ticket.
*
* @return string
*/
Expand Down Expand Up @@ -158,7 +152,7 @@ public function attachExtension($cardId, array $extension = [])
}

/**
* 创建卡券.
* Create card.
*
* @param array $base
* @param array $properties
Expand All @@ -183,7 +177,7 @@ public function create(array $base, array $properties = [], $type = self::GENERA
}

/**
* 卡券详情.
* Get card detail.
*
* @param string $cardId
*
Expand All @@ -199,7 +193,7 @@ public function get($cardId)
}

/**
* 修改卡券.
* Update card.
*
* @param string $cardId
* @param string $type
Expand All @@ -222,7 +216,7 @@ public function update($cardId, $type, array $base = [], array $data = [])
}

/**
* 批量获取卡券列表.
* Batch get card list.
*
* @param int $offset
* @param int $count
Expand All @@ -242,11 +236,10 @@ public function lists($offset = 0, $count = 10)
}

/**
* 核销.
* Confirm consume.
*
* @param string $code 要消耗序列号
* @param string $cardId 卡券 ID。创建卡券时 use_custom_code 填写 true 时必填。
* 非自定义 code 不必填写。
* @param string $code
* @param string $cardId
*
* @return Collection
*/
Expand All @@ -261,7 +254,7 @@ public function consume($code, $cardId = null)
}

/**
* 废弃卡券,失效.
* Disable card.
*
* @param string $code
* @param string $cardId
Expand All @@ -279,7 +272,7 @@ public function disable($code, $cardId = null)
}

/**
* 删除卡券.
* Delete card.
*
* @param string $cardId
*
Expand All @@ -293,7 +286,7 @@ public function delete($cardId)
}

/**
* 修改库存.
* Change stock.
*
* @param string $cardId
* @param int $amount
Expand All @@ -317,7 +310,7 @@ public function updateStock($cardId, $amount)
}

/**
* 增加库存.
* Increment stock.
*
* @param string $cardId
* @param int $amount
Expand All @@ -330,7 +323,7 @@ public function incStock($cardId, $amount)
}

/**
* 减少库存.
* Decrement stock.
*
* @param string $cardId
* @param int $amount
Expand All @@ -343,7 +336,7 @@ public function decStock($cardId, $amount)
}

/**
* 查询Code.
* Get code of card.
*
* @param string $code
* @param string $cardId
Expand All @@ -361,7 +354,7 @@ public function getCode($code, $cardId = null)
}

/**
* 修改code.
* Update code of card.
*
* @param string $code
* @param string $newCode
Expand All @@ -381,7 +374,7 @@ public function updateCode($code, $newCode, $cardId)
}

/**
* code 解码.
* Decode code.
*
* @param string $encryptedCode
*
Expand All @@ -397,7 +390,7 @@ public function getRealCode($encryptedCode)
}

/**
* 激活/绑定会员卡
* Active member card.
*
* <pre>
* $data:
Expand All @@ -422,7 +415,7 @@ public function memberCardActivate($cardId, array $data)
}

/**
* 会员卡交易.
* Trade member card.
*
* <pre>
* $data:
Expand All @@ -449,7 +442,7 @@ public function memberCardTrade($cardId, array $data)
}

/**
* 电影票更新座位.
* Update ticket.
*
* <pre>
* $data:
Expand All @@ -475,7 +468,7 @@ public function updateMovieTicket($cardId, array $data)
}

/**
* 会议门票更新.
* Update meeting ticket.
*
* <pre>
* $data:
Expand All @@ -501,7 +494,7 @@ public function updateMeetingTicket($cardId, array $data)
}

/**
* 在线值机.
* Checkin.
*
* <pre>
* $data:
Expand All @@ -528,7 +521,7 @@ public function checkin($cardId, array $data)
}

/**
* 生成签名.
* Return signature.
*
* @return string
*/
Expand All @@ -542,7 +535,7 @@ public function getSignature()
}

/**
* 获取随机字符串.
* Return random string.
*
* @return string
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Exception.php.
* CardHttpException.php.
*
* Part of EasyWeChat.
*
Expand All @@ -15,11 +15,14 @@
* @link http://overtrue.me
*/

namespace EasyWeChat\Core;
namespace EasyWeChat\Card;

use Exception as BaseException;
use EasyWeChat\Core\Exception as CoreException;

class Exception extends BaseException
/**
* Class CardHttpException.
*/
class CardHttpException extends CoreException
{
protected $errors = [
'-1' => '系统繁忙,此时请开发者稍候再试',
Expand Down Expand Up @@ -173,8 +176,6 @@ public function __construct($message, $code = -1)
{
$message = empty($this->errors[$code]) ? $this->message : $message.' '.$this->errors[$code];

$message = "[Wechat]{$message}";

parent::__construct($message, $code);
}
}
44 changes: 42 additions & 2 deletions src/EasyWechat/Core/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

use EasyWeChat\Core\Exceptions\FaultException;
use EasyWeChat\Core\Exceptions\HttpException;
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
use EasyWeChat\Support\Http as HttpClient;
use EasyWeChat\Support\JSON;
use Exception;

/**
* Class Http.
Expand All @@ -43,6 +45,13 @@ class Http extends HttpClient
*/
protected $json = false;

/**
* Defualt exception.
*
* @var string
*/
protected $exception = 'EasyWeChat\Core\Exceptions\HttpException';

/**
* Constructor.
*
Expand All @@ -65,6 +74,24 @@ public function setToken(AccessToken $token)
$this->token = $token;
}

/**
* Set exception to be throw when an error occurs.
*
* @param Exception $exception
*
* @return Http
*/
public function setExpectedException($exception)
{
if (!is_subclass_of($exception, 'Exception')) {
throw new InvalidArgumentException('Invalid Exception name.');
}

$this->exception = is_string($exception) ? $exception : get_class($exception);

return $this;
}

/**
* Make a request.
*
Expand Down Expand Up @@ -99,7 +126,7 @@ public function request($url, $method = self::GET, $params = [], $options = [])
}

// plain text or JSON
$textMIME = '~application/json|text/plain~i';
$textMIME = '~.*/json|text/plain~';

$contents = JSON::decode($response['data'], true);

Expand All @@ -115,7 +142,7 @@ public function request($url, $method = self::GET, $params = [], $options = [])
$contents['errmsg'] = 'Unknown';
}

throw new FaultException("[{$contents['errcode']}] ".$contents['errcode'], $contents['errcode']);
$this->thorwException($contents['errmsg'], $contents['errcode']);
}

if ($contents === ['errcode' => '0', 'errmsg' => 'ok']) {
Expand All @@ -125,6 +152,19 @@ public function request($url, $method = self::GET, $params = [], $options = [])
return $contents;
}

/**
* Throw Http Exception.
*
* @param string $msg
* @param int $code
*/
protected function thorwException($msg, $code)
{
$exception = new $this->exception($msg, $code);

throw $exception;
}

/**
* Magic call.
*
Expand Down
Loading

0 comments on commit f558ccf

Please sign in to comment.