Completed
Push — master ( 398989...6954b4 )
by Tobias
29:12 queued 04:09
created

RequestBuilder::createRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 4
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2013 Mailgun
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license. See the LICENSE file for details.
10
 */
11
12
namespace Mailgun\HttpClient;
13
14
use Http\Discovery\Psr17FactoryDiscovery;
15
use Http\Message\MultipartStream\MultipartStreamBuilder;
16
use Psr\Http\Message\RequestFactoryInterface;
17
use Psr\Http\Message\RequestInterface;
18
use Psr\Http\Message\StreamFactoryInterface;
19
use Psr\Http\Message\StreamInterface;
20
21
/**
22
 * @author Tobias Nyholm <[email protected]>
23
 */
24
class RequestBuilder
25
{
26
    /**
27
     * @var RequestFactoryInterface|null
28
     */
29
    private $requestFactory;
30
31
    /**
32
     * @var StreamFactoryInterface|null
33
     */
34
    private $streamFactory;
35
36
    /**
37
     * @var MultipartStreamBuilder
38
     */
39
    private $multipartStreamBuilder;
40
41
    /**
42
     * Creates a new PSR-7 request.
43
     *
44
     * @param array|string|null $body Request body. If body is an array we will send a as multipart stream request.
45
     *                                If array, each array *item* MUST look like:
46
     *                                array (
47
     *                                'content' => string|resource|StreamInterface,
48 2
     *                                'name'    => string,
49
     *                                'filename'=> string (optional)
50 2
     *                                'headers' => array (optinal) ['header-name' => 'header-value']
51 1
     *                                )
52
     */
53
    public function create(string $method, string $uri, array $headers = [], $body = null): RequestInterface
54 1
    {
55 1
        if (!is_array($body)) {
56 1
            $stream = $this->getStreamFactory()->createStream($body);
57 1
58 1
            return $this->createRequest($method, $uri, $headers, $stream);
59 1
        }
60
61 1
        $builder = $this->getMultipartStreamBuilder();
62
        foreach ($body as $item) {
63
            $name = $item['name'];
64 1
            $content = $item['content'];
65 1
            unset($item['name']);
66 1
            unset($item['content']);
67
68 1
            $builder->addResource($name, $content, $item);
69
        }
70 1
71
        $multipartStream = $builder->build();
72
        $boundary = $builder->getBoundary();
73
        $builder->reset();
74
75
        $headers['Content-Type'] = 'multipart/form-data; boundary="'.$boundary.'"';
76 2
77
        return $this->createRequest($method, $uri, $headers, $multipartStream);
78 2
    }
79
80
    private function getRequestFactory(): RequestFactoryInterface
81
    {
82 2
        if (null === $this->requestFactory) {
83
            $this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
84
        }
85
86
        return $this->requestFactory;
87
    }
88
89
    public function setRequestFactory(RequestFactoryInterface $requestFactory): self
90 2
    {
91
        $this->requestFactory = $requestFactory;
92 2
93
        return $this;
94 2
    }
95
96
    private function getStreamFactory(): StreamFactoryInterface
97
    {
98
        if (null === $this->streamFactory) {
99
            $this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
100 1
        }
101
102 1
        return $this->streamFactory;
103
    }
104
105
    public function setStreamFactory(StreamFactoryInterface $streamFactory): self
106 1
    {
107
        $this->streamFactory = $streamFactory;
108
109
        return $this;
110
    }
111
112
    private function getMultipartStreamBuilder(): MultipartStreamBuilder
113
    {
114 1
        if (null === $this->multipartStreamBuilder) {
115
            $this->multipartStreamBuilder = new MultipartStreamBuilder();
116 1
        }
117
118 1
        return $this->multipartStreamBuilder;
119
    }
120
121
    public function setMultipartStreamBuilder(MultipartStreamBuilder $multipartStreamBuilder): self
122
    {
123
        $this->multipartStreamBuilder = $multipartStreamBuilder;
124
125
        return $this;
126
    }
127
128
    private function createRequest(string $method, string $uri, array $headers, StreamInterface $stream)
129
    {
130
        $request = $this->getRequestFactory()->createRequest($method, $uri);
131
        $request = $request->withBody($stream);
132
        foreach ($headers as $name => $value) {
133
            $request = $request->withAddedHeader($name, $value);
134
        }
135
136
        return $request;
137
    }
138
}
139