Completed
Pull Request — master (#4)
by Pavel
10:02
created

TraceableResponseCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.59%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 59
ccs 25
cts 27
cp 0.9259
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getResponse() 0 8 1
B getIterator() 0 21 5
1
<?php
2
3
namespace Bankiru\Api\Client;
4
5
use ScayTrase\Api\Rpc\ResponseCollectionInterface;
6
use ScayTrase\Api\Rpc\RpcRequestInterface;
7
use Symfony\Component\Stopwatch\Stopwatch;
8
9
class TraceableResponseCollection implements \IteratorAggregate, ResponseCollectionInterface
10
{
11
    /** @var  ResponseCollectionInterface */
12
    private $collection;
13
    /** @var  Stopwatch */
14
    private $stopwatch;
15
    /** @var  string */
16
    private $client;
17
18
    /**
19
     * TraceableResponseCollection constructor.
20
     *
21
     * @param ResponseCollectionInterface $collection
22
     * @param Stopwatch                   $stopwatch
23
     * @param string                      $client
24
     */
25 1
    public function __construct(
26
        ResponseCollectionInterface $collection,
27
        Stopwatch $stopwatch,
28
        $client)
29
    {
30 1
        $this->collection = $collection;
31 1
        $this->stopwatch  = $stopwatch;
32 1
        $this->client     = $client;
33 1
    }
34
35
    /** {@inheritdoc} */
36 1
    public function getResponse(RpcRequestInterface $request)
37
    {
38 1
        $this->stopwatch->start($this->client, 'rpc_response');
39 1
        $response = $this->collection->getResponse($request);
40 1
        $this->stopwatch->stop($this->client);
41
42 1
        return $response;
43
    }
44
45
    /** {@inheritdoc} */
46 1
    public function getIterator()
47
    {
48
        /** @var \Iterator $iterator */
49 1
        $iterator = null;
50
        do {
51 1
            $this->stopwatch->start($this->client, 'rpc_response');
52 1
            if (null === $iterator) {
53 1
                if ($this->collection instanceof \IteratorAggregate) {
54 1
                    $iterator = $this->collection->getIterator();
55 1
                } elseif ($this->collection instanceof \Iterator) {
56
                    $iterator = $this->collection;
57
                }
58 1
                $iterator->rewind();
59 1
            }
60 1
            $value = $iterator->current();
61 1
            $this->stopwatch->start($this->client, 'rpc_response');
62 1
            $iterator->next();
63
64 1
            yield $value;
65 1
        } while ($iterator->valid());
66 1
    }
67
}
68