Completed
Pull Request — master (#176)
by ru cheng
02:33
created

Broadcast::status()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Broadcast.php
4
 *
5
 * Part of Overtrue\Wechat.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    overtrue <[email protected]>
11
 * @copyright 2015 overtrue <[email protected]>
12
 * @link      https://github.com/overtrue
13
 * @link      http://overtrue.me
14
 */
15
16
namespace Overtrue\Wechat;
17
18
use Overtrue\Wechat\Messages\BaseMessage;
19
20
class Broadcast
21
{
22
    const API_SEND_BY_GROUP = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall';
23
    const API_SEND_BY_OPENID = 'https://api.weixin.qq.com/cgi-bin/message/mass/send';
24
    const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/message/mass/delete';
25
    const API_PREVIEW = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview';
26
    const API_GET = 'http://api.weixin.qq.com/cgi-bin/message/mass/get';
27
28
    const PREVIEW_BY_OPENID = 'touser';
29
    const PREVIEW_BY_WXH = 'towxname';
30
31
    /**
32
     * Http对象
33
     *
34
     * @var Http
35
     */
36
    protected $http;
37
38
    /**
39
     * 消息
40
     *
41
     * @var \Overtrue\Wechat\Messages\BaseMessage;
42
     */
43
    protected $message;
44
45
    /**
46
     * constructor
47
     *
48
     * @param string $appId
49
     * @param string $appSecret
50
     */
51
    public function __construct($appId, $appSecret)
52
    {
53
        $this->http = new Http(new AccessToken($appId, $appSecret));
54
    }
55
56
    /**
57
     * 准备消息
58
     *
59
     * @param \Overtrue\Wechat\Messages\BaseMessage $message
60
     *
61
     * @return Broadcast
62
     */
63 View Code Duplication
    public function send($message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        is_string($message) && $message = Message::make('text')->with('content', $message);
66
67
        if (!$message instanceof BaseMessage) {
68
            throw new \Exception("消息必须继承自 'Overtrue\\Wechat\\BaseMessage'");
69
        }
70
71
        $this->message = $message;
72
73
        return $this;
74
    }
75
76
    /**
77
     * 发送消息
78
     *
79
     * @param string $group 组或oppenid
80
     *
81
     * @return bool
82
     */
83
    public function to($group = null)
84
    {
85
        if (empty($this->message)) {
86
            throw new Exception('未设置要发送的消息');
87
        }
88
89
        $this->message->to_group = $group;
0 ignored issues
show
Documentation introduced by
The property to_group does not exist on object<Overtrue\Wechat\Messages\BaseMessage>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
91
        $apiSend = is_array($group) ? self::API_SEND_BY_OPENID : self::API_SEND_BY_GROUP;
92
93
        return $this->http->jsonPost($apiSend, $this->message->buildForBroadcast());
94
    }
95
96
    /**
97
     * 删除群发
98
     *
99
     * @param string $msgId 发出去的消息ID
100
     *
101
     * @return bool
102
     */
103
    public function delete($msgId)
104
    {
105
        return $this->http->jsonPost(self::API_DELETE, array('msg_id' => $msgId));
106
    }
107
108
    /**
109
     * 预览
110
     *
111
     * @param string $openId 接收消息用户对应该公众号的openid
112
     * @param string $type 接收消息用户的类型
113
     *
114
     * @return bool
115
     */
116
    public function preview($openId, $type = self::PREVIEW_BY_OPENID)
117
    {
118
        $this->message->to = $openId;
119
120
        return $this->http->jsonPost(self::API_PREVIEW, $this->message->buildForBroadcastPreview($type));
121
    }
122
123
    /**
124
     * 查询群发消息发送状态
125
     *
126
     * @param string $msgId 全消息ID
127
     *
128
     * @return array
129
     */
130
    public function status($msgId)
131
    {
132
        return $this->http->jsonPost(self::API_GET, array('msg_id' => $msgId));
133
    }
134
}
135