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 | 32 | 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 | 19 | public function performApiRequest(TelegramMethods $method): TelegramTypes |
|
73 | |||
74 | /** |
||
75 | * Will download a file from the Telegram server. Before calling this function, you have to call the getFile method! |
||
76 | * |
||
77 | * @see unreal4u\Telegram\Types\File |
||
78 | * @see unreal4u\Telegram\Methods\GetFile |
||
79 | * |
||
80 | * @param File $file |
||
81 | * @return TelegramDocument |
||
82 | */ |
||
83 | public function downloadFile(File $file): TelegramDocument |
||
89 | |||
90 | /** |
||
91 | * Builds up the Telegram API url |
||
92 | * @return TgLog |
||
93 | */ |
||
94 | 32 | final private function constructApiUrl(): TgLog |
|
99 | |||
100 | /** |
||
101 | * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work |
||
102 | * |
||
103 | * @param TelegramMethods $method |
||
104 | * @param array $formData |
||
105 | * @return \stdClass |
||
106 | */ |
||
107 | protected function sendRequestToTelegram(TelegramMethods $method, array $formData): \stdClass |
||
113 | |||
114 | 19 | private function resetObjectValues(): TgLog |
|
121 | |||
122 | 19 | private function constructFormData(TelegramMethods $method): array |
|
142 | |||
143 | /** |
||
144 | * Can perform any special checks needed to be performed before sending the actual request to Telegram |
||
145 | * |
||
146 | * This will return an array with data that will be different in each case (for now). This can be changed in the |
||
147 | * future. |
||
148 | * |
||
149 | * @param TelegramMethods $method |
||
150 | * @return array |
||
151 | */ |
||
152 | 19 | private function checkSpecialConditions(TelegramMethods $method): array |
|
175 | |||
176 | /** |
||
177 | * Builds up the URL with which we can work with |
||
178 | * |
||
179 | * @param TelegramMethods $call |
||
180 | * @return string |
||
181 | */ |
||
182 | 19 | 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 |