Completed
Push — master ( 9c21b6...43c791 )
by David
04:48
created

BatchClient::sendRequests()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.6333
c 0
b 0
f 0
cc 4
nc 8
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\Client\Common;
6
7
use Http\Client\Exception;
8
use Http\Client\Common\Exception\BatchException;
9
use Psr\Http\Client\ClientInterface;
10
use Psr\Http\Message\RequestInterface;
11
use Psr\Http\Message\ResponseInterface;
12
13
final class BatchClient implements BatchClientInterface
14
{
15
    /**
16
     * @var ClientInterface
17
     */
18
    private $client;
19
20 2
    public function __construct(ClientInterface $client)
21
    {
22 2
        $this->client = $client;
23 2
    }
24
25 2
    public function sendRequest(RequestInterface $request): ResponseInterface
26
    {
27 2
        return $this->client->sendRequest($request);
28
    }
29
30 2
    public function sendRequests(array $requests): BatchResult
31
    {
32 2
        $batchResult = new BatchResult();
33
34 2
        foreach ($requests as $request) {
35
            try {
36 2
                $response = $this->sendRequest($request);
37 2
                $batchResult = $batchResult->addResponse($request, $response);
38 1
            } catch (Exception $e) {
39 2
                $batchResult = $batchResult->addException($request, $e);
40
            }
41
        }
42
43 2
        if ($batchResult->hasExceptions()) {
44 1
            throw new BatchException($batchResult);
45
        }
46
47 1
        return $batchResult;
48
    }
49
}
50