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 | 5 | public function __construct($webhook = null, array $messageDefaults = [], $httpClient = null) |
|
39 | { |
||
40 | 5 | $this->webhook = $webhook; |
|
41 | 5 | $this->messageDefaults = $messageDefaults; |
|
42 | 5 | $this->httpClient = $httpClient; |
|
43 | 5 | } |
|
44 | |||
45 | /** |
||
46 | * Get the webhook. |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | 1 | public function getWebhook() |
|
51 | { |
||
52 | 1 | return $this->webhook; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * Set the webhook. |
||
57 | * |
||
58 | * @param string $webhook |
||
59 | * @return $this |
||
60 | */ |
||
61 | 1 | public function setWebhook($webhook) |
|
62 | { |
||
63 | 1 | $this->webhook = $webhook; |
|
64 | |||
65 | 1 | return $this; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Change the webhook URL. |
||
70 | * |
||
71 | * @param string $webhook |
||
72 | */ |
||
73 | 1 | public function webhook($webhook) |
|
74 | { |
||
75 | 1 | return $this->setWebhook($webhook); |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Retrieve message defaults. |
||
80 | * |
||
81 | * @param string|null $option |
||
82 | * @return mixed |
||
83 | */ |
||
84 | 3 | public function getMessageDefaults($option = null) |
|
85 | { |
||
86 | 3 | return is_null($option) ? |
|
87 | 3 | $this->messageDefaults : |
|
88 | 3 | (isset($this->messageDefaults[$option]) ? $this->messageDefaults[$option] : null); |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Set the message defaults. |
||
93 | * |
||
94 | * @param array $messageDefaults |
||
95 | */ |
||
96 | 2 | public function setMessageDefaults(array $messageDefaults) |
|
97 | { |
||
98 | 2 | $this->messageDefaults = $messageDefaults; |
|
99 | |||
100 | 2 | return $this; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Create a new Message instance. |
||
105 | * |
||
106 | * @return \ElfSundae\BearyChat\Message |
||
107 | */ |
||
108 | 1 | public function createMessage() |
|
109 | { |
||
110 | 1 | return new Message($this); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Send message to the BearyChat. |
||
115 | * |
||
116 | * @param mixed $message A JSON string, or any arrayable object. |
||
117 | * @return bool |
||
118 | */ |
||
119 | public function sendMessage($message) |
||
134 | |||
135 | /** |
||
136 | * Get the payload from an object. |
||
137 | * |
||
138 | * @param mixed $message |
||
139 | * @return string |
||
140 | */ |
||
141 | protected function getPayload($message) |
||
159 | |||
160 | /** |
||
161 | * Get the http client. |
||
162 | * @return \GuzzleHttp\Client |
||
163 | */ |
||
164 | protected function getHttpClient() |
||
172 | |||
173 | /** |
||
174 | * Any unhandled methods will be sent to a new Message instance. |
||
175 | * |
||
176 | * @param string $name |
||
177 | * @param array $args |
||
178 | * @return mixed |
||
179 | */ |
||
180 | public function __call($name, $args) |
||
186 | } |
||
187 |