1 | <?php |
||
28 | class TgLog |
||
29 | { |
||
30 | /** |
||
31 | * @var ClientInterface |
||
32 | */ |
||
33 | protected $httpClient; |
||
34 | |||
35 | /** |
||
36 | * Stores the token |
||
37 | * @var string |
||
38 | */ |
||
39 | private $botToken; |
||
40 | |||
41 | /** |
||
42 | * Contains an instance to a PSR-3 compatible logger |
||
43 | * @var LoggerInterface |
||
44 | */ |
||
45 | protected $logger; |
||
46 | |||
47 | /** |
||
48 | * Stores the API URL from Telegram |
||
49 | * @var string |
||
50 | */ |
||
51 | private $apiUrl = ''; |
||
52 | |||
53 | /** |
||
54 | * With this flag we'll know what type of request to send to Telegram |
||
55 | * |
||
56 | * 'application/x-www-form-urlencoded' is the "normal" one, which is simpler and quicker. |
||
57 | * 'multipart/form-data' should be used only when you upload documents, photos, etc. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | private $formType = 'application/x-www-form-urlencoded'; |
||
62 | |||
63 | /** |
||
64 | * Stores the last method name used |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $methodName = ''; |
||
68 | |||
69 | /** |
||
70 | * TelegramLog constructor. |
||
71 | * @param string $botToken |
||
72 | * @param LoggerInterface $logger |
||
73 | * @param Client $client Optional Guzzle object |
||
74 | */ |
||
75 | 39 | public function __construct(string $botToken, LoggerInterface $logger = null, Client $client = null) |
|
93 | |||
94 | /** |
||
95 | * Prepares and sends an API request to Telegram |
||
96 | * |
||
97 | * @param TelegramMethods $method |
||
98 | * @return TelegramTypes |
||
99 | * @throws \unreal4u\TelegramAPI\Exceptions\MissingMandatoryField |
||
100 | */ |
||
101 | 30 | public function performApiRequest(TelegramMethods $method): TelegramTypes |
|
112 | |||
113 | /** |
||
114 | * @param TelegramMethods $method |
||
115 | * |
||
116 | * @return PromiseInterface |
||
117 | */ |
||
118 | public function performAsyncApiRequest(TelegramMethods $method) |
||
124 | |||
125 | /** |
||
126 | * Will download a file from the Telegram server. Before calling this function, you have to call the getFile method! |
||
127 | * |
||
128 | * @see \unreal4u\TelegramAPI\Telegram\Types\File |
||
129 | * @see \unreal4u\TelegramAPI\Telegram\Methods\GetFile |
||
130 | * |
||
131 | * @param File $file |
||
132 | * @return TelegramDocument |
||
133 | */ |
||
134 | public function downloadFile(File $file): TelegramDocument |
||
141 | |||
142 | /** |
||
143 | * @param File $file |
||
144 | * |
||
145 | * @return PromiseInterface |
||
146 | */ |
||
147 | public function downloadFileAsync(File $file): PromiseInterface |
||
167 | |||
168 | /** |
||
169 | * Builds up the Telegram API url |
||
170 | * @return TgLog |
||
171 | */ |
||
172 | 39 | final private function constructApiUrl(): TgLog |
|
178 | |||
179 | /** |
||
180 | * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work |
||
181 | * |
||
182 | * @param TelegramMethods $method |
||
183 | * @param array $formData |
||
184 | * @return TelegramRawData |
||
185 | */ |
||
186 | 23 | protected function sendRequestToTelegram(TelegramMethods $method, array $formData): TelegramRawData |
|
204 | |||
205 | /** |
||
206 | * @param TelegramMethods $method |
||
207 | * @param array $formData |
||
208 | * |
||
209 | * @return PromiseInterface |
||
210 | */ |
||
211 | protected function sendAsyncRequestToTelegram(TelegramMethods $method, array $formData): PromiseInterface |
||
231 | |||
232 | /** |
||
233 | * Resets everything to the default values |
||
234 | * |
||
235 | * @return TgLog |
||
236 | */ |
||
237 | 30 | private function resetObjectValues(): TgLog |
|
244 | |||
245 | /** |
||
246 | * Builds up the form elements to be sent to Telegram |
||
247 | * |
||
248 | * @TODO Move this to apart function |
||
249 | * |
||
250 | * @param TelegramMethods $method |
||
251 | * @return array |
||
252 | * @throws \unreal4u\TelegramAPI\Exceptions\MissingMandatoryField |
||
253 | */ |
||
254 | 30 | private function constructFormData(TelegramMethods $method): array |
|
283 | |||
284 | /** |
||
285 | * Can perform any special checks needed to be performed before sending the actual request to Telegram |
||
286 | * |
||
287 | * This will return an array with data that will be different in each case (for now). This can be changed in the |
||
288 | * future. |
||
289 | * |
||
290 | * @param TelegramMethods $method |
||
291 | * @return array |
||
292 | */ |
||
293 | 30 | private function checkSpecialConditions(TelegramMethods $method): array |
|
314 | |||
315 | /** |
||
316 | * Builds up the URL with which we can work with |
||
317 | * |
||
318 | * All methods in the Bot API are case-insensitive. |
||
319 | * All queries must be made using UTF-8. |
||
320 | * |
||
321 | * @see https://core.telegram.org/bots/api#making-requests |
||
322 | * |
||
323 | * @param TelegramMethods $call |
||
324 | * @return string |
||
325 | */ |
||
326 | 29 | protected function composeApiMethodUrl(TelegramMethods $call): string |
|
334 | |||
335 | /** |
||
336 | * Builds up a multipart form-like array for Guzzle |
||
337 | * |
||
338 | * @param array $data The original object in array form |
||
339 | * @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is |
||
340 | * @param resource $stream The actual file handler |
||
341 | * @return array Returns the actual formdata to be sent |
||
342 | */ |
||
343 | 4 | private function buildMultipartFormData(array $data, string $fileKeyName, $stream): array |
|
368 | |||
369 | /** |
||
370 | * @param TelegramRawData $telegramRawData |
||
371 | * @return TgLog |
||
372 | * @throws CustomClientException |
||
373 | */ |
||
374 | private function handleOffErrorRequest(TelegramRawData $telegramRawData): TgLog |
||
386 | } |
||
387 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: