1 | <?php |
||
19 | class TgLog |
||
20 | { |
||
21 | /** |
||
22 | * @var ClientInterface |
||
23 | */ |
||
24 | protected $httpClient; |
||
25 | |||
26 | /** |
||
27 | * Stores the token |
||
28 | * @var string |
||
29 | */ |
||
30 | private $botToken = ''; |
||
31 | |||
32 | /** |
||
33 | * Contains an instance to a PSR-3 compatible logger |
||
34 | * @var LoggerInterface |
||
35 | */ |
||
36 | protected $logger = null; |
||
37 | |||
38 | /** |
||
39 | * Stores the API URL from Telegram |
||
40 | * @var string |
||
41 | */ |
||
42 | private $apiUrl = ''; |
||
43 | |||
44 | /** |
||
45 | * With this flag we'll know what type of request to send to Telegram |
||
46 | * |
||
47 | * 'application/x-www-form-urlencoded' is the "normal" one, which is simpler and quicker. |
||
48 | * 'multipart/form-data' should be used only to upload documents, photos, etc. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | private $formType = 'application/x-www-form-urlencoded'; |
||
53 | |||
54 | /** |
||
55 | * Stores the last method name used |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $methodName = ''; |
||
59 | |||
60 | /** |
||
61 | * TelegramLog constructor. |
||
62 | * @param string $botToken |
||
63 | */ |
||
64 | 36 | public function __construct(string $botToken, LoggerInterface $logger = null) |
|
76 | |||
77 | /** |
||
78 | * Prepares and sends an API request to Telegram |
||
79 | * |
||
80 | * @param TelegramMethods $method |
||
81 | * @return TelegramTypes |
||
82 | */ |
||
83 | 22 | public function performApiRequest(TelegramMethods $method): TelegramTypes |
|
93 | |||
94 | /** |
||
95 | * Will download a file from the Telegram server. Before calling this function, you have to call the getFile method! |
||
96 | * |
||
97 | * @see unreal4u\Telegram\Types\File |
||
98 | * @see unreal4u\Telegram\Methods\GetFile |
||
99 | * |
||
100 | * @param File $file |
||
101 | * @return TelegramDocument |
||
102 | */ |
||
103 | public function downloadFile(File $file): TelegramDocument |
||
110 | |||
111 | /** |
||
112 | * Builds up the Telegram API url |
||
113 | * @return TgLog |
||
114 | */ |
||
115 | 36 | final private function constructApiUrl(): TgLog |
|
121 | |||
122 | /** |
||
123 | * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work |
||
124 | * |
||
125 | * @param TelegramMethods $method |
||
126 | * @param array $formData |
||
127 | * @return array |
||
128 | */ |
||
129 | protected function sendRequestToTelegram(TelegramMethods $method, array $formData): array |
||
136 | |||
137 | 22 | private function resetObjectValues(): TgLog |
|
144 | |||
145 | 22 | private function constructFormData(TelegramMethods $method): array |
|
167 | |||
168 | /** |
||
169 | * Can perform any special checks needed to be performed before sending the actual request to Telegram |
||
170 | * |
||
171 | * This will return an array with data that will be different in each case (for now). This can be changed in the |
||
172 | * future. |
||
173 | * |
||
174 | * @param TelegramMethods $method |
||
175 | * @return array |
||
176 | */ |
||
177 | 22 | private function checkSpecialConditions(TelegramMethods $method): array |
|
200 | |||
201 | /** |
||
202 | * Builds up the URL with which we can work with |
||
203 | * |
||
204 | * All methods in the Bot API are case-insensitive. |
||
205 | * All queries must be made using UTF-8. |
||
206 | * |
||
207 | * @see https://core.telegram.org/bots/api#making-requests |
||
208 | * |
||
209 | * @param TelegramMethods $call |
||
210 | * @return string |
||
211 | */ |
||
212 | 22 | protected function composeApiMethodUrl(TelegramMethods $call): string |
|
220 | |||
221 | /** |
||
222 | * Builds up a multipart form-like array for Guzzle |
||
223 | * |
||
224 | * @param array $data The original object in array form |
||
225 | * @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is |
||
226 | * @param resource $stream The actual file handler |
||
227 | * @return array Returns the actual formdata to be sent |
||
228 | */ |
||
229 | 4 | private function buildMultipartFormData(array $data, string $fileKeyName, $stream): array |
|
254 | } |
||
255 |