Completed
Push — master ( 955d05...19c28e )
by David
03:37
created

src/BatchClient.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Http\Client\Common;
4
5
use Http\Client\Exception;
6
use Http\Client\HttpClient;
7
use Http\Client\Common\Exception\BatchException;
8
use Psr\Http\Client\ClientInterface;
9
use Psr\Http\Message\RequestInterface;
10
11
/**
12
 * BatchClient allow to sends multiple request and retrieve a Batch Result.
13
 *
14
 * This implementation simply loops over the requests and uses sendRequest with each of them.
15
 *
16
 * @author Joel Wurtz <[email protected]>
17
 */
18
class BatchClient implements HttpClient
19
{
20
    /**
21
     * @var HttpClient|ClientInterface
22
     */
23
    private $client;
24
25
    /**
26
     * @param HttpClient|ClientInterface  $client
27
     */
28 2
    public function __construct($client)
29
    {
30 2
        if (!($client instanceof HttpClient) && !($client instanceof ClientInterface)) {
0 ignored issues
show
The class Psr\Http\Client\ClientInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
31
            throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
32
        }
33
34 2
        $this->client = $client;
35 2
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 2
    public function sendRequest(RequestInterface $request)
41
    {
42 2
        return $this->client->sendRequest($request);
43
    }
44
45
    /**
46
     * Send several requests.
47
     *
48
     * You may not assume that the requests are executed in a particular order. If the order matters
49
     * for your application, use sendRequest sequentially.
50
     *
51
     * @param RequestInterface[] The requests to send
52
     *
53
     * @return BatchResult Containing one result per request
54
     *
55
     * @throws BatchException If one or more requests fails. The exception gives access to the
56
     *                        BatchResult with a map of request to result for success, request to
57
     *                        exception for failures
58
     */
59 2
    public function sendRequests(array $requests)
60
    {
61 2
        $batchResult = new BatchResult();
62
63 2
        foreach ($requests as $request) {
64
            try {
65 2
                $response = $this->sendRequest($request);
66 2
                $batchResult = $batchResult->addResponse($request, $response);
67 1
            } catch (Exception $e) {
68 2
                $batchResult = $batchResult->addException($request, $e);
69
            }
70
        }
71
72 2
        if ($batchResult->hasExceptions()) {
73 1
            throw new BatchException($batchResult);
74
        }
75
76 1
        return $batchResult;
77
    }
78
}
79