Completed
Pull Request — develop (#15)
by Adam
03:45
created

RequestBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
A create() 0 10 2
A createMultipartStreamRequest() 0 17 2
1
<?php
2
3
namespace IBM\Watson\Common;
4
5
use Http\Discovery\MessageFactoryDiscovery;
6
use Http\Message\MultipartStream\MultipartStreamBuilder;
7
use Http\Message\RequestFactory;
8
use Psr\Http\Message\StreamInterface;
9
use Psr\Http\Message\UriInterface;
10
11
final class RequestBuilder
12
{
13
    /**
14
     * @var \Http\Message\MessageFactory
15
     */
16
    private $requestFactory;
17
18
    private $multipartStreamBuilder;
19
20
    /**
21
     * RequestBuilder constructor.
22
     *
23
     * @param \Http\Message\MultipartStream\MultipartStreamBuilder|null $multipartStreamBuilder
24
     * @param \Http\Message\RequestFactory|\Http\Message\MessageFactory|null $requestFactory
25
     *
26
     * @throws \Http\Discovery\Exception\NotFoundException
27
     */
28
    public function __construct(
29
        RequestFactory $requestFactory = null,
30
        MultipartStreamBuilder $multipartStreamBuilder = null
31
    ) {
32
        $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
0 ignored issues
show
Documentation Bug introduced by
It seems like $requestFactory ?: \Http...actoryDiscovery::find() can also be of type object<Http\Message\RequestFactory>. However, the property $requestFactory is declared as type object<Http\Message\MessageFactory>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
33
        $this->multipartStreamBuilder = $multipartStreamBuilder ?: new MultipartStreamBuilder;
34
    }
35
36
    /**
37
     * Create a new PSR-7 request
38
     *
39
     * @param string                                     $method
40
     * @param string|UriInterface                        $uri
41
     * @param array|null                                 $headers
42
     * @param array|resource|string|StreamInterface|null $body
43
     *
44
     * @return \Psr\Http\Message\RequestInterface
45
     */
46
    public function create($method, $uri, array $headers = [], $body = null)
47
    {
48
        if (!is_array($body)) {
49
            $request = $this->requestFactory->createRequest($method, $uri, $headers, $body);
50
        } else {
51
            $request = $this->createMultipartStreamRequest($method, $uri, $headers, $body);
52
        }
53
54
        return $request;
55
    }
56
57
    protected function createMultipartStreamRequest($method, $uri, array $headers = [], array $body = [])
58
    {
59
        foreach ($body as $item) {
60
            $name = $item['name'];
61
            $content = $item['content'];
62
            unset($item['name'], $item['content']);
63
64
            $this->multipartStreamBuilder->addResource($name, $content, $item);
65
        }
66
67
        $multipartStream = $this->multipartStreamBuilder->build();
68
        $boundary = $this->multipartStreamBuilder->getBoundary();
69
70
        $headers['Content-Type'] = 'multipart/form-data; boundary='.$boundary;
71
72
        return $this->requestFactory->createRequest($method, $uri, $headers, $multipartStream);
73
    }
74
}
75