Completed
Push — master ( 3f41dd...1f43a2 )
by Carlos
14:26
created

MessageBuilder::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 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\OfficialAccount\Broadcasting;
13
14
use EasyWeChat\Kernel\Contracts\MessageInterface;
15
use EasyWeChat\Kernel\Exceptions\RuntimeException;
16
17
/**
18
 * Class MessageBuilder.
19
 *
20
 * @author overtrue <[email protected]>
21
 */
22
class MessageBuilder
23
{
24
    /**
25
     * @var array
26
     */
27
    protected $to = [];
28
29
    /**
30
     * @var \EasyWeChat\Kernel\Contracts\MessageInterface
31
     */
32
    protected $message;
33
34
    /**
35
     * Set message.
36
     *
37
     * @param \EasyWeChat\Kernel\Contracts\MessageInterface $message
38
     *
39
     * @return $this
40
     */
41
    public function message(MessageInterface $message)
42
    {
43
        $this->message = $message;
44
45
        return $this;
46
    }
47
48
    /**
49
     * Set target user or group.
50
     *
51
     * @param mixed $to
52
     *
53
     * @return $this
54
     */
55
    public function to(array $to)
56
    {
57
        $this->to = $to;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @param int $tagId
64
     *
65
     * @return \EasyWeChat\OfficialAccount\Broadcasting\MessageBuilder
66
     */
67
    public function toTag(int $tagId)
68
    {
69
        $this->to([
70
            'filter' => [
71
                'is_to_all' => false,
72
                'tag_id' => $tagId,
73
            ],
74
        ]);
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param array $openids
81
     *
82
     * @return \EasyWeChat\OfficialAccount\Broadcasting\MessageBuilder
83
     */
84
    public function toUsers(array $openids)
85
    {
86
        $this->to([
87
            'touser' => $openids,
88
        ]);
89
90
        return $this;
91
    }
92
93
    /**
94
     * @return $this
95
     */
96
    public function toAll()
97
    {
98
        $this->to([
99
            'filter' => ['is_to_all' => true],
100
        ]);
101
102
        return $this;
103
    }
104
105
    /**
106
     * Build message.
107
     *
108
     * @param array $prepends
109
     *
110
     * @return array
111
     *
112
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
113
     */
114
    public function build(array $prepends = []): array
115
    {
116
        if (empty($this->message)) {
117
            throw new RuntimeException('No message content to send.');
118
        }
119
120
        $content = $this->message->transformForJsonRequest();
121
122
        if (empty($prepends)) {
123
            if (empty($this->to)) {
124
                throw new RuntimeException('The message reception object is not specified');
125
            }
126
127
            $prepends = $this->to;
128
        }
129
130
        $message = array_merge($prepends, $content);
131
132
        return $message;
133
    }
134
135
    /**
136
     * Build preview message.
137
     *
138
     * @param string $by
139
     * @param string $user
140
     *
141
     * @return array
142
     *
143
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
144
     */
145
    public function buildForPreview(string $by, string $user): array
146
    {
147
        return $this->build([$by => $user]);
148
    }
149
}
150