RequestBuilder::create()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 10.0876

Importance

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