Completed
Pull Request — master (#55)
by Rick
08:13
created

GuzzleRequestHandler::postAsync()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 13
cp 0
rs 9.2
c 0
b 0
f 0
cc 2
eloc 14
nc 1
nop 2
crap 6
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 Psr\Http\Message\ResponseInterface;
12
use Psr\Log\LoggerInterface;
13
use React\Promise\Deferred;
14
use React\Promise\PromiseInterface;
15
use unreal4u\TelegramAPI\InternalFunctionality\DummyLogger;
16
use unreal4u\TelegramAPI\InternalFunctionality\TelegramResponse;
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
    public function __construct(ClientInterface $client = null, LoggerInterface $logger = null)
37 16
    {
38
        if ($logger === null) {
39 16
            $logger = new DummyLogger();
40
        }
41
        $this->logger = $logger;
42
43 16
        if ($client === null) {
44
            $client = new Client();
45 16
        }
46
        $this->httpClient = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client can also be of type object<GuzzleHttp\Client>. However, the property $httpClient is declared as type object<GuzzleHttp\ClientInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47 16
    }
48
49 16
    /**
50 16
     * @param string $uri
51
     * @param array $formData
52
     *
53
     * @return PromiseInterface
54
     */
55
    public function post(string $uri, array $formData = []): PromiseInterface
56
    {
57
        $this->logger->debug('About to perform async HTTP call to Telegram\'s API');
58
        $deferred = new Deferred();
59
60
        $promise = $this->httpClient->postAsync($uri, $formData);
61
        $promise->then(
62
            function (ResponseInterface $response) use ($deferred) {
63
                $deferred->resolve(new TelegramResponse((string)$response->getBody()));
64
            },
65
            function (RequestException $exception) use ($deferred) {
66
                if (!empty($exception->getResponse()->getBody())) {
67
                    $deferred->resolve(new TelegramResponse((string)$exception->getResponse()
68
                        ->getBody(), $exception));
69
                } else {
70
                    $deferred->reject($exception);
71
                }
72
            }
73
        );
74
75
        return $deferred->promise();
76
    }
77
78
    /**
79
     * @param string $uri
80
     *
81
     * @return PromiseInterface
82
     */
83
    public function get(string $uri): PromiseInterface
84
    {
85
        $this->logger->debug('About to perform async HTTP call to Telegram\'s API');
86
        $deferred = new Deferred();
87
88
        $promise = $this->httpClient->getAsync($uri);
89
        $promise->then(
90
            function (ResponseInterface $response) use ($deferred) {
91
                $deferred->resolve(new TelegramResponse((string)$response->getBody()));
92
            },
93
            function (RequestException $exception) use ($deferred) {
94
                if (!empty($exception->getResponse()->getBody())) {
95
                    $deferred->resolve(new TelegramResponse((string)$exception->getResponse()
96
                        ->getBody(), $exception));
97
                } else {
98
                    $deferred->reject($exception);
99
                }
100
            }
101
        );
102
103
        return $deferred->promise();
104
    }
105
}
106