Completed
Pull Request — master (#1606)
by
unknown
03:24
created

Messenger   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 103
ccs 26
cts 26
cp 1
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __get() 0 7 2
A send() 0 17 4
A message() 0 13 4
A toGroup() 0 5 1
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\GroupRobot;
13
14
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
15
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
16
use EasyWeChat\Kernel\Exceptions\RuntimeException;
17
use EasyWeChat\Work\GroupRobot\Messages\Message;
18
use EasyWeChat\Work\GroupRobot\Messages\Text;
19
20
/**
21
 * Class Messenger.
22
 *
23
 * @author her-cat <[email protected]>
24
 */
25
class Messenger
26
{
27
    /**
28
     * @var Client
29
     */
30
    protected $client;
31
32
    /**
33
     * @var Message
34
     */
35
    protected $message;
36
37
    /**
38
     * @var string
39
     */
40
    protected $groupKey;
41
42
    /**
43
     * Messenger constructor.
44
     *
45
     * @param Client $client
46
     */
47 10
    public function __construct(Client $client)
48
    {
49 10
        $this->client = $client;
50 10
    }
51
52
    /**
53
     * @param string|Message $message
54
     *
55
     * @return Messenger
56
     *
57
     * @throws InvalidArgumentException
58
     */
59 8
    public function message($message)
60
    {
61 8
        if (is_string($message) || is_numeric($message)) {
62 4
            $message = new Text($message);
63
        }
64
65 8
        if (!($message instanceof  Message)) {
0 ignored issues
show
introduced by
$message is always a sub-type of EasyWeChat\Work\GroupRobot\Messages\Message.
Loading history...
66 1
            throw new InvalidArgumentException('Invalid message.');
67
        }
68
69 8
        $this->message = $message;
70
71 8
        return $this;
72
    }
73
74
    /**
75
     * @param string $groupKey
76
     *
77
     * @return Messenger
78
     */
79 6
    public function toGroup(string $groupKey)
80
    {
81 6
        $this->groupKey = $groupKey;
82
83 6
        return $this;
84
    }
85
86
    /**
87
     * @param null|string|Message $message
88
     *
89
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
90
     *
91
     * @throws RuntimeException
92
     * @throws InvalidArgumentException
93
     * @throws InvalidConfigException
94
     */
95 7
    public function send($message = null)
96
    {
97 7
        if ($message) {
98 2
            $this->message($message);
99
        }
100
101 7
        if (empty($this->message)) {
102 1
            throw new RuntimeException('No message to send.');
103
        }
104
105 6
        if (is_null($this->groupKey)) {
0 ignored issues
show
introduced by
The condition is_null($this->groupKey) is always false.
Loading history...
106 1
            throw new RuntimeException('No group key specified.');
107
        }
108
109 5
        $message = $this->message->transformForJsonRequest();
110
111 5
        return $this->client->send($this->groupKey, $message);
112
    }
113
114
    /**
115
     * @param string $property
116
     *
117
     * @return mixed
118
     *
119
     * @throws InvalidArgumentException
120
     */
121 2
    public function __get($property)
122
    {
123 2
        if (property_exists($this, $property)) {
124 2
            return $this->$property;
125
        }
126
127 1
        throw new InvalidArgumentException(sprintf('No property named "%s"', $property));
128
    }
129
}
130