RequestBuilder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 38.89%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 61
ccs 7
cts 18
cp 0.3889
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
A create() 0 19 3
1
<?php
2
3
declare(strict_types=1);
4
/*
5
 * This software may be modified and distributed under the terms
6
 * of the MIT license. See the LICENSE file for details.
7
 */
8
9
namespace Billogram;
10
11
use Http\Discovery\MessageFactoryDiscovery;
12
use Http\Message\MultipartStream\MultipartStreamBuilder;
13
use Http\Message\RequestFactory;
14
use Psr\Http\Message\RequestInterface;
15
16
/**
17
 * @author Tobias Nyholm <[email protected]>
18
 *
19
 * @internal This class should not be used outside of the API Client, it is not part of the BC promise.
20
 */
21
final class RequestBuilder
22
{
23
    /**
24
     * @var RequestFactory
25
     */
26
    private $requestFactory;
27
28
    /**
29
     * @var MultipartStreamBuilder
30
     */
31
    private $multipartStreamBuilder;
32
33
    /**
34
     * @param RequestFactory         $requestFactory
35
     * @param MultipartStreamBuilder $multipartStreamBuilder
36
     */
37 19
    public function __construct(
38
        RequestFactory $requestFactory = null,
39
        MultipartStreamBuilder $multipartStreamBuilder = null
40
    ) {
41 19
        $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
42 19
        $this->multipartStreamBuilder = $multipartStreamBuilder ?: new MultipartStreamBuilder();
43 19
    }
44
45
    /**
46
     * Creates a new PSR-7 request.
47
     *
48
     * @param string            $method
49
     * @param string            $uri
50
     * @param array             $headers
51
     * @param array|string|null $body    Request body. If body is an array we will send a as multipart stream request.
52
     *                                   If array, each array *item* MUST look like:
53
     *                                   array (
54
     *                                   'content' => string|resource|StreamInterface,
55
     *                                   'name'    => string,
56
     *                                   'filename'=> string (optional)
57
     *                                   'headers' => array (optinal) ['header-name' => 'header-value']
58
     *                                   )
59
     *
60
     * @return RequestInterface
61
     */
62 19
    public function create(string $method, string $uri, array $headers = [], $body = null): RequestInterface
63
    {
64 19
        if (!is_array($body)) {
65 19
            return $this->requestFactory->createRequest($method, $uri, $headers, $body);
66
        }
67
        foreach ($body as $item) {
68
            $name = $item['name'];
69
            $content = $item['content'];
70
            unset($item['name']);
71
            unset($item['content']);
72
            $this->multipartStreamBuilder->addResource($name, $content, $item);
73
        }
74
        $multipartStream = $this->multipartStreamBuilder->build();
75
        $boundary = $this->multipartStreamBuilder->getBoundary();
76
        $headers['Content-Type'] = 'multipart/form-data; boundary='.$boundary;
77
        $this->multipartStreamBuilder->reset();
78
79
        return $this->requestFactory->createRequest($method, $uri, $headers, $multipartStream);
80
    }
81
}
82