Completed
Push — master ( 4a903b...b725ab )
by Tobias
03:23 queued 53s
created

RequestBuilder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 102
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 24 3
A getRequestFactory() 0 8 2
A setRequestFactory() 0 6 1
A getMultipartStreamBuilder() 0 8 2
A setMultipartStreamBuilder() 0 6 1
1
<?php
2
3
/*
4
 * Copyright (C) 2013 Mailgun
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 Mailgun\HttpClient;
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
class RequestBuilder
21
{
22
    /**
23
     * @var RequestFactory
24
     */
25
    private $requestFactory;
26
27
    /**
28
     * @var MultipartStreamBuilder
29
     */
30
    private $multipartStreamBuilder;
31
32
    /**
33
     * Creates a new PSR-7 request.
34
     *
35
     * @param string            $method
36
     * @param string            $uri
37
     * @param array             $headers
38
     * @param array|string|null $body    Request body. If body is an array we will send a as multipart stream request.
39
     *                                   If array, each array *item* MUST look like:
40
     *                                   array (
41
     *                                   'content' => string|resource|StreamInterface,
42
     *                                   'name'    => string,
43
     *                                   'filename'=> string (optional)
44
     *                                   'headers' => array (optinal) ['header-name' => 'header-value']
45
     *                                   )
46
     *
47
     * @return RequestInterface
48
     */
49 2
    public function create(string $method, string $uri, array $headers = [], $body = null)
50
    {
51 2
        if (!is_array($body)) {
52 1
            return $this->getRequestFactory()->createRequest($method, $uri, $headers, $body);
53
        }
54
55 1
        $builder = $this->getMultipartStreamBuilder();
56 1
        foreach ($body as $item) {
57 1
            $name = $item['name'];
58 1
            $content = $item['content'];
59 1
            unset($item['name']);
60 1
            unset($item['content']);
61
62 1
            $builder->addResource($name, $content, $item);
63
        }
64
65 1
        $multipartStream = $builder->build();
66 1
        $boundary = $builder->getBoundary();
67 1
        $builder->reset();
68
69 1
        $headers['Content-Type'] = 'multipart/form-data; boundary="'.$boundary.'"';
70
71 1
        return $this->getRequestFactory()->createRequest($method, $uri, $headers, $multipartStream);
72
    }
73
74
    /**
75
     * @return RequestFactory
76
     */
77 2
    private function getRequestFactory()
78
    {
79 2
        if (null === $this->requestFactory) {
80
            $this->requestFactory = MessageFactoryDiscovery::find();
81
        }
82
83 2
        return $this->requestFactory;
84
    }
85
86
    /**
87
     * @param RequestFactory $requestFactory
88
     *
89
     * @return RequestBuilder
90
     */
91 2
    public function setRequestFactory($requestFactory)
92
    {
93 2
        $this->requestFactory = $requestFactory;
94
95 2
        return $this;
96
    }
97
98
    /**
99
     * @return MultipartStreamBuilder
100
     */
101 1
    private function getMultipartStreamBuilder()
102
    {
103 1
        if (null === $this->multipartStreamBuilder) {
104
            $this->multipartStreamBuilder = new MultipartStreamBuilder();
105
        }
106
107 1
        return $this->multipartStreamBuilder;
108
    }
109
110
    /**
111
     * @param MultipartStreamBuilder $multipartStreamBuilder
112
     *
113
     * @return RequestBuilder
114
     */
115 1
    public function setMultipartStreamBuilder($multipartStreamBuilder)
116
    {
117 1
        $this->multipartStreamBuilder = $multipartStreamBuilder;
118
119 1
        return $this;
120
    }
121
}
122