Completed
Pull Request — master (#55)
by Rick
03:55 queued 01:59
created

TgLog::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\TelegramAPI;
6
7
use Psr\Log\LoggerInterface;
8
use React\Promise\Deferred;
9
use React\Promise\PromiseInterface;
10
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
11
use unreal4u\TelegramAPI\InternalFunctionality\DummyLogger;
12
use unreal4u\TelegramAPI\InternalFunctionality\PostOptionsConstructor;
13
use unreal4u\TelegramAPI\InternalFunctionality\TelegramDocument;
14
use unreal4u\TelegramAPI\InternalFunctionality\TelegramResponse;
15
use unreal4u\TelegramAPI\Telegram\Types\File;
16
17
/**
18
 * The main API which does it all
19
 */
20
class TgLog
21
{
22
    /**
23
     * @var RequestHandlerInterface
24
     */
25
    protected $requestHandler;
26
27
    /**
28
     * @var PostOptionsConstructor
29
     */
30
    protected $formConstructor;
31
32
    /**
33
     * Stores the token
34
     * @var string
35
     */
36
    private $botToken;
37
38
    /**
39
     * Contains an instance to a PSR-3 compatible logger
40
     * @var LoggerInterface
41
     */
42
    protected $logger;
43
44
    /**
45
     * Stores the API URL from Telegram
46
     * @var string
47
     */
48
    private $apiUrl = '';
49
50
    /**
51
     * @var string
52
     */
53
    protected $methodName = '';
54
55
    /**
56
     * TelegramLog constructor.
57
     *
58
     * @param string $botToken
59
     * @param RequestHandlerInterface $handler
60
     * @param LoggerInterface $logger
61
     */
62 40
    public function __construct(string $botToken, RequestHandlerInterface $handler, LoggerInterface $logger = null)
63
    {
64 40
        $this->botToken = $botToken;
65
66
        // Initialize new dummy logger (PSR-3 compatible) if not injected
67 40
        if ($logger === null) {
68 40
            $logger = new DummyLogger();
69
        }
70 40
        $this->logger = $logger;
71
72 40
        $this->requestHandler = $handler;
73 40
        $this->formConstructor = new PostOptionsConstructor();
74 40
        $this->apiUrl = 'https://api.telegram.org/bot' . $this->botToken . '/';
75 40
    }
76
77
    /**
78
     * @param TelegramMethods $method
79
     *
80
     * @return PromiseInterface
81
     */
82 31
    public function performApiRequest(TelegramMethods $method)
83
    {
84 31
        $this->logger->debug('Request for async API call, resetting internal values', [get_class($method)]);
85 31
        $this->resetObjectValues();
86 31
        $option = $this->formConstructor->constructOptions($method);
87 29
        return $this->sendRequestToTelegram($method, $option)
88
            ->then(function (TelegramResponse $response) use ($method) {
89 24
                return $method::bindToObject($response, $this->logger);
90 24
            });
91
    }
92
93
    /**
94
     * @param File $file
95
     *
96
     * @return PromiseInterface
97
     */
98
    public function downloadFile(File $file): PromiseInterface
99
    {
100
        $this->logger->debug('Downloading file async from Telegram, creating URL');
101
        $url = 'https://api.telegram.org/file/bot' . $this->botToken . '/' . $file->file_path;
102
        $this->logger->debug('About to perform request to begin downloading file');
103
104
        $deferred = new Deferred();
105
106
        return $this->requestHandler->get($url)->then(
107
            function (TelegramResponse $rawData) use ($deferred) {
108
                $deferred->resolve(new TelegramDocument($rawData));
109
            },
110
            function (\Exception $exception) use ($deferred) {
111
                $deferred->reject($exception);
112
            }
113
        );
114
    }
115
116
    /**
117
     * @param TelegramMethods $method
118
     * @param array $formData
119
     *
120
     * @return PromiseInterface
121
     */
122
    protected function sendRequestToTelegram(TelegramMethods $method, array $formData): PromiseInterface
123
    {
124
        $this->logger->debug('About to perform async HTTP call to Telegram\'s API');
125
        return $this->requestHandler->post($this->composeApiMethodUrl($method), $formData);
126
    }
127
128
    /**
129
     * Resets everything to the default values
130
     *
131
     * @return TgLog
132
     */
133 31
    private function resetObjectValues(): TgLog
134
    {
135 31
        $this->formConstructor->formType = 'application/x-www-form-urlencoded';
136 31
        return $this;
137
    }
138
139
    /**
140
     * Builds up the URL with which we can work with
141
     *
142
     * All methods in the Bot API are case-insensitive.
143
     * All queries must be made using UTF-8.
144
     *
145
     * @see https://core.telegram.org/bots/api#making-requests
146
     *
147
     * @param TelegramMethods $call
148
     * @return string
149
     */
150 30
    protected function composeApiMethodUrl(TelegramMethods $call): string
151
    {
152 30
        $completeClassName = get_class($call);
153 30
        $this->methodName = substr($completeClassName, strrpos($completeClassName, '\\') + 1);
154 30
        $this->logger->info('About to perform API request', ['method' => $this->methodName]);
155
156 30
        return $this->apiUrl . $this->methodName;
157
    }
158
}
159