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 | 39 | 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 | 30 | public function performApiRequest(TelegramMethods $method): TelegramTypes |
|
100 | { |
||
101 | 30 | $this->logger->debug('Request for API call, resetting internal values', [get_class($method)]); |
|
102 | 30 | $this->resetObjectValues(); |
|
103 | 30 | $telegramRawData = $this->sendRequestToTelegram($method, $this->constructFormData($method)); |
|
104 | 23 | if ($telegramRawData->isError()) { |
|
105 | $this->handleOffErrorRequest($telegramRawData); |
||
106 | } |
||
107 | |||
108 | 23 | return $method::bindToObject($telegramRawData, $this->logger); |
|
109 | } |
||
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 |
||
164 | |||
165 | /** |
||
166 | * Builds up the Telegram API url |
||
167 | * @return TgLog |
||
168 | */ |
||
169 | 39 | final private function constructApiUrl(): TgLog |
|
175 | |||
176 | /** |
||
177 | * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work |
||
178 | * |
||
179 | * @param TelegramMethods $method |
||
180 | * @param array $formData |
||
181 | * |
||
182 | * @return TelegramRawData |
||
183 | * @throws \Exception |
||
184 | */ |
||
185 | 23 | protected function sendRequestToTelegram(TelegramMethods $method, array $formData): TelegramRawData |
|
201 | |||
202 | /** |
||
203 | * @param TelegramMethods $method |
||
204 | * @param array $formData |
||
205 | * |
||
206 | * @return PromiseInterface |
||
207 | */ |
||
208 | protected function sendAsyncRequestToTelegram(TelegramMethods $method, array $formData): PromiseInterface |
||
228 | |||
229 | /** |
||
230 | * Resets everything to the default values |
||
231 | * |
||
232 | * @return TgLog |
||
233 | */ |
||
234 | 30 | private function resetObjectValues(): TgLog |
|
241 | |||
242 | /** |
||
243 | * Builds up the form elements to be sent to Telegram |
||
244 | * |
||
245 | * @TODO Move this to apart function |
||
246 | * |
||
247 | * @param TelegramMethods $method |
||
248 | * @return array |
||
249 | * @throws \unreal4u\TelegramAPI\Exceptions\MissingMandatoryField |
||
250 | */ |
||
251 | 30 | private function constructFormData(TelegramMethods $method): array |
|
287 | |||
288 | /** |
||
289 | * Can perform any special checks needed to be performed before sending the actual request to Telegram |
||
290 | * |
||
291 | * This will return an array with data that will be different in each case (for now). This can be changed in the |
||
292 | * future. |
||
293 | * |
||
294 | * @param TelegramMethods $method |
||
295 | * @return array |
||
296 | */ |
||
297 | 30 | private function checkSpecialConditions(TelegramMethods $method): array |
|
318 | |||
319 | /** |
||
320 | * Builds up the URL with which we can work with |
||
321 | * |
||
322 | * All methods in the Bot API are case-insensitive. |
||
323 | * All queries must be made using UTF-8. |
||
324 | * |
||
325 | * @see https://core.telegram.org/bots/api#making-requests |
||
326 | * |
||
327 | * @param TelegramMethods $call |
||
328 | * @return string |
||
329 | */ |
||
330 | 29 | protected function composeApiMethodUrl(TelegramMethods $call): string |
|
338 | |||
339 | /** |
||
340 | * Builds up a multipart form-like array for Guzzle |
||
341 | * |
||
342 | * @param array $data The original object in array form |
||
343 | * @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is |
||
344 | * @param resource $stream The actual file handler |
||
345 | * @return array Returns the actual formdata to be sent |
||
346 | */ |
||
347 | 4 | private function buildMultipartFormData(array $data, string $fileKeyName, $stream): array |
|
374 | |||
375 | /** |
||
376 | * @param TelegramRawData $telegramRawData |
||
377 | * @return TgLog |
||
378 | * @throws CustomClientException |
||
379 | */ |
||
380 | private function handleOffErrorRequest(TelegramRawData $telegramRawData): TgLog |
||
392 | } |
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: