1 | <?php |
||
16 | class TgLog |
||
17 | { |
||
18 | /** |
||
19 | * Stores the token |
||
20 | * @var string |
||
21 | */ |
||
22 | private $botToken = ''; |
||
23 | |||
24 | /** |
||
25 | * Stores the API URL from Telegram |
||
26 | * @var string |
||
27 | */ |
||
28 | private $apiUrl = ''; |
||
29 | |||
30 | /** |
||
31 | * With this flag we'll know what type of request to send to Telegram |
||
32 | * |
||
33 | * 'application/x-www-form-urlencoded' is the "normal" one, which is simpler and quicker. |
||
34 | * 'multipart/form-data' should be used only to upload documents, photos, etc. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | private $formType = 'application/x-www-form-urlencoded'; |
||
39 | |||
40 | /** |
||
41 | * Stores the last method name used |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $methodName = ''; |
||
45 | |||
46 | /** |
||
47 | * TelegramLog constructor. |
||
48 | * @param string $botToken |
||
49 | */ |
||
50 | 36 | public function __construct(string $botToken) |
|
55 | |||
56 | /** |
||
57 | * Prepares and sends an API request to Telegram |
||
58 | * |
||
59 | * @param mixed $method |
||
60 | * @return mixed |
||
61 | */ |
||
62 | 21 | public function performApiRequest(TelegramMethods $method): TelegramTypes |
|
70 | |||
71 | /** |
||
72 | * Will download a file from the Telegram server. Before calling this function, you have to call the getFile method! |
||
73 | * |
||
74 | * @see unreal4u\Telegram\Types\File |
||
75 | * @see unreal4u\Telegram\Methods\GetFile |
||
76 | * |
||
77 | * @param File $file |
||
78 | * @return TelegramDocument |
||
79 | */ |
||
80 | public function downloadFile(File $file): TelegramDocument |
||
86 | |||
87 | /** |
||
88 | * Builds up the Telegram API url |
||
89 | * @return TgLog |
||
90 | */ |
||
91 | 36 | final private function constructApiUrl(): TgLog |
|
96 | |||
97 | /** |
||
98 | * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work |
||
99 | * |
||
100 | * @param TelegramMethods $method |
||
101 | * @param array $formData |
||
102 | * @return array |
||
103 | */ |
||
104 | protected function sendRequestToTelegram(TelegramMethods $method, array $formData): array |
||
110 | |||
111 | 21 | private function resetObjectValues(): TgLog |
|
118 | |||
119 | 21 | private function constructFormData(TelegramMethods $method): array |
|
139 | |||
140 | /** |
||
141 | * Can perform any special checks needed to be performed before sending the actual request to Telegram |
||
142 | * |
||
143 | * This will return an array with data that will be different in each case (for now). This can be changed in the |
||
144 | * future. |
||
145 | * |
||
146 | * @param TelegramMethods $method |
||
147 | * @return array |
||
148 | */ |
||
149 | 21 | private function checkSpecialConditions(TelegramMethods $method): array |
|
170 | |||
171 | /** |
||
172 | * Builds up the URL with which we can work with |
||
173 | * |
||
174 | * All methods in the Bot API are case-insensitive. |
||
175 | * All queries must be made using UTF-8. |
||
176 | * |
||
177 | * @see https://core.telegram.org/bots/api#making-requests |
||
178 | * |
||
179 | * @param TelegramMethods $call |
||
180 | * @return string |
||
181 | */ |
||
182 | 21 | protected function composeApiMethodUrl(TelegramMethods $call): string |
|
189 | |||
190 | /** |
||
191 | * Builds up a multipart form-like array for Guzzle |
||
192 | * |
||
193 | * @param array $data The original object in array form |
||
194 | * @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is |
||
195 | * @param resource $stream The actual file handler |
||
196 | * @return array Returns the actual formdata to be sent |
||
197 | */ |
||
198 | 5 | private function buildMultipartFormData(array $data, string $fileKeyName, $stream): array |
|
222 | } |
||
223 |
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.