Completed
Pull Request — master (#65)
by Timothée
01:57
created

RestApiBrowser::addRequestHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Rezzza\RestApiBehatExtension\Rest;
4
5
use Http\Client\HttpClient;
6
use Http\Discovery\HttpClientDiscovery;
7
use Http\Discovery\MessageFactoryDiscovery;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Tolerance\Operation\Callback;
11
use Tolerance\Operation\Runner\RetryOperationRunner;
12
use Tolerance\Operation\Runner\CallbackOperationRunner;
13
use Tolerance\Waiter\SleepWaiter;
14
use Rezzza\RestApiBehatExtension\Tolerance\ExecutionTimeLimited;
15
16
class RestApiBrowser
17
{
18
    /** @var HttpClient */
19
    private $httpClient;
20
21
    /** @var RequestInterface */
22
    private $request;
23
24
    /** @var ResponseInterface */
25
    private $response;
26
27
    /** @var ResponseStorage */
28
    private $responseStorage;
29
30
    /** @var string */
31
    private $host;
32
33
    /** @var MessageFactoryDiscovery */
34
    private $messageFactory;
35
36
    /**
37
     * @param string $host
38
     */
39
    public function __construct($host, HttpClient $httpClient = null)
40
    {
41
        $this->host = $host;
42
        $this->httpClient = $httpClient ?: HttpClientDiscovery::find();
43
        $this->messageFactory = MessageFactoryDiscovery::find();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Http\Discovery\MessageFactoryDiscovery::find() of type object<Http\Message\MessageFactory> is incompatible with the declared type object<Http\Discovery\MessageFactoryDiscovery> of property $messageFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
    }
45
46
    /**
47
     * Allow to override the httpClient to use yours with specific middleware for example
48
     */
49
    public function useHttpClient(HttpClient $httpClient)
50
    {
51
        $this->httpClient = $httpClient;
52
    }
53
54
    /**
55
     * @param ResponseStorage $responseStorage
56
     */
57
    public function enableResponseStorage(ResponseStorage $responseStorage)
58
    {
59
        $this->responseStorage = $responseStorage;
60
    }
61
62
    /**
63
     * @return ResponseInterface
64
     */
65
    public function getResponse()
66
    {
67
        return $this->response;
68
    }
69
70
    /**
71
     * @param ResponseInterface $response
72
     */
73
    public function setResponse(ResponseInterface $response)
74
    {
75
        $this->response = $response;
76
    }
77
78
    public function getRequest()
79
    {
80
        return $this->request;
81
    }
82
83
    /**
84
     * @param string $method
85
     * @param string $uri
86
     * @param string|array $body
87
     * @param array $headers
88
     */
89
    public function sendRequest($method, $uri, $body = null, array $headers = [])
90
    {
91
        if (false === $this->hasHost($uri)) {
92
            $uri = rtrim($this->host, '/').'/'.ltrim($uri, '/');
93
        }
94
95
        $this->request = $this->messageFactory->createRequest($method, $uri, $headers, $body);
0 ignored issues
show
Bug introduced by
The method createRequest() does not seem to exist on object<Http\Discovery\MessageFactoryDiscovery>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
        $this->response = $this->httpClient->sendRequest($this->request);
97
98
        if (null !== $this->responseStorage) {
99
            $this->responseStorage->writeRawContent((string) $this->response->getBody());
100
        }
101
    }
102
103
    public function sendRequestUntil($method, $uri, $body, array $headers, callable $assertion, $maxExecutionTime = 10)
104
    {
105
        $runner = new RetryOperationRunner(
106
            new CallbackOperationRunner(),
107
            new ExecutionTimeLimited(new SleepWaiter(), $maxExecutionTime)
108
        );
109
        $restApiBrowser = $this;
110
        $runner->run(new Callback(function () use ($restApiBrowser, $method, $uri, $body, $assertion, $headers) {
111
            $restApiBrowser->sendRequest($method, $uri, $body, $headers);
112
113
            return $assertion();
114
        }));
115
    }
116
117
    /**
118
     * @param string $uri
119
     *
120
     * @return bool
121
     */
122
    private function hasHost($uri)
123
    {
124
        return strpos($uri, '://') !== false;
125
    }
126
}
127