Completed
Push — master ( 3ba296...5d6a10 )
by Irfaq
10:32
created

GuzzleHttpClient::getConnectTimeOut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Telegram\Bot\HttpClients;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\RequestException;
7
use GuzzleHttp\Promise;
8
use GuzzleHttp\Promise\PromiseInterface;
9
use GuzzleHttp\RequestOptions;
10
use Psr\Http\Message\ResponseInterface;
11
use Telegram\Bot\Exceptions\TelegramSDKException;
12
13
/**
14
 * Class GuzzleHttpClient.
15
 */
16
class GuzzleHttpClient implements HttpClientInterface
17
{
18
    /**
19
     * HTTP client.
20
     *
21
     * @var Client
22
     */
23
    protected $client;
24
25
    /**
26
     * @var PromiseInterface[]
27
     */
28
    private static $promises = [];
29
30
    /**
31
     * Timeout of the request in seconds.
32
     *
33
     * @var int
34
     */
35
    protected $timeOut = 30;
36
37
    /**
38
     * Connection timeout of the request in seconds.
39
     *
40
     * @var int
41
     */
42
    protected $connectTimeOut = 10;
43
44
    /**
45
     * @param Client|null $client
46
     */
47 74
    public function __construct(Client $client = null)
48
    {
49 74
        $this->client = $client ?: new Client();
50 74
    }
51
52
    /**
53
     * Unwrap Promises.
54
     */
55 42
    public function __destruct()
56
    {
57 42
        Promise\unwrap(self::$promises);
58 42
    }
59
60
    /**
61
     * Sets HTTP client.
62
     *
63
     * @param Client $client
64
     *
65
     * @return GuzzleHttpClient
66
     */
67
    public function setClient(Client $client)
68
    {
69
        $this->client = $client;
70
71
        return $this;
72
    }
73
74
    /**
75
     * Gets HTTP client for internal class use.
76
     *
77
     * @return Client
78
     */
79 40
    private function getClient()
80
    {
81 40
        return $this->client;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 40
    public function send(
88
        $url,
89
        $method,
90
        array $headers = [],
91
        array $options = [],
92
        $timeOut = 30,
93
        $isAsyncRequest = false,
94
        $connectTimeOut = 10
95
    ) {
96 40
        $this->timeOut = $timeOut;
97 40
        $this->connectTimeOut = $connectTimeOut;
98
99 40
        $body = isset($options['body']) ? $options['body'] : null;
100 40
        $options = $this->getOptions($headers, $body, $options, $timeOut, $isAsyncRequest, $connectTimeOut);
101
102
        try {
103 40
            $response = $this->getClient()->requestAsync($method, $url, $options);
104
105 40
            if ($isAsyncRequest) {
106
                self::$promises[] = $response;
107
            } else {
108 40
                $response = $response->wait();
109
            }
110 40
        } catch (RequestException $e) {
111
            $response = $e->getResponse();
112
113
            if (!$response instanceof ResponseInterface) {
114
                throw new TelegramSDKException($e->getMessage(), $e->getCode());
115
            }
116
        }
117
118 40
        return $response;
119 2
    }
120
121
    /**
122
     * Prepares and returns request options.
123
     *
124
     * @param array $headers
125
     * @param       $body
126
     * @param       $options
127
     * @param       $timeOut
128
     * @param       $isAsyncRequest
129
     * @param int   $connectTimeOut
130
     *
131
     * @return array
132
     */
133 40
    private function getOptions(array $headers, $body, $options, $timeOut, $isAsyncRequest = false, $connectTimeOut = 10)
134
    {
135
        $default_options = [
136 40
            RequestOptions::HEADERS         => $headers,
137 40
            RequestOptions::BODY            => $body,
138 40
            RequestOptions::TIMEOUT         => $timeOut,
139 40
            RequestOptions::CONNECT_TIMEOUT => $connectTimeOut,
140 40
            RequestOptions::SYNCHRONOUS     => !$isAsyncRequest,
141 40
        ];
142
143 40
        return array_merge($default_options, $options);
144
    }
145
146
    /**
147
     * @return int
148
     */
149 2
    public function getTimeOut()
150
    {
151 2
        return $this->timeOut;
152
    }
153
154
    /**
155
     * @return int
156
     */
157 2
    public function getConnectTimeOut()
158
    {
159 2
        return $this->connectTimeOut;
160
    }
161
}
162