TupleCollection::getResponse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
3
namespace ScayTrase\Api\Rpc\Test;
4
5
use ScayTrase\Api\Rpc\ResponseCollectionInterface;
6
use ScayTrase\Api\Rpc\RpcRequestInterface;
7
8
/**
9
 * @internal
10
 */
11
final class TupleCollection implements \IteratorAggregate, ResponseCollectionInterface
12
{
13
    private $tuples = [];
14
15
    /**
16
     * TypleCollection constructor.
17
     *
18
     * @param array $tuples
19
     */
20 11
    public function __construct(array $tuples)
21
    {
22 11
        $this->tuples = $tuples;
23 11
    }
24
25
    /** {@inheritdoc} */
26 7
    public function getResponse(RpcRequestInterface $request)
27
    {
28 7
        foreach ($this->tuples as $tuple) {
29 7
            if ($tuple['request'] === $request) {
30 6
                return $tuple['response'];
31
            }
32 3
        }
33
34 1
        throw new \OutOfBoundsException('Request is not valid');
35
    }
36
37
    /** {@inheritdoc} */
38 5
    public function getIterator()
39
    {
40 5
        return new \ArrayIterator(array_map([$this, 'getResponseFromTuple'], $this->tuples));
41
    }
42
43 5
    private function getResponseFromTuple(array $tuple)
44
    {
45 5
        return $tuple['response'];
46
    }
47
}
48