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 | }, function ($error) { |
||
93 | $this->logger->error($error); |
||
94 | throw $error; |
||
95 | 24 | }); |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param File $file |
||
100 | * |
||
101 | * @return PromiseInterface |
||
102 | */ |
||
103 | public function downloadFile(File $file): PromiseInterface |
||
104 | { |
||
105 | $url = 'https://api.telegram.org/file/bot' . $this->botToken . '/' . $file->file_path; |
||
106 | $this->logger->debug('About to perform request to begin downloading file'); |
||
107 | |||
108 | return $this->requestHandler->get($url)->then( |
||
109 | function (TelegramResponse $rawData) { |
||
110 | return new TelegramDocument($rawData); |
||
111 | } |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param TelegramMethods $method |
||
117 | * @param array $formData |
||
118 | * |
||
119 | * @return PromiseInterface |
||
120 | */ |
||
121 | protected function sendRequestToTelegram(TelegramMethods $method, array $formData): PromiseInterface |
||
126 | |||
127 | /** |
||
128 | * Resets everything to the default values |
||
129 | * |
||
130 | * @return TgLog |
||
131 | */ |
||
132 | 31 | final protected function resetObjectValues(): TgLog |
|
137 | |||
138 | /** |
||
139 | * Builds up the URL with which we can work with |
||
140 | * |
||
141 | * All methods in the Bot API are case-insensitive. |
||
142 | * All queries must be made using UTF-8. |
||
143 | * |
||
144 | * @see https://core.telegram.org/bots/api#making-requests |
||
145 | * |
||
146 | * @param TelegramMethods $call |
||
147 | * @return string |
||
148 | */ |
||
149 | 30 | protected function composeApiMethodUrl(TelegramMethods $call): string |
|
157 | } |
||
158 |