Completed
Pull Request — master (#53)
by Rick
02:24
created

GuzzleRequestHandler::post()   A

Complexity

Conditions 3
Paths 9

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.125

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 6
cts 12
cp 0.5
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 9
nop 2
crap 4.125
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\TelegramAPI;
6
7
use GuzzleHttp\Client;
8
use GuzzleHttp\ClientInterface;
9
use GuzzleHttp\Exception\ClientException;
10
use GuzzleHttp\Exception\RequestException;
11
use React\Promise\PromiseInterface;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Log\LoggerInterface;
14
use React\Promise\Deferred;
15
use unreal4u\TelegramAPI\InternalFunctionality\DummyLogger;
16
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData;
17
18
class GuzzleRequestHandler implements RequestHandlerInterface
19
{
20
    /**
21
     * @var LoggerInterface
22
     */
23
    protected $logger;
24
25
    /**
26
     * @var ClientInterface
27
     */
28
    protected $httpClient;
29
30
    /**
31
     * GuzzleRequestHandler constructor.
32
     *
33
     * @param ClientInterface $client
34
     * @param LoggerInterface $logger
35
     */
36 40
    public function __construct(ClientInterface $client = null, LoggerInterface $logger = null)
37
    {
38 40
        if ($logger === null) {
39 24
            $logger = new DummyLogger();
40
        }
41 40
        $this->logger = $logger;
42
43 40
        if ($client === null) {
44 40
            $client = new Client();
45
        }
46 40
        $this->httpClient = $client;
47 40
    }
48
49
    /**
50
     * @param string $uri
51
     *
52
     * @return ResponseInterface
53
     */
54
    public function get(string $uri): ResponseInterface
55
    {
56
        return $this->httpClient->get($uri);
57
    }
58
59
    /**
60
     * This is the method that actually makes the call,
61
     * which can be easily overwritten so that our unit tests can work
62
     *
63
     * @param string $uri
64
     * @param array $formData
65
     *
66
     * @return TelegramRawData
67
     */
68 24
    public function post(string $uri, array $formData = []): TelegramRawData
69
    {
70 24
        $e = null;
71 24
        $this->logger->debug('About to perform HTTP call to Telegram\'s API');
72
        try {
73 24
            $response = $this->httpClient->post($uri, $formData);
74 24
            $this->logger->debug('Got response back from Telegram, applying json_decode');
75
        } catch (ClientException $e) {
76
            $response = $e->getResponse();
77
            // It can happen that we have a network problem, in such case, we can't do nothing about it, so rethrow
78
            if (empty($response)) {
79
                throw $e;
80
            }
81
        } finally {
82 24
            return new TelegramRawData((string)$response->getBody(), $e);
83
        }
84
    }
85
86
    /**
87
     * @param string $uri
88
     * @param array $formData
89
     *
90
     * @return PromiseInterface
91
     */
92
    public function postAsync(string $uri, array $formData = []): PromiseInterface
93
    {
94
        $this->logger->debug('About to perform async HTTP call to Telegram\'s API');
95
        $deferred = new Deferred();
96
97
        $promise = $this->httpClient->postAsync($uri, $formData);
98
        $promise->then(
99
            function (ResponseInterface $response) use ($deferred) {
100
                $deferred->resolve(new TelegramRawData((string)$response->getBody()));
101
            },
102
            function (RequestException $exception) use ($deferred) {
103
                if (!empty($exception->getResponse()->getBody())) {
104
                    $deferred->resolve(new TelegramRawData((string)$exception->getResponse()
105
                        ->getBody(), $exception));
106
                } else {
107
                    $deferred->reject($exception);
108
                }
109
            }
110
        );
111
112
        return $deferred->promise();
113
    }
114
115
    /**
116
     * @param string $uri
117
     *
118
     * @return PromiseInterface
119
     */
120
    public function getAsync(string $uri): PromiseInterface
121
    {
122
        $this->logger->debug('About to perform async HTTP call to Telegram\'s API');
123
        $deferred = new Deferred();
124
125
        $promise = $this->httpClient->getAsync($uri);
126
        $promise->then(
127
            function (ResponseInterface $response) use ($deferred) {
128
                $deferred->resolve(new TelegramRawData((string)$response->getBody()));
129
            },
130
            function (RequestException $exception) use ($deferred) {
131
                if (!empty($exception->getResponse()->getBody())) {
132
                    $deferred->resolve(new TelegramRawData((string)$exception->getResponse()
133
                        ->getBody(), $exception));
134
                } else {
135
                    $deferred->reject($exception);
136
                }
137
            }
138
        );
139
140
        return $deferred->promise();
141
    }
142
}
143