Passed
Pull Request — master (#23)
by Anton
13:40 queued 08:26
created

Client::sendRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Service\AkeneoPim\Dependencies\External\Api\Adapter\HttpClient;
9
10
use GuzzleHttp\Client as GuzzleHttpClient;
11
use GuzzleHttp\ClientInterface;
12
use GuzzleHttp\HandlerStack;
13
use GuzzleHttp\Middleware;
14
use GuzzleHttp\Utils;
15
use Http\Client\HttpClient;
16
use Http\Promise\Promise as HttpPromise;
17
use Psr\Http\Message\RequestInterface;
18
use Psr\Http\Message\ResponseInterface;
19
use function GuzzleHttp\choose_handler;
20
21
class Client implements HttpClient
0 ignored issues
show
Deprecated Code introduced by
The interface Http\Client\HttpClient has been deprecated: since version 2.4, use Psr\Http\Client\ClientInterface instead; see https://www.php-fig.org/psr/psr-18/ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

21
class Client implements /** @scrutinizer ignore-deprecated */ HttpClient

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
22
{
23
    /**
24
     * @var \GuzzleHttp\ClientInterface
25
     */
26
    protected $client;
27
28
    public function __construct()
29
    {
30
        $this->client = $this->buildClient();
31
    }
32
33
    /**
34
     * @param \Psr\Http\Message\RequestInterface $request
35
     *
36
     * @return \Psr\Http\Message\ResponseInterface
37
     */
38
    public function sendRequest(RequestInterface $request): ResponseInterface
39
    {
40
        return $this->sendAsyncRequest($request)->wait();
41
    }
42
43
    /**
44
     * @param \Psr\Http\Message\RequestInterface $request
45
     *
46
     * @return \Http\Promise\Promise
47
     */
48
    protected function sendAsyncRequest(RequestInterface $request): HttpPromise
49
    {
50
        $promise = $this->client->sendAsync($request);
51
52
        return new Promise($promise, $request);
53
    }
54
55
    /**
56
     * @return \GuzzleHttp\ClientInterface
57
     */
58
    protected function buildClient(): ClientInterface
59
    {
60
        $handlerStack = $this->createHandlerStack();
61
        $handlerStack->push(Middleware::prepareBody(), 'prepare_body');
62
63
        return new GuzzleHttpClient(['handler' => $handlerStack]);
64
    }
65
66
    /**
67
     * @return \GuzzleHttp\HandlerStack
68
     */
69
    protected function createHandlerStack(): HandlerStack
70
    {
71
        if (method_exists(Utils::class, 'chooseHandler')) {
72
            return new HandlerStack(Utils::chooseHandler());
73
        }
74
75
        return new HandlerStack(choose_handler());
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\choose_handler() has been deprecated: choose_handler will be removed in guzzlehttp/guzzle:8.0. Use Utils::chooseHandler instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

75
        return new HandlerStack(/** @scrutinizer ignore-deprecated */ choose_handler());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
76
    }
77
}
78