1 | <?php |
||
14 | class TgLog |
||
15 | { |
||
16 | /** |
||
17 | * Stores the token |
||
18 | * @var string |
||
19 | */ |
||
20 | private $botToken = ''; |
||
21 | |||
22 | /** |
||
23 | * Stores the API URL from Telegram |
||
24 | * @var string |
||
25 | */ |
||
26 | private $apiUrl = ''; |
||
27 | |||
28 | /** |
||
29 | * With this flag we'll know what type of request to send to Telegram |
||
30 | * |
||
31 | * 'application/x-www-form-urlencoded' is the "normal" one, which is simpler and quicker. |
||
32 | * 'multipart/form-data' should be used only to upload documents, photos, etc. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $formType = 'application/x-www-form-urlencoded'; |
||
37 | |||
38 | /** |
||
39 | * Stores the last method name used |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $methodName = ''; |
||
43 | |||
44 | /** |
||
45 | * TelegramLog constructor. |
||
46 | * @param string $botToken |
||
47 | */ |
||
48 | 32 | public function __construct(string $botToken) |
|
53 | |||
54 | /** |
||
55 | * Prepares and sends an API request to Telegram |
||
56 | * |
||
57 | * @param mixed $method |
||
58 | * @return mixed |
||
59 | */ |
||
60 | 19 | public function performApiRequest($method) |
|
68 | |||
69 | /** |
||
70 | * Will download a file from the Telegram server. Before calling this function, you have to call the getFile method! |
||
71 | * |
||
72 | * @see unreal4u\Telegram\Types\File |
||
73 | * @see unreal4u\Telegram\Methods\GetFile |
||
74 | * |
||
75 | * @param File $file |
||
76 | * @return TelegramDocument |
||
77 | */ |
||
78 | public function downloadFile(File $file): TelegramDocument |
||
84 | |||
85 | /** |
||
86 | * Builds up the Telegram API url |
||
87 | * @return TgLog |
||
88 | */ |
||
89 | 32 | final private function constructApiUrl(): TgLog |
|
94 | |||
95 | /** |
||
96 | * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work |
||
97 | * |
||
98 | * @param $method |
||
99 | * @param array $formData |
||
100 | * @return \stdClass |
||
101 | */ |
||
102 | protected function sendRequestToTelegram($method, array $formData): \stdClass |
||
108 | |||
109 | 19 | private function resetObjectValues(): TgLog |
|
116 | |||
117 | 19 | private function constructFormData($method): array |
|
118 | { |
||
119 | 19 | $result = $this->checkSpecialConditions($method); |
|
120 | |||
121 | 19 | switch ($this->formType) { |
|
122 | case 'application/x-www-form-urlencoded': |
||
123 | $formData = [ |
||
124 | 15 | 'form_params' => get_object_vars($method), |
|
125 | ]; |
||
126 | 15 | break; |
|
127 | case 'multipart/form-data': |
||
128 | 4 | $formData = $this->buildMultipartFormData(get_object_vars($method), $result['id'], $result['stream']); |
|
129 | 4 | break; |
|
130 | default: |
||
131 | $formData = []; |
||
132 | break; |
||
133 | } |
||
134 | |||
135 | 19 | return $formData; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * Can perform any special checks needed to be performed before sending the actual request to Telegram |
||
140 | * |
||
141 | * This will return an array with data that will be different in each case (for now). This can be changed in the |
||
142 | * future. |
||
143 | * |
||
144 | * @param $method |
||
145 | * @return array |
||
146 | */ |
||
147 | 19 | private function checkSpecialConditions($method): array |
|
148 | { |
||
149 | 19 | $return = [false]; |
|
150 | |||
151 | 19 | foreach ($method as $key => $value) { |
|
152 | 17 | if (is_object($value)) { |
|
153 | 5 | if (get_class($value) == 'unreal4u\\Telegram\\Types\\Custom\\InputFile') { |
|
154 | // If we are about to send a file, we must use the multipart/form-data way |
||
155 | 4 | $this->formType = 'multipart/form-data'; |
|
156 | $return = [ |
||
157 | 4 | 'id' => $key, |
|
158 | 4 | 'stream' => $value->getStream(), |
|
159 | ]; |
||
160 | 1 | } elseif (in_array('unreal4u\\InternalFunctionality\\AbstractKeyboardMethods', class_parents($value))) { |
|
161 | // If we are about to send a KeyboardMethod, we must send a serialized object |
||
162 | 1 | $method->$key = json_encode($value); |
|
163 | 17 | $return = [true]; |
|
164 | } |
||
165 | } |
||
166 | } |
||
167 | |||
168 | 19 | return $return; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * Builds up the URL with which we can work with |
||
173 | * |
||
174 | * @param $call |
||
175 | * @return string |
||
176 | */ |
||
177 | 19 | protected function composeApiMethodUrl($call): string |
|
184 | |||
185 | /** |
||
186 | * Builds up a multipart form-like array for Guzzle |
||
187 | * |
||
188 | * @param array $data The original object in array form |
||
189 | * @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is |
||
190 | * @param mixed $stream The actual file handler |
||
191 | * @return array Returns the actual formdata to be sent |
||
192 | */ |
||
193 | 5 | private function buildMultipartFormData(array $data, string $fileKeyName, $stream): array |
|
217 | } |
||
218 |
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.