1 | <?php |
||
25 | class TgLog |
||
26 | { |
||
27 | /** |
||
28 | * @var RequestHandlerInterface |
||
29 | */ |
||
30 | protected $requestHandler; |
||
31 | |||
32 | /** |
||
33 | * Stores the token |
||
34 | * @var string |
||
35 | */ |
||
36 | private $botToken; |
||
37 | |||
38 | /** |
||
39 | * Contains an instance to a PSR-3 compatible logger |
||
40 | * @var LoggerInterface |
||
41 | */ |
||
42 | protected $logger; |
||
43 | |||
44 | /** |
||
45 | * Stores the API URL from Telegram |
||
46 | * @var string |
||
47 | */ |
||
48 | private $apiUrl = ''; |
||
49 | |||
50 | /** |
||
51 | * With this flag we'll know what type of request to send to Telegram |
||
52 | * |
||
53 | * 'application/x-www-form-urlencoded' is the "normal" one, which is simpler and quicker. |
||
54 | * 'multipart/form-data' should be used only when you upload documents, photos, etc. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | private $formType = 'application/x-www-form-urlencoded'; |
||
59 | |||
60 | /** |
||
61 | * Stores the last method name used |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $methodName = ''; |
||
65 | |||
66 | /** |
||
67 | * TelegramLog constructor. |
||
68 | * |
||
69 | * @param string $botToken |
||
70 | * @param LoggerInterface $logger |
||
71 | * @param RequestHandlerInterface $handler |
||
72 | */ |
||
73 | 16 | public function __construct(string $botToken, LoggerInterface $logger = null, RequestHandlerInterface $handler = null) |
|
91 | |||
92 | /** |
||
93 | * Prepares and sends an API request to Telegram |
||
94 | * |
||
95 | * @param TelegramMethods $method |
||
96 | * @return TelegramTypes |
||
97 | * @throws \unreal4u\TelegramAPI\Exceptions\MissingMandatoryField |
||
98 | */ |
||
99 | 7 | public function performApiRequest(TelegramMethods $method): TelegramTypes |
|
110 | |||
111 | /** |
||
112 | * @param TelegramMethods $method |
||
113 | * |
||
114 | * @return PromiseInterface |
||
115 | */ |
||
116 | public function performAsyncApiRequest(TelegramMethods $method) |
||
122 | |||
123 | /** |
||
124 | * Will download a file from the Telegram server. Before calling this function, you have to call the getFile method! |
||
125 | * |
||
126 | * @see \unreal4u\TelegramAPI\Telegram\Types\File |
||
127 | * @see \unreal4u\TelegramAPI\Telegram\Methods\GetFile |
||
128 | * |
||
129 | * @param File $file |
||
130 | * @return TelegramDocument |
||
131 | */ |
||
132 | public function downloadFile(File $file): TelegramDocument |
||
139 | |||
140 | /** |
||
141 | * @param File $file |
||
142 | * |
||
143 | * @return PromiseInterface |
||
144 | */ |
||
145 | public function downloadFileAsync(File $file): PromiseInterface |
||
165 | |||
166 | /** |
||
167 | * Builds up the Telegram API url |
||
168 | * @return TgLog |
||
169 | */ |
||
170 | 16 | final private function constructApiUrl(): TgLog |
|
176 | |||
177 | /** |
||
178 | * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work |
||
179 | * |
||
180 | * @param TelegramMethods $method |
||
181 | * @param array $formData |
||
182 | * |
||
183 | * @return TelegramRawData |
||
184 | * @throws \Exception |
||
185 | */ |
||
186 | protected function sendRequestToTelegram(TelegramMethods $method, array $formData): TelegramRawData |
||
202 | |||
203 | /** |
||
204 | * @param TelegramMethods $method |
||
205 | * @param array $formData |
||
206 | * |
||
207 | * @return PromiseInterface |
||
208 | */ |
||
209 | protected function sendAsyncRequestToTelegram(TelegramMethods $method, array $formData): PromiseInterface |
||
229 | |||
230 | /** |
||
231 | * Resets everything to the default values |
||
232 | * |
||
233 | * @return TgLog |
||
234 | */ |
||
235 | 7 | private function resetObjectValues(): TgLog |
|
242 | |||
243 | /** |
||
244 | * Builds up the form elements to be sent to Telegram |
||
245 | * |
||
246 | * @TODO Move this to apart function |
||
247 | * |
||
248 | * @param TelegramMethods $method |
||
249 | * @return array |
||
250 | * @throws \unreal4u\TelegramAPI\Exceptions\MissingMandatoryField |
||
251 | */ |
||
252 | 7 | private function constructFormData(TelegramMethods $method): array |
|
288 | |||
289 | /** |
||
290 | * Can perform any special checks needed to be performed before sending the actual request to Telegram |
||
291 | * |
||
292 | * This will return an array with data that will be different in each case (for now). This can be changed in the |
||
293 | * future. |
||
294 | * |
||
295 | * @param TelegramMethods $method |
||
296 | * @return array |
||
297 | */ |
||
298 | 7 | private function checkSpecialConditions(TelegramMethods $method): array |
|
319 | |||
320 | /** |
||
321 | * Builds up the URL with which we can work with |
||
322 | * |
||
323 | * All methods in the Bot API are case-insensitive. |
||
324 | * All queries must be made using UTF-8. |
||
325 | * |
||
326 | * @see https://core.telegram.org/bots/api#making-requests |
||
327 | * |
||
328 | * @param TelegramMethods $call |
||
329 | * @return string |
||
330 | */ |
||
331 | 6 | protected function composeApiMethodUrl(TelegramMethods $call): string |
|
339 | |||
340 | /** |
||
341 | * Builds up a multipart form-like array for Guzzle |
||
342 | * |
||
343 | * @param array $data The original object in array form |
||
344 | * @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is |
||
345 | * @param resource $stream The actual file handler |
||
346 | * @return array Returns the actual formdata to be sent |
||
347 | */ |
||
348 | private function buildMultipartFormData(array $data, string $fileKeyName, $stream): array |
||
375 | |||
376 | /** |
||
377 | * @param TelegramRawData $telegramRawData |
||
378 | * @return TgLog |
||
379 | * @throws CustomClientException |
||
380 | */ |
||
381 | private function handleOffErrorRequest(TelegramRawData $telegramRawData): TgLog |
||
393 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: