Commit 00fc2ae3 by banice

重构状态模式

parent 83f1318e
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/23
* Time: 下午7:58
*/
namespace Lib\state;
class Order
{
private $state = 0;
private $outTradeNo = '';
private $returnDoorNum = 0;
private $returnDoorId = 0;
private $returnBoxId = 0;
private $returnBoxNum = 0;
private $boxCode = '';
public function __get($name)
{
return $this->$name;
}
public function __set($name, $value)
{
$this->$name = $value;
}
public function getCacheKey()
{
return config('redis_key.ORDER_CACHE') . ':' . $this->outTradeNo;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/23
* Time: 下午7:56
*/
namespace Lib\state;
use Lib\aliyun\api\PublishToTopic;
use Lib\aliyun\Client;
use Lib\command\ConfirmOpenDoor;
class ReturnWaiting
{
const STATE = 2;
private $order;
public function __construct(Order $order)
{
$this->order = $order;
}
public function toReturnDoorConfirm()
{
$this->order->state = self::STATE;
$update = new Update($this->order);
$update->update(['state']);
//todo 推送websocket消息 确认门已打开
//todo 回复确认门已打开消息
$confirmOpenDoor = new ConfirmOpenDoor();
$confirmOpenDoor->setOrderId($this->order->outTradeNo);
$publishToTopic = new PublishToTopic();
$publishToTopic->setMessageContent($confirmOpenDoor->getMessage());
$publishToTopic->setProductKey(env('MQTT_PRODUCT_KEY'));
$publishToTopic->setTopicFullName(Box::getOpenDoorTopic($this->order->boxCode));
$client = new Client();
$client->setApi($publishToTopic);
if ($client->request() === true) {
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/23
* Time: 下午8:03
*/
namespace Lib\state;
class Update
{
private $order;
public function __construct(Order $order)
{
$this->order = $order;
}
public function update(array $attribute)
{
$redis = app('Redis');
if ($redis instanceof \Redis) {
$update = [];
foreach ($attribute as $item) {
$update[$item] = $this->order->$item;
}
$redis->hMset($this->order->getCacheKey(), $update);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment