Completed
Push — master ( f61680...98184f )
by Pavel
02:28
created

TupleCollection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 37
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getIterator() 0 4 1
A getResponseFromTuple() 0 4 1
A getResponse() 0 10 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 4
    public function __construct(array $tuples)
21
    {
22 4
        $this->tuples = $tuples;
23 4
    }
24
25
    /** {@inheritdoc} */
26 4
    public function getResponse(RpcRequestInterface $request)
27
    {
28 4
        foreach ($this->tuples as $tuple) {
29 4
            if ($tuple['request'] === $request) {
30 3
                return $tuple['response'];
31
            }
32 2
        }
33
34 1
        throw new \OutOfBoundsException('Request is not valid');
35
    }
36
37
    /** {@inheritdoc} */
38 1
    public function getIterator()
39
    {
40 1
        return new \ArrayIterator(array_map([$this, 'getResponseFromTuple'], $this->tuples));
41
    }
42
43 1
    private function getResponseFromTuple(array $tuple)
44
    {
45 1
        return $tuple['response'];
46
    }
47
}
48