1 | <?php |
||
19 | class TgLog |
||
20 | { |
||
21 | /** |
||
22 | * @var RequestHandlerInterface |
||
23 | */ |
||
24 | protected $requestHandler; |
||
25 | |||
26 | /** |
||
27 | * @var PostOptionsConstructor |
||
28 | */ |
||
29 | protected $formConstructor; |
||
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 | * @var string |
||
51 | */ |
||
52 | protected $methodName = ''; |
||
53 | |||
54 | /** |
||
55 | * TelegramLog constructor. |
||
56 | * |
||
57 | * @param string $botToken |
||
58 | * @param RequestHandlerInterface $handler |
||
59 | * @param LoggerInterface $logger |
||
60 | */ |
||
61 | 40 | public function __construct(string $botToken, RequestHandlerInterface $handler, LoggerInterface $logger = null) |
|
75 | |||
76 | /** |
||
77 | * Performs the request to the Telegram servers |
||
78 | * |
||
79 | * @param TelegramMethods $method |
||
80 | * |
||
81 | * @return PromiseInterface |
||
82 | * @throws \unreal4u\TelegramAPI\Exceptions\MissingMandatoryField |
||
83 | */ |
||
84 | 31 | public function performApiRequest(TelegramMethods $method): PromiseInterface |
|
85 | { |
||
86 | 31 | $this->logger->debug('Request for async API call, resetting internal values', [get_class($method)]); |
|
87 | 31 | $this->resetObjectValues(); |
|
88 | 31 | $option = $this->formConstructor->constructOptions($method); |
|
89 | 29 | return $this->sendRequestToTelegram($method, $option) |
|
90 | 24 | ->then(function (TelegramResponse $response) use ($method) { |
|
91 | 24 | return $method::bindToObject($response, $this->logger); |
|
92 | 24 | }); |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param File $file |
||
97 | * |
||
98 | * @return PromiseInterface |
||
99 | */ |
||
100 | public function downloadFile(File $file): PromiseInterface |
||
111 | |||
112 | /** |
||
113 | * @param TelegramMethods $method |
||
114 | * @param array $formData |
||
115 | * |
||
116 | * @return PromiseInterface |
||
117 | */ |
||
118 | protected function sendRequestToTelegram(TelegramMethods $method, array $formData): PromiseInterface |
||
123 | |||
124 | /** |
||
125 | * Resets everything to the default values |
||
126 | * |
||
127 | * @return TgLog |
||
128 | */ |
||
129 | 31 | final protected function resetObjectValues(): TgLog |
|
134 | |||
135 | /** |
||
136 | * Builds up the URL with which we can work with |
||
137 | * |
||
138 | * All methods in the Bot API are case-insensitive. |
||
139 | * All queries must be made using UTF-8. |
||
140 | * |
||
141 | * @see https://core.telegram.org/bots/api#making-requests |
||
142 | * |
||
143 | * @param TelegramMethods $call |
||
144 | * @return string |
||
145 | */ |
||
146 | 30 | protected function composeApiMethodUrl(TelegramMethods $call): string |
|
154 | } |
||
155 |