Completed
Push — master ( 8b523a...f84fc8 )
by Xu
07:27
created

Broadcast::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.tintsoft.com/
4
 * @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
5
 * @license http://www.tintsoft.com/license/
6
 */
7
8
namespace yuncms\broadcast\aliyun;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yuncms\broadcast\BaseBroadcast;
13
use yuncms\broadcast\MessageInterface;
14
use AliyunMNS\Config;
15
use AliyunMNS\Http\HttpClient;
16
use AliyunMNS\Requests\PublishMessageRequest;
17
use AliyunMNS\Responses\PublishMessageResponse;
18
19
/**
20
 * Class Broadcast
21
 *
22
 * @author Tongle Xu <[email protected]>
23
 * @since 3.0
24
 */
25
class Broadcast extends BaseBroadcast
26
{
27
28
    /**
29
     * @var  string
30
     */
31
    public $endPoint;
32
33
    /**
34
     * @var string
35
     */
36
    public $accessId;
37
38
    /**
39
     * @var string
40
     */
41
    public $accessKey;
42
43
    /**
44
     * @var null|string
45
     */
46
    public $securityToken = null;
47
48
    /**
49
     * @var  string 主题名称
50
     */
51
    public $topicName;
52
53
    /**
54
     * @var null|Config
55
     */
56
    public $config = null;
57
58
    /**
59
     * @var HttpClient
60
     */
61
    protected $_client;
62
63
    /**
64
     * 初始化组件
65
     * @throws InvalidConfigException
66
     */
67
    public function init()
68
    {
69
        parent::init();
70
        if (empty ($this->endPoint)) {
71
            throw new InvalidConfigException ('The "endPoint" property must be set.');
72
        }
73
        if (empty ($this->accessId)) {
74
            throw new InvalidConfigException ('The "accessId" property must be set.');
75
        }
76
        if (empty ($this->accessKey)) {
77
            throw new InvalidConfigException ('The "accessKey" property must be set.');
78
        }
79
        if (empty ($this->topicName)) {
80
            throw new InvalidConfigException ('The "topicName" property must be set.');
81
        }
82
    }
83
84
    /**
85
     * @return array|HttpClient aliyun mns topic instance or array configuration.
86
     * @throws InvalidConfigException
87
     */
88
    public function getClient()
89
    {
90
        if (!is_object($this->_client)) {
91
            $this->_client = Yii::createObject('AliyunMNS\Http\HttpClient', [
92
                $this->endPoint,
93
                $this->accessId,
94
                $this->accessKey,
95
                $this->securityToken,
96
                $this->config
97
            ]);
98
        }
99
        return $this->_client;
100
    }
101
102
    /**
103
     * Sends the specified message.
104
     * This method should be implemented by child classes with the actual broadcast sending logic.
105
     * @param MessageInterface $message the message to be sent
106
     * @return bool whether the message is sent successfully
107
     * @throws InvalidConfigException
108
     */
109
    protected function sendMessage($message)
110
    {
111
        $request = new PublishMessageRequest($message->toString(), $message->getTag(), $message->getAttributes());
112
        $request->setTopicName($this->topicName);
113
        $response = new PublishMessageResponse();
114
        $this->getClient()->sendRequest($request, $response);
115
        return $response->isSucceed();
116
    }
117
}