Completed
Push — master ( 5d00af...04d482 )
by David
03:48
created

Client::doSendRequest()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 9.2408
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
namespace Http\Mock;
4
5
use Http\Client\Common\HttpAsyncClientEmulator;
6
use Http\Client\Common\VersionBridgeClient;
7
use Http\Client\Exception;
8
use Http\Client\HttpAsyncClient;
9
use Http\Client\HttpClient;
10
use Http\Discovery\MessageFactoryDiscovery;
11
use Http\Message\ResponseFactory;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
15
/**
16
 * An implementation of the HTTP client that is useful for automated tests.
17
 *
18
 * This mock does not send requests but stores them for later retrieval.
19
 * You can configure the mock with responses to return and/or exceptions to throw.
20
 *
21
 * @author David de Boer <[email protected]>
22
 */
23
class Client implements HttpClient, HttpAsyncClient
24
{
25
    use HttpAsyncClientEmulator;
26
    use VersionBridgeClient;
27
28
    /**
29
     * @var ResponseFactory
30
     */
31
    private $responseFactory;
32
33
    /**
34
     * @var RequestInterface[]
35
     */
36
    private $requests = [];
37
38
    /**
39
     * @var ResponseInterface[]
40
     */
41
    private $responses = [];
42
43
    /**
44
     * @var ResponseInterface|null
45
     */
46
    private $defaultResponse;
47
48
    /**
49
     * @var Exception[]
50
     */
51
    private $exceptions = [];
52
53
    /**
54
     * @var Exception|null
55
     */
56
    private $defaultException;
57
58 10
    public function __construct(ResponseFactory $responseFactory = null)
59
    {
60 10
        $this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
61 10
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 6
    public function doSendRequest(RequestInterface $request)
67
    {
68 6
        $this->requests[] = $request;
69
70 6
        if (count($this->exceptions) > 0) {
71 1
            throw array_shift($this->exceptions);
72
        }
73
74 5
        if (count($this->responses) > 0) {
75 2
            return array_shift($this->responses);
76
        }
77
78 3
        if ($this->defaultException) {
79 1
            throw $this->defaultException;
80
        }
81
82 2
        if ($this->defaultResponse) {
83 1
            return $this->defaultResponse;
84
        }
85
86
        // Return success response by default
87 1
        return $this->responseFactory->createResponse();
88
    }
89
90
    /**
91
     * Adds an exception that will be thrown.
92
     */
93 1
    public function addException(\Exception $exception)
94
    {
95 1
        $this->exceptions[] = $exception;
96 1
    }
97
98
    /**
99
     * Sets the default exception to throw when the list of added exceptions and responses is exhausted.
100
     *
101
     * If both a default exception and a default response are set, the exception will be thrown.
102
     */
103 1
    public function setDefaultException(\Exception $defaultException = null)
104
    {
105 1
        $this->defaultException = $defaultException;
0 ignored issues
show
Documentation Bug introduced by
It seems like $defaultException can also be of type object<Exception>. However, the property $defaultException is declared as type object<Http\Client\Exception>|null. 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...
106 1
    }
107
108
    /**
109
     * Adds a response that will be returned in first in first out order.
110
     */
111 2
    public function addResponse(ResponseInterface $response)
112
    {
113 2
        $this->responses[] = $response;
114 2
    }
115
116
    /**
117
     * Sets the default response to be returned when the list of added exceptions and responses is exhausted.
118
     */
119 1
    public function setDefaultResponse(ResponseInterface $defaultResponse = null)
120
    {
121 1
        $this->defaultResponse = $defaultResponse;
122 1
    }
123
124
    /**
125
     * Returns requests that were sent.
126
     *
127
     * @return RequestInterface[]
128
     */
129
    public function getRequests()
130
    {
131
        return $this->requests;
132
    }
133
134 2
    public function getLastRequest()
135
    {
136 2
        return end($this->requests);
137
    }
138
}
139