1 | <?php |
||
2 | |||
3 | namespace DingNotice; |
||
4 | |||
5 | use DingNotice\Messages\ActionCard; |
||
6 | use DingNotice\Messages\FeedCard; |
||
7 | use DingNotice\Messages\Link; |
||
8 | use DingNotice\Messages\Markdown; |
||
9 | use DingNotice\Messages\Message; |
||
10 | use DingNotice\Messages\Text; |
||
11 | use GuzzleHttp\Client; |
||
12 | |||
13 | class DingTalkService |
||
14 | { |
||
15 | |||
16 | protected $config; |
||
17 | |||
18 | /** |
||
19 | * @var Message |
||
20 | */ |
||
21 | protected $message; |
||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $mobiles = []; |
||
26 | /** |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $atAll = false; |
||
30 | |||
31 | /** |
||
32 | * @var SendClient |
||
33 | */ |
||
34 | protected $client; |
||
35 | |||
36 | /** |
||
37 | * DingTalkService constructor. |
||
38 | * @param $config |
||
39 | * @param null $client |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
40 | */ |
||
41 | public function __construct($config, SendClient $client = null) |
||
42 | { |
||
43 | $this->config = $config; |
||
44 | $this->setTextMessage('null'); |
||
45 | |||
46 | if ($client != null) { |
||
47 | $this->client = $client; |
||
48 | return; |
||
49 | } |
||
50 | $this->client = $this->createClient($config); |
||
51 | |||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param Message $message |
||
56 | */ |
||
57 | public function setMessage($message) |
||
58 | { |
||
59 | $this->message = $message; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return array |
||
64 | */ |
||
65 | public function getMessage() |
||
66 | { |
||
67 | return $this->message->getMessage(); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param array $mobiles |
||
72 | * @param bool $atAll |
||
73 | */ |
||
74 | public function setAt($mobiles = [], $atAll = false) |
||
75 | { |
||
76 | $this->mobiles = $mobiles; |
||
77 | $this->atAll = $atAll; |
||
78 | if ($this->message) { |
||
79 | $this->message->sendAt($mobiles, $atAll); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * create a guzzle client |
||
85 | * @return HttpClient |
||
86 | * @author wangju 2019-05-17 20:25 |
||
87 | */ |
||
88 | protected function createClient($config) |
||
89 | { |
||
90 | $client = new HttpClient($config); |
||
91 | return $client; |
||
92 | } |
||
93 | |||
94 | |||
95 | /** |
||
96 | * @param $content |
||
97 | * @return $this |
||
98 | */ |
||
99 | public function setTextMessage($content) |
||
100 | { |
||
101 | $this->message = new Text($content); |
||
102 | $this->message->sendAt($this->mobiles, $this->atAll); |
||
103 | return $this; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param $title |
||
108 | * @param $text |
||
109 | * @param $messageUrl |
||
110 | * @param string $picUrl |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function setLinkMessage($title, $text, $messageUrl, $picUrl = '') |
||
114 | { |
||
115 | $this->message = new Link($title, $text, $messageUrl, $picUrl); |
||
116 | $this->message->sendAt($this->mobiles, $this->atAll); |
||
117 | return $this; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param $title |
||
122 | * @param $text |
||
123 | * @return $this |
||
124 | */ |
||
125 | public function setMarkdownMessage($title, $markdown) |
||
126 | { |
||
127 | $this->message = new Markdown($title, $markdown); |
||
128 | $this->message->sendAt($this->mobiles, $this->atAll); |
||
129 | return $this; |
||
130 | } |
||
131 | |||
132 | |||
133 | /** |
||
134 | * @param $title |
||
135 | * @param $text |
||
136 | * @param int $hideAvatar |
||
137 | * @param int $btnOrientation |
||
138 | * @return ActionCard|Message |
||
139 | */ |
||
140 | public function setActionCardMessage($title, $markdown, $hideAvatar = 0, $btnOrientation = 0) |
||
141 | { |
||
142 | $this->message = new ActionCard($this, $title, $markdown, $hideAvatar, $btnOrientation); |
||
143 | $this->message->sendAt($this->mobiles, $this->atAll); |
||
144 | return $this->message; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return FeedCard|Message |
||
149 | */ |
||
150 | public function setFeedCardMessage() |
||
151 | { |
||
152 | $this->message = new FeedCard($this); |
||
153 | $this->message->sendAt($this->mobiles, $this->atAll); |
||
154 | return $this->message; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @return bool|array |
||
159 | */ |
||
160 | public function send() |
||
161 | { |
||
162 | if (!$this->config['enabled']) { |
||
163 | return false; |
||
164 | } |
||
165 | return $this->client->send($this->message->getBody()); |
||
166 | } |
||
167 | |||
168 | } |
||
169 |