RequestFactory::parseOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PCextreme\Cloudstack;
6
7
use GuzzleHttp\Psr7\Request;
8
use Psr\Http\Message\StreamInterface;
9
10
/**
11
 * Used to produce PSR-7 Request instances.
12
 *
13
 * @link https://github.com/guzzle/guzzle/pull/1101
14
 */
15
class RequestFactory
16
{
17
    /**
18
     * Creates a PSR-7 Request instance.
19
     *
20
     * @param  null|string                     $method  HTTP method for the request.
21
     * @param  null|string                     $uri     URI for the request.
22
     * @param  array                           $headers Headers for the message.
23
     * @param  string|resource|StreamInterface $body    Message body.
24
     * @param  string                          $version HTTP protocol version.
25
     * @return Request
26
     */
27
    public function getRequest(
28
        $method,
29
        $uri,
30
        array $headers = [],
31
        $body = null,
32
        string $version = '1.1'
33
    ) : Request {
34
        return new Request($method, $uri, $headers, $body, $version);
35
    }
36
37
    /**
38
     * Parses simplified options.
39
     *
40
     * @param  array $options
41
     * @return array
42
     */
43
    protected function parseOptions(array $options) : array
44
    {
45
        // Should match default values for getRequest
46
        $defaults = [
47
            'headers' => [],
48
            'body'    => null,
49
            'version' => '1.1',
50
        ];
51
52
        return array_merge($defaults, $options);
53
    }
54
55
    /**
56
     * Creates a request using a simplified array of options.
57
     *
58
     * @param  null|string $method
59
     * @param  null|string $uri
60
     * @param  array       $options
61
     * @return Request
62
     */
63
    public function getRequestWithOptions($method, $uri, array $options = []) : Request
64
    {
65
        $options = $this->parseOptions($options);
66
67
        return $this->getRequest(
68
            $method,
69
            $uri,
70
            $options['headers'],
71
            $options['body'],
72
            $options['version']
73
        );
74
    }
75
}
76