Passed
Push — master ( 95b064...f63c48 )
by Carlos
02:38
created

Messenger::send()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 6
nop 1
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Work\Message;
13
14
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
15
use EasyWeChat\Kernel\Exceptions\RuntimeException;
16
use EasyWeChat\Kernel\Messages\Message;
17
use EasyWeChat\Kernel\Messages\Text;
18
19
/**
20
 * Class MessageBuilder.
21
 *
22
 * @author overtrue <[email protected]>
23
 */
24
class Messenger
25
{
26
    /**
27
     * @var \EasyWeChat\Kernel\Messages\Message;
28
     */
29
    protected $message;
30
31
    /**
32
     * @var array
33
     */
34
    protected $to = ['touser' => '@all'];
35
36
    /**
37
     * @var int
38
     */
39
    protected $agentId;
40
41
    /**
42
     * @var bool
43
     */
44
    protected $secretive = false;
45
46
    /**
47
     * @var \EasyWeChat\Work\Message\Client
48
     */
49
    protected $client;
50
51
    /**
52
     * MessageBuilder constructor.
53
     *
54
     * @param \EasyWeChat\Work\Message\Client $client
55
     */
56
    public function __construct(Client $client)
57
    {
58
        $this->client = $client;
59
    }
60
61
    /**
62
     * Set message to send.
63
     *
64
     * @param string|Message $message
65
     *
66
     * @return \EasyWeChat\Work\Message\Messenger
67
     *
68
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
69
     */
70
    public function message($message)
71
    {
72
        if (is_string($message) || is_numeric($message)) {
73
            $message = new Text($message);
74
        }
75
76
        if (!($message instanceof Message)) {
0 ignored issues
show
introduced by
$message is always a sub-type of EasyWeChat\Kernel\Messages\Message.
Loading history...
77
            throw new InvalidArgumentException('Invalid message.');
78
        }
79
80
        $this->message = $message;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param int $agentId
87
     *
88
     * @return \EasyWeChat\Work\Message\Messenger
89
     */
90
    public function ofAgent(int $agentId)
91
    {
92
        $this->agentId = $agentId;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param array|string $userIds
99
     *
100
     * @return \EasyWeChat\Work\Message\Messenger
101
     */
102
    public function toUser($userIds)
103
    {
104
        return $this->setRecipients($userIds, 'touser');
105
    }
106
107
    /**
108
     * @param array|string $partyIds
109
     *
110
     * @return \EasyWeChat\Work\Message\Messenger
111
     */
112
    public function toParty($partyIds)
113
    {
114
        return $this->setRecipients($partyIds, 'toparty');
115
    }
116
117
    /**
118
     * @param array|string $tagIds
119
     *
120
     * @return \EasyWeChat\Work\Message\Messenger
121
     */
122
    public function toTag($tagIds)
123
    {
124
        return $this->setRecipients($tagIds, 'totag');
125
    }
126
127
    /**
128
     * Keep secret.
129
     *
130
     * @return \EasyWeChat\Work\Message\Messenger
131
     */
132
    public function secretive()
133
    {
134
        $this->secretive = true;
135
136
        return $this;
137
    }
138
139
    /**
140
     * @param array|string $ids
141
     * @param string       $key
142
     *
143
     * @return \EasyWeChat\Work\Message\Messenger
144
     */
145
    protected function setRecipients($ids, string $key): self
146
    {
147
        if (is_array($ids)) {
148
            $ids = implode('|', $ids);
149
        }
150
151
        $this->to = [$key => $ids];
152
153
        return $this;
154
    }
155
156
    /**
157
     * @param \EasyWeChat\Kernel\Messages\Message|string|null $message
158
     *
159
     * @return mixed
160
     *
161
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
162
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
163
     */
164
    public function send($message = null)
165
    {
166
        if ($message) {
167
            $this->message($message);
168
        }
169
170
        if (empty($this->message)) {
171
            throw new RuntimeException('No message to send.');
172
        }
173
174
        if (is_null($this->agentId)) {
0 ignored issues
show
introduced by
The condition is_null($this->agentId) is always false.
Loading history...
175
            throw new RuntimeException('No agentid specified.');
176
        }
177
178
        $message = $this->message->transformForJsonRequest(array_merge([
179
            'agentid' => $this->agentId,
180
            'safe' => intval($this->secretive),
181
        ], $this->to));
182
183
        $this->secretive = false;
184
185
        return $this->client->send($message);
186
    }
187
188
    /**
189
     * Return property.
190
     *
191
     * @param string $property
192
     *
193
     * @return mixed
194
     *
195
     * @throws InvalidArgumentException
196
     */
197
    public function __get($property)
198
    {
199
        if (property_exists($this, $property)) {
200
            return $this->$property;
201
        }
202
203
        throw new InvalidArgumentException(sprintf('No property named "%s"', $property));
204
    }
205
}
206