Completed
Push — master ( f9f44c...122ff5 )
by Tobias
02:39
created

BatchClient::sendRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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