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

TraceableResponseCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1
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