1 | <?php |
||
24 | class TgLog |
||
25 | { |
||
26 | /** |
||
27 | * @var RequestHandlerInterface |
||
28 | */ |
||
29 | protected $requestHandler; |
||
30 | |||
31 | /** |
||
32 | * @var FormConstructor |
||
33 | */ |
||
34 | protected $formConstructor; |
||
35 | |||
36 | /** |
||
37 | * Stores the token |
||
38 | * @var string |
||
39 | */ |
||
40 | private $botToken; |
||
41 | |||
42 | /** |
||
43 | * Contains an instance to a PSR-3 compatible logger |
||
44 | * @var LoggerInterface |
||
45 | */ |
||
46 | protected $logger; |
||
47 | |||
48 | /** |
||
49 | * Stores the API URL from Telegram |
||
50 | * @var string |
||
51 | */ |
||
52 | private $apiUrl = ''; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $methodName = ''; |
||
58 | |||
59 | /** |
||
60 | * TelegramLog constructor. |
||
61 | * |
||
62 | * @param string $botToken |
||
63 | * @param LoggerInterface $logger |
||
64 | * @param RequestHandlerInterface $handler |
||
65 | */ |
||
66 | 40 | public function __construct( |
|
88 | |||
89 | /** |
||
90 | * Prepares and sends an API request to Telegram |
||
91 | * |
||
92 | * @param TelegramMethods $method |
||
93 | * @return TelegramTypes |
||
94 | * @throws \unreal4u\TelegramAPI\Exceptions\MissingMandatoryField |
||
95 | */ |
||
96 | 31 | public function performApiRequest(TelegramMethods $method): TelegramTypes |
|
107 | |||
108 | /** |
||
109 | * @param TelegramMethods $method |
||
110 | * |
||
111 | * @return PromiseInterface |
||
112 | */ |
||
113 | public function performAsyncApiRequest(TelegramMethods $method) |
||
119 | |||
120 | /** |
||
121 | * Will download a file from the Telegram server. Before calling this function, you have to call the getFile method! |
||
122 | * |
||
123 | * @see \unreal4u\TelegramAPI\Telegram\Types\File |
||
124 | * @see \unreal4u\TelegramAPI\Telegram\Methods\GetFile |
||
125 | * |
||
126 | * @param File $file |
||
127 | * @return TelegramDocument |
||
128 | */ |
||
129 | public function downloadFile(File $file): TelegramDocument |
||
136 | |||
137 | /** |
||
138 | * @param File $file |
||
139 | * |
||
140 | * @return PromiseInterface |
||
141 | */ |
||
142 | public function downloadFileAsync(File $file): PromiseInterface |
||
163 | |||
164 | /** |
||
165 | * Builds up the Telegram API url |
||
166 | * @return TgLog |
||
167 | */ |
||
168 | 40 | final private function constructApiUrl(): TgLog |
|
174 | |||
175 | /** |
||
176 | * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work |
||
177 | * |
||
178 | * @param TelegramMethods $method |
||
179 | * @param array $formData |
||
180 | * |
||
181 | * @return TelegramRawData |
||
182 | * @throws \Exception |
||
183 | */ |
||
184 | 24 | protected function sendRequestToTelegram(TelegramMethods $method, array $formData): TelegramRawData |
|
200 | |||
201 | /** |
||
202 | * @param TelegramMethods $method |
||
203 | * @param array $formData |
||
204 | * |
||
205 | * @return PromiseInterface |
||
206 | */ |
||
207 | protected function sendAsyncRequestToTelegram(TelegramMethods $method, array $formData): PromiseInterface |
||
228 | |||
229 | /** |
||
230 | * Resets everything to the default values |
||
231 | * |
||
232 | * @return TgLog |
||
233 | */ |
||
234 | 31 | private function resetObjectValues(): TgLog |
|
239 | |||
240 | /** |
||
241 | * Builds up the URL with which we can work with |
||
242 | * |
||
243 | * All methods in the Bot API are case-insensitive. |
||
244 | * All queries must be made using UTF-8. |
||
245 | * |
||
246 | * @see https://core.telegram.org/bots/api#making-requests |
||
247 | * |
||
248 | * @param TelegramMethods $call |
||
249 | * @return string |
||
250 | */ |
||
251 | 30 | protected function composeApiMethodUrl(TelegramMethods $call): string |
|
259 | |||
260 | /** |
||
261 | * @param TelegramRawData $telegramRawData |
||
262 | * @return TgLog |
||
263 | * @throws CustomClientException |
||
264 | */ |
||
265 | private function handleOffErrorRequest(TelegramRawData $telegramRawData): TgLog |
||
277 | } |
||
278 |
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: