GuzzleHttpClient::sendRequestBatch()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
namespace EventStore\Http;
3
4
use Doctrine\Common\Cache\ApcCache;
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Cache\FilesystemCache;
7
use Exception as PhpException;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\ClientInterface;
10
use GuzzleHttp\Exception\ClientException as GuzzleClientException;
11
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
12
use GuzzleHttp\Handler\CurlMultiHandler;
13
use GuzzleHttp\HandlerStack;
14
use GuzzleHttp\Pool;
15
use Kevinrob\GuzzleCache\CacheMiddleware;
16
use Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage;
17
use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy;
18
use Psr\Http\Message\RequestInterface;
19
20
final class GuzzleHttpClient implements HttpClientInterface
21
{
22 26
    public function __construct(ClientInterface $client = null)
23
    {
24 26
        $this->client = $client ?: new Client([
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25 26
            'handler' => new CurlMultiHandler(),
26
        ]);
27 26
    }
28
29
    public static function withFilesystemCache($path)
30
    {
31
        return self::withDoctrineCache(
32
            new FilesystemCache($path)
33
        );
34
    }
35
36
    public static function withApcCache()
37
    {
38
        return self::withDoctrineCache(
39
            new ApcCache()
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ApcCache has been deprecated with message: since version 1.6, use ApcuCache instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
40
        );
41
    }
42
43
    public static function withDoctrineCache(Cache $doctrineCache)
44
    {
45
        $stack = new HandlerStack(new CurlMultiHandler());
46
47
        $stack->push(
48
            new CacheMiddleware(
49
                new PublicCacheStrategy(
50
                    new DoctrineCacheStorage(
51
                        $doctrineCache
52
                    )
53
                )
54
            ),
55
          'cache'
56
        );
57
58
        $client = new Client([
59
            'handler' => $stack
60
        ]);
61
62
        return new self($client);
63
    }
64
65 7
    public function sendRequestBatch(array $requests)
66
    {
67 7
        $responses = Pool::batch(
68 7
            $this->client,
69
            $requests
70
        );
71
72 7
        foreach ($responses as $response) {
73 7
            if ($response instanceof PhpException) {
74 7
                throw $response;
75
            }
76
        }
77
78 7
        return $responses;
79
    }
80
81 26
    public function send(RequestInterface $request)
82
    {
83
        try {
84 26
            return $this->client->send($request);
85 1
        } catch (GuzzleClientException $e) {
86
            throw new Exception\ClientException($e->getMessage(), $e->getCode(), $e);
87 1
        } catch (GuzzleRequestException $e) {
88 1
            throw new Exception\RequestException($e->getMessage(), $e->getCode(), $e);
89
        }
90
    }
91
}
92