1 | <?php |
||
18 | class TgLog |
||
19 | { |
||
20 | /** |
||
21 | * Stores the token |
||
22 | * @var string |
||
23 | */ |
||
24 | private $botToken = ''; |
||
25 | |||
26 | /** |
||
27 | * Contains an instance to a PSR-3 compatible logger |
||
28 | * @var LoggerInterface |
||
29 | */ |
||
30 | protected $logger = null; |
||
31 | |||
32 | /** |
||
33 | * Stores the API URL from Telegram |
||
34 | * @var string |
||
35 | */ |
||
36 | private $apiUrl = ''; |
||
37 | |||
38 | /** |
||
39 | * With this flag we'll know what type of request to send to Telegram |
||
40 | * |
||
41 | * 'application/x-www-form-urlencoded' is the "normal" one, which is simpler and quicker. |
||
42 | * 'multipart/form-data' should be used only to upload documents, photos, etc. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $formType = 'application/x-www-form-urlencoded'; |
||
47 | |||
48 | /** |
||
49 | * Stores the last method name used |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $methodName = ''; |
||
53 | |||
54 | /** |
||
55 | * TelegramLog constructor. |
||
56 | * @param string $botToken |
||
57 | */ |
||
58 | 36 | public function __construct(string $botToken, LoggerInterface $logger = null) |
|
69 | |||
70 | /** |
||
71 | * Prepares and sends an API request to Telegram |
||
72 | * |
||
73 | * @param TelegramMethods $method |
||
74 | * @return TelegramTypes |
||
75 | */ |
||
76 | 22 | public function performApiRequest(TelegramMethods $method): TelegramTypes |
|
86 | |||
87 | /** |
||
88 | * Will download a file from the Telegram server. Before calling this function, you have to call the getFile method! |
||
89 | * |
||
90 | * @see unreal4u\Telegram\Types\File |
||
91 | * @see unreal4u\Telegram\Methods\GetFile |
||
92 | * |
||
93 | * @param File $file |
||
94 | * @return TelegramDocument |
||
95 | */ |
||
96 | public function downloadFile(File $file): TelegramDocument |
||
104 | |||
105 | /** |
||
106 | * Builds up the Telegram API url |
||
107 | * @return TgLog |
||
108 | */ |
||
109 | 36 | final private function constructApiUrl(): TgLog |
|
115 | |||
116 | /** |
||
117 | * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work |
||
118 | * |
||
119 | * @param TelegramMethods $method |
||
120 | * @param array $formData |
||
121 | * @return array |
||
122 | */ |
||
123 | protected function sendRequestToTelegram(TelegramMethods $method, array $formData): array |
||
131 | |||
132 | 22 | private function resetObjectValues(): TgLog |
|
139 | |||
140 | 22 | private function constructFormData(TelegramMethods $method): array |
|
162 | |||
163 | /** |
||
164 | * Can perform any special checks needed to be performed before sending the actual request to Telegram |
||
165 | * |
||
166 | * This will return an array with data that will be different in each case (for now). This can be changed in the |
||
167 | * future. |
||
168 | * |
||
169 | * @param TelegramMethods $method |
||
170 | * @return array |
||
171 | */ |
||
172 | 22 | private function checkSpecialConditions(TelegramMethods $method): array |
|
195 | |||
196 | /** |
||
197 | * Builds up the URL with which we can work with |
||
198 | * |
||
199 | * All methods in the Bot API are case-insensitive. |
||
200 | * All queries must be made using UTF-8. |
||
201 | * |
||
202 | * @see https://core.telegram.org/bots/api#making-requests |
||
203 | * |
||
204 | * @param TelegramMethods $call |
||
205 | * @return string |
||
206 | */ |
||
207 | 22 | protected function composeApiMethodUrl(TelegramMethods $call): string |
|
215 | |||
216 | /** |
||
217 | * Builds up a multipart form-like array for Guzzle |
||
218 | * |
||
219 | * @param array $data The original object in array form |
||
220 | * @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is |
||
221 | * @param resource $stream The actual file handler |
||
222 | * @return array Returns the actual formdata to be sent |
||
223 | */ |
||
224 | 4 | private function buildMultipartFormData(array $data, string $fileKeyName, $stream): array |
|
249 | } |
||
250 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.