1 | <?php |
||
8 | class Client |
||
9 | { |
||
10 | /** |
||
11 | * The BearyChat incoming webhook. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $webhook; |
||
16 | |||
17 | /** |
||
18 | * The default fields for messages. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $messageDefaults = []; |
||
23 | |||
24 | /** |
||
25 | * The Guzzle http client. |
||
26 | * |
||
27 | * @var \GuzzleHttp\Client |
||
28 | */ |
||
29 | protected $httpClient; |
||
30 | |||
31 | /** |
||
32 | * Create a new Client. |
||
33 | * |
||
34 | * @param string $webhook |
||
35 | * @param array $messageDefaults See `\ElfSundae\BearyChat\MessageDefaults` |
||
36 | * @param \GuzzleHttp\Client $httpClient |
||
37 | */ |
||
38 | 9 | public function __construct($webhook = null, $messageDefaults = [], $httpClient = null) |
|
44 | |||
45 | /** |
||
46 | * Get the webhook. |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | 2 | public function getWebhook() |
|
54 | |||
55 | /** |
||
56 | * Set the webhook. |
||
57 | * |
||
58 | * @param string $webhook |
||
59 | * @return $this |
||
60 | */ |
||
61 | 9 | public function setWebhook($webhook) |
|
67 | |||
68 | /** |
||
69 | * Change the webhook URL. |
||
70 | * |
||
71 | * @param string $webhook |
||
72 | * @return $this |
||
73 | */ |
||
74 | 1 | public function webhook($webhook) |
|
78 | |||
79 | /** |
||
80 | * Retrieve message defaults. |
||
81 | * |
||
82 | * @param string|null $key |
||
83 | * @return mixed |
||
84 | */ |
||
85 | 4 | public function getMessageDefaults($key = null) |
|
95 | |||
96 | /** |
||
97 | * Set the message defaults. |
||
98 | * |
||
99 | * @param array $defaults |
||
100 | * @return $this |
||
101 | */ |
||
102 | 9 | public function setMessageDefaults($defaults) |
|
108 | |||
109 | /** |
||
110 | * Set the message defaults. |
||
111 | * |
||
112 | * @param array $defaults |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function messageDefaults($defaults) |
||
119 | |||
120 | /** |
||
121 | * Create a new Message instance. |
||
122 | * |
||
123 | * @return \ElfSundae\BearyChat\Message |
||
124 | */ |
||
125 | 2 | public function createMessage() |
|
129 | |||
130 | /** |
||
131 | * Send message to the BearyChat. |
||
132 | * |
||
133 | * @param mixed $message A JSON string, or any arrayable object. |
||
134 | * @return bool |
||
135 | */ |
||
136 | 2 | public function sendMessage($message) |
|
137 | { |
||
138 | 2 | if ($payload = $this->getJsonPayload($message)) { |
|
139 | 1 | $response = $this->getHttpClient() |
|
140 | 1 | ->post($this->getWebhook(), [ |
|
141 | 1 | 'headers' => ['Content-Type' => 'application/json'], |
|
142 | 1 | 'body' => $payload, |
|
143 | ]); |
||
144 | |||
145 | return 200 === $response->getStatusCode(); |
||
146 | } |
||
147 | |||
148 | 1 | return false; |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * Get the JSON payload from an object. |
||
153 | * |
||
154 | * @param mixed $message |
||
155 | * @return string |
||
156 | */ |
||
157 | 2 | protected function getJsonPayload($message) |
|
169 | |||
170 | /** |
||
171 | * Get the http client. |
||
172 | * @return \GuzzleHttp\Client |
||
173 | */ |
||
174 | 2 | public function getHttpClient() |
|
175 | { |
||
176 | 2 | if (! $this->httpClient instanceof HttpClient) { |
|
177 | $this->httpClient = new HttpClient; |
||
178 | } |
||
179 | |||
180 | 2 | return $this->httpClient; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Any unhandled methods will be sent to a new Message instance. |
||
185 | * |
||
186 | * @param string $name |
||
187 | * @param array $args |
||
188 | * @return mixed |
||
189 | */ |
||
190 | 1 | public function __call($name, $args) |
|
196 | } |
||
197 |