Completed
Push — master ( 6387a2...3f53e4 )
by Sercan
02:30
created

Client::createRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 5
1
<?php
2
3
namespace CryptoMarkets\Common;
4
5
use Http\Client\HttpClient;
6
use Http\Message\RequestFactory;
7
use Http\Discovery\HttpClientDiscovery;
8
use Http\Discovery\MessageFactoryDiscovery;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\StreamInterface;
12
use Psr\Http\Message\UriInterface;
13
14
class Client implements RequestFactory
15
{
16
    /**
17
     * The implemented HTTP client instance.
18
     *
19
     * @var \Http\Client\HttpClient
20
     */
21
    private $httpClient;
22
23
    /**
24
     * @var RequestFactory
25
     */
26
    private $requestFactory;
27
28
    /**
29
     * Create a new Client instance.
30
     *
31
     * @param  \Http\Client\HttpClient|null  $httpClient
32
     * @param  \Http\Message\RequestFactory|null  $requestFactory
33
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
34
     */
35
    public function __construct(HttpClient $httpClient = null, RequestFactory $requestFactory = null)
36
    {
37
        $this->httpClient = $httpClient ?: HttpClientDiscovery::find();
38
        $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
39
    }
40
41
    /**
42
     * Creates a new PSR-7 request.
43
     *
44
     * @param  string  $method
45
     * @param  mixed  $uri
46
     * @param  array  $headers
47
     * @param  mixed  $body
48
     * @param  string  $protocol
49
     * @return \Psr\Http\Message\RequestInterface
50
     */
51
    public function createRequest($method, $uri, array $headers = [], $body = null, $protocol = '1.1')
52
    {
53
        if (is_array($body)) {
54
            $body = http_build_query($body, '', '&');
55
        }
56
57
        return $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocol);
58
    }
59
60
    /**
61
     * Dynamically call the default driver instance.
62
     *
63
     * @param  string  $method
64
     * @param  array  $parameters
65
     * @return mixed
66
     */
67
    public function __call($method, $parameters)
68
    {
69
        return $this->httpClient->{$method}(...$parameters);
70
    }
71
}
72