1 | <?php |
||
24 | class TgLog |
||
25 | { |
||
26 | /** |
||
27 | * @var ClientInterface |
||
28 | */ |
||
29 | protected $httpClient; |
||
30 | |||
31 | /** |
||
32 | * Stores the token |
||
33 | * @var string |
||
34 | */ |
||
35 | private $botToken; |
||
36 | |||
37 | /** |
||
38 | * Contains an instance to a PSR-3 compatible logger |
||
39 | * @var LoggerInterface |
||
40 | */ |
||
41 | protected $logger; |
||
42 | |||
43 | /** |
||
44 | * Stores the API URL from Telegram |
||
45 | * @var string |
||
46 | */ |
||
47 | private $apiUrl = ''; |
||
48 | |||
49 | /** |
||
50 | * With this flag we'll know what type of request to send to Telegram |
||
51 | * |
||
52 | * 'application/x-www-form-urlencoded' is the "normal" one, which is simpler and quicker. |
||
53 | * 'multipart/form-data' should be used only when you upload documents, photos, etc. |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | private $formType = 'application/x-www-form-urlencoded'; |
||
58 | |||
59 | /** |
||
60 | * Stores the last method name used |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $methodName = ''; |
||
64 | |||
65 | /** |
||
66 | * TelegramLog constructor. |
||
67 | * @param string $botToken |
||
68 | * @param LoggerInterface $logger |
||
69 | * @param Client $client Optional Guzzle object |
||
70 | */ |
||
71 | 39 | public function __construct(string $botToken, LoggerInterface $logger = null, Client $client = null) |
|
89 | |||
90 | /** |
||
91 | * Prepares and sends an API request to Telegram |
||
92 | * |
||
93 | * @param TelegramMethods $method |
||
94 | * @return TelegramTypes |
||
95 | * @throws \unreal4u\TelegramAPI\Exceptions\MissingMandatoryField |
||
96 | */ |
||
97 | 30 | public function performApiRequest(TelegramMethods $method): TelegramTypes |
|
108 | |||
109 | /** |
||
110 | * Will download a file from the Telegram server. Before calling this function, you have to call the getFile method! |
||
111 | * |
||
112 | * @see unreal4u\TelegramAPI\Telegram\Types\File |
||
113 | * @see unreal4u\TelegramAPI\Telegram\Methods\GetFile |
||
114 | * |
||
115 | * @param File $file |
||
116 | * @return TelegramDocument |
||
117 | */ |
||
118 | public function downloadFile(File $file): TelegramDocument |
||
125 | |||
126 | /** |
||
127 | * Builds up the Telegram API url |
||
128 | * @return TgLog |
||
129 | */ |
||
130 | 39 | final private function constructApiUrl(): TgLog |
|
136 | |||
137 | /** |
||
138 | * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work |
||
139 | * |
||
140 | * @param TelegramMethods $method |
||
141 | * @param array $formData |
||
142 | * @return TelegramRawData |
||
143 | */ |
||
144 | 23 | protected function sendRequestToTelegram(TelegramMethods $method, array $formData): TelegramRawData |
|
162 | |||
163 | /** |
||
164 | * Resets everything to the default values |
||
165 | * |
||
166 | * @return TgLog |
||
167 | */ |
||
168 | 30 | private function resetObjectValues(): TgLog |
|
175 | |||
176 | /** |
||
177 | * Builds up the form elements to be sent to Telegram |
||
178 | * |
||
179 | * @TODO Move this to apart function |
||
180 | * |
||
181 | * @param TelegramMethods $method |
||
182 | * @return array |
||
183 | * @throws \unreal4u\TelegramAPI\Exceptions\MissingMandatoryField |
||
184 | */ |
||
185 | 30 | private function constructFormData(TelegramMethods $method): array |
|
214 | |||
215 | /** |
||
216 | * Can perform any special checks needed to be performed before sending the actual request to Telegram |
||
217 | * |
||
218 | * This will return an array with data that will be different in each case (for now). This can be changed in the |
||
219 | * future. |
||
220 | * |
||
221 | * @param TelegramMethods $method |
||
222 | * @return array |
||
223 | */ |
||
224 | 30 | private function checkSpecialConditions(TelegramMethods $method): array |
|
245 | |||
246 | /** |
||
247 | * Builds up the URL with which we can work with |
||
248 | * |
||
249 | * All methods in the Bot API are case-insensitive. |
||
250 | * All queries must be made using UTF-8. |
||
251 | * |
||
252 | * @see https://core.telegram.org/bots/api#making-requests |
||
253 | * |
||
254 | * @param TelegramMethods $call |
||
255 | * @return string |
||
256 | */ |
||
257 | 29 | protected function composeApiMethodUrl(TelegramMethods $call): string |
|
265 | |||
266 | /** |
||
267 | * Builds up a multipart form-like array for Guzzle |
||
268 | * |
||
269 | * @param array $data The original object in array form |
||
270 | * @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is |
||
271 | * @param resource $stream The actual file handler |
||
272 | * @return array Returns the actual formdata to be sent |
||
273 | */ |
||
274 | 4 | private function buildMultipartFormData(array $data, string $fileKeyName, $stream): array |
|
299 | |||
300 | /** |
||
301 | * @param TelegramRawData $telegramRawData |
||
302 | * @return TgLog |
||
303 | * @throws CustomClientException |
||
304 | */ |
||
305 | private function handleOffErrorRequest(TelegramRawData $telegramRawData): TgLog |
||
317 | } |
||
318 |