Completed
Push — master ( 0ef30a...eb3939 )
by Kevin
03:35
created

RequestFactory::getRequestWithOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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