Messenger   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 180
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0
wmc 18

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A secretive() 0 5 1
A toUser() 0 3 1
A toTag() 0 3 1
A setRecipients() 0 9 2
A send() 0 22 4
A toParty() 0 3 1
A message() 0 13 4
A ofAgent() 0 5 1
A __get() 0 7 2
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 8
    public function __construct(Client $client)
57
    {
58 8
        $this->client = $client;
59 8
    }
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 4
    public function message($message)
71
    {
72 4
        if (is_string($message) || is_numeric($message)) {
73 3
            $message = new Text($message);
74
        }
75
76 4
        if (!($message instanceof Message)) {
0 ignored issues
show
introduced by
$message is always a sub-type of EasyWeChat\Kernel\Messages\Message.
Loading history...
77 1
            throw new InvalidArgumentException('Invalid message.');
78
        }
79
80 4
        $this->message = $message;
81
82 4
        return $this;
83
    }
84
85
    /**
86
     * @param int $agentId
87
     *
88
     * @return \EasyWeChat\Work\Message\Messenger
89
     */
90 4
    public function ofAgent(int $agentId)
91
    {
92 4
        $this->agentId = $agentId;
93
94 4
        return $this;
95
    }
96
97
    /**
98
     * @param array|string $userIds
99
     *
100
     * @return \EasyWeChat\Work\Message\Messenger
101
     */
102 1
    public function toUser($userIds)
103
    {
104 1
        return $this->setRecipients($userIds, 'touser');
105
    }
106
107
    /**
108
     * @param array|string $partyIds
109
     *
110
     * @return \EasyWeChat\Work\Message\Messenger
111
     */
112 1
    public function toParty($partyIds)
113
    {
114 1
        return $this->setRecipients($partyIds, 'toparty');
115
    }
116
117
    /**
118
     * @param array|string $tagIds
119
     *
120
     * @return \EasyWeChat\Work\Message\Messenger
121
     */
122 1
    public function toTag($tagIds)
123
    {
124 1
        return $this->setRecipients($tagIds, 'totag');
125
    }
126
127
    /**
128
     * Keep secret.
129
     *
130
     * @return \EasyWeChat\Work\Message\Messenger
131
     */
132 1
    public function secretive()
133
    {
134 1
        $this->secretive = true;
135
136 1
        return $this;
137
    }
138
139
    /**
140
     * @param array|string $ids
141
     * @param string       $key
142
     *
143
     * @return \EasyWeChat\Work\Message\Messenger
144
     */
145 1
    protected function setRecipients($ids, string $key): self
146
    {
147 1
        if (is_array($ids)) {
148 1
            $ids = implode('|', $ids);
149
        }
150
151 1
        $this->to = [$key => $ids];
152
153 1
        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 2
    public function send($message = null)
165
    {
166 2
        if ($message) {
167 1
            $this->message($message);
168
        }
169
170 2
        if (empty($this->message)) {
171 1
            throw new RuntimeException('No message to send.');
172
        }
173
174 2
        if (is_null($this->agentId)) {
0 ignored issues
show
introduced by
The condition is_null($this->agentId) is always false.
Loading history...
175 1
            throw new RuntimeException('No agentid specified.');
176
        }
177
178 2
        $message = $this->message->transformForJsonRequest(array_merge([
179 2
            'agentid' => $this->agentId,
180 2
            'safe' => intval($this->secretive),
181 2
        ], $this->to));
182
183 2
        $this->secretive = false;
184
185 2
        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 4
    public function __get($property)
198
    {
199 4
        if (property_exists($this, $property)) {
200 4
            return $this->$property;
201
        }
202
203 1
        throw new InvalidArgumentException(sprintf('No property named "%s"', $property));
204
    }
205
}
206