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 | 40 | public function __construct( |
|
94 | |||
95 | /** |
||
96 | * Prepares and sends an API request to Telegram |
||
97 | * |
||
98 | * @param TelegramMethods $method |
||
99 | * @return TelegramTypes |
||
100 | * @throws \unreal4u\TelegramAPI\Exceptions\MissingMandatoryField |
||
101 | */ |
||
102 | 31 | public function performApiRequest(TelegramMethods $method): TelegramTypes |
|
113 | |||
114 | /** |
||
115 | * @param TelegramMethods $method |
||
116 | * |
||
117 | * @return PromiseInterface |
||
118 | */ |
||
119 | public function performAsyncApiRequest(TelegramMethods $method) |
||
125 | |||
126 | /** |
||
127 | * Will download a file from the Telegram server. Before calling this function, you have to call the getFile method! |
||
128 | * |
||
129 | * @see \unreal4u\TelegramAPI\Telegram\Types\File |
||
130 | * @see \unreal4u\TelegramAPI\Telegram\Methods\GetFile |
||
131 | * |
||
132 | * @param File $file |
||
133 | * @return TelegramDocument |
||
134 | */ |
||
135 | public function downloadFile(File $file): TelegramDocument |
||
142 | |||
143 | /** |
||
144 | * @param File $file |
||
145 | * |
||
146 | * @return PromiseInterface |
||
147 | */ |
||
148 | public function downloadFileAsync(File $file): PromiseInterface |
||
169 | |||
170 | /** |
||
171 | * Builds up the Telegram API url |
||
172 | * @return TgLog |
||
173 | */ |
||
174 | 40 | final private function constructApiUrl(): TgLog |
|
180 | |||
181 | /** |
||
182 | * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work |
||
183 | * |
||
184 | * @param TelegramMethods $method |
||
185 | * @param array $formData |
||
186 | * |
||
187 | * @return TelegramRawData |
||
188 | * @throws \Exception |
||
189 | */ |
||
190 | 24 | protected function sendRequestToTelegram(TelegramMethods $method, array $formData): TelegramRawData |
|
206 | |||
207 | /** |
||
208 | * @param TelegramMethods $method |
||
209 | * @param array $formData |
||
210 | * |
||
211 | * @return PromiseInterface |
||
212 | */ |
||
213 | protected function sendAsyncRequestToTelegram(TelegramMethods $method, array $formData): PromiseInterface |
||
234 | |||
235 | /** |
||
236 | * Resets everything to the default values |
||
237 | * |
||
238 | * @return TgLog |
||
239 | */ |
||
240 | 31 | private function resetObjectValues(): TgLog |
|
247 | |||
248 | /** |
||
249 | * Builds up the form elements to be sent to Telegram |
||
250 | * |
||
251 | * @TODO Move this to apart function |
||
252 | * |
||
253 | * @param TelegramMethods $method |
||
254 | * @return array |
||
255 | * @throws \unreal4u\TelegramAPI\Exceptions\MissingMandatoryField |
||
256 | */ |
||
257 | 31 | private function constructFormData(TelegramMethods $method): array |
|
293 | |||
294 | /** |
||
295 | * Can perform any special checks needed to be performed before sending the actual request to Telegram |
||
296 | * |
||
297 | * This will return an array with data that will be different in each case (for now). This can be changed in the |
||
298 | * future. |
||
299 | * |
||
300 | * @param TelegramMethods $method |
||
301 | * @return array |
||
302 | */ |
||
303 | 31 | private function checkSpecialConditions(TelegramMethods $method): array |
|
324 | |||
325 | /** |
||
326 | * Builds up the URL with which we can work with |
||
327 | * |
||
328 | * All methods in the Bot API are case-insensitive. |
||
329 | * All queries must be made using UTF-8. |
||
330 | * |
||
331 | * @see https://core.telegram.org/bots/api#making-requests |
||
332 | * |
||
333 | * @param TelegramMethods $call |
||
334 | * @return string |
||
335 | */ |
||
336 | 30 | protected function composeApiMethodUrl(TelegramMethods $call): string |
|
344 | |||
345 | /** |
||
346 | * Builds up a multipart form-like array for Guzzle |
||
347 | * |
||
348 | * @param array $data The original object in array form |
||
349 | * @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is |
||
350 | * @param resource $stream The actual file handler |
||
351 | * @return array Returns the actual formdata to be sent |
||
352 | */ |
||
353 | 4 | private function buildMultipartFormData(array $data, string $fileKeyName, $stream): array |
|
380 | |||
381 | /** |
||
382 | * @param TelegramRawData $telegramRawData |
||
383 | * @return TgLog |
||
384 | * @throws CustomClientException |
||
385 | */ |
||
386 | private function handleOffErrorRequest(TelegramRawData $telegramRawData): TgLog |
||
398 | } |
||
399 |
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: