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

BatchClient   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 37
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A sendRequest() 0 4 1
A sendRequests() 0 19 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