Completed
Push — master ( f65b85...4d38d1 )
by Xu
07:50
created

Aliyun::init()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 0
dl 0
loc 16
rs 8.8571
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;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use AliyunMNS\Config;
0 ignored issues
show
Bug introduced by
The type AliyunMNS\Config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use AliyunMNS\Http\HttpClient;
0 ignored issues
show
Bug introduced by
The type AliyunMNS\Http\HttpClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use AliyunMNS\Requests\CreateTopicRequest;
0 ignored issues
show
Bug introduced by
The type AliyunMNS\Requests\CreateTopicRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use AliyunMNS\Requests\DeleteTopicRequest;
0 ignored issues
show
Bug introduced by
The type AliyunMNS\Requests\DeleteTopicRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use AliyunMNS\Responses\CreateTopicResponse;
0 ignored issues
show
Bug introduced by
The type AliyunMNS\Responses\CreateTopicResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use AliyunMNS\Responses\DeleteTopicResponse;
0 ignored issues
show
Bug introduced by
The type AliyunMNS\Responses\DeleteTopicResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
/**
20
 * Class Aliyun
21
 *
22
 * @author Tongle Xu <[email protected]>
23
 * @since 3.0
24
 */
25
class Aliyun extends Broadcast
26
{
27
    /**
28
     * @var  string
29
     */
30
    public $endPoint;
31
32
    /**
33
     * @var string
34
     */
35
    public $accessId;
36
37
    /**
38
     * @var string
39
     */
40
    public $accessKey;
41
42
    /**
43
     * @var null|string
44
     */
45
    public $securityToken = null;
46
47
    /** @var  string 默认操作的主题 */
48
    public $topicName;
49
50
    /**
51
     * @var null|Config
52
     */
53
    public $config = null;
54
55
    /**
56
     * @var HttpClient
57
     */
58
    protected $client;
59
    /**
60
     * 初始化组件
61
     * @throws InvalidConfigException
62
     */
63
    public function init()
64
    {
65
        parent::init();
66
        if (empty ($this->endPoint)) {
67
            throw new InvalidConfigException ('The "endPoint" property must be set.');
68
        }
69
        if (empty ($this->accessId)) {
70
            throw new InvalidConfigException ('The "accessId" property must be set.');
71
        }
72
        if (empty ($this->accessKey)) {
73
            throw new InvalidConfigException ('The "accessKey" property must be set.');
74
        }
75
        if (empty ($this->topicName)) {
76
            $this->topicName = Yii::$app->id;
77
        }
78
        $this->client = new HttpClient($this->endPoint, $this->accessId, $this->accessKey, $this->securityToken, $this->config);
79
    }
80
    /**
81
     * 获取主题(广播)
82
     * @param string $topicName
83
     * @return Topic
84
     */
85
    public function getTopicRef($topicName = null)
86
    {
87
        return new Topic([
0 ignored issues
show
Bug introduced by
The type yuncms\broadcast\Topic was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
88
            'client' => $this->client,
89
            'topicName' => !is_null($topicName) ? $topicName : $this->topicName
90
        ]);
91
    }
92
    /**
93
     * 向主题推送消息
94
     * @param string|array $messageBody
95
     * @param string $messageTag
96
     * @param null $messageAttributes
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $messageAttributes is correct as it would always require null to be passed?
Loading history...
97
     * @param string $topicName
98
     * @return \AliyunMNS\Responses\BaseResponse
0 ignored issues
show
Bug introduced by
The type AliyunMNS\Responses\BaseResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
99
     */
100
    public function publishMessage($messageBody, $messageTag = null, $messageAttributes = null, $topicName = null)
101
    {
102
        $topic = $this->getTopicRef($topicName);
103
        return $topic->publishMessage($messageBody, $messageTag, $messageAttributes);
104
    }
105
    /**
106
     * 创建主题
107
     * @param string $topicName
108
     * @return \AliyunMNS\Responses\BaseResponse
109
     */
110
    public function create($topicName)
111
    {
112
        $request = new CreateTopicRequest($topicName);
113
        $response = new CreateTopicResponse($request->getTopicName());
114
        return $this->client->sendRequest($request, $response);
115
    }
116
117
    /**
118
     * 删除主题
119
     * @param string $topicName
120
     * @return \AliyunMNS\Responses\BaseResponse
121
     */
122
    public function delete($topicName)
123
    {
124
        $request = new DeleteTopicRequest($topicName);
125
        $response = new DeleteTopicResponse();
126
        return $this->client->sendRequest($request, $response);
127
    }
128
}