Completed
Pull Request — master (#7)
by Igor
07:53 queued 03:25
created

ProcessingBatchRequester   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 19
c 0
b 0
f 0
dl 0
loc 41
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 20 3
A getMatchingResponseForRequest() 0 17 4
1
<?php
2
/*
3
 * This file is part of JSON RPC Client.
4
 *
5
 * (c) Igor Lazarev <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Strider2038\JsonRpcClient\Service;
12
13
use Strider2038\JsonRpcClient\Exception\NoResponseReceivedException;
14
use Strider2038\JsonRpcClient\Request\RequestObjectInterface;
15
use Strider2038\JsonRpcClient\Response\ResponseObjectInterface;
16
17
/**
18
 * @author Igor Lazarev <[email protected]>
19
 */
20
class ProcessingBatchRequester extends RawBatchRequester
21
{
22 9
    public function send(): array
23
    {
24
        /** @var ResponseObjectInterface[] $responses */
25 9
        $responses = parent::send();
26
27
        /** @var RequestObjectInterface[] $queue */
28 9
        $queue = $this->getQueue();
29
30 9
        $orderedResults = [];
31
32 9
        foreach ($queue as $request) {
33 9
            if (null === $request->getId()) {
34 7
                $orderedResults[] = null;
35
            } else {
36 8
                $matchingResponse = $this->getMatchingResponseForRequest($responses, $request);
37 9
                $orderedResults[] = $matchingResponse->getResult();
38
            }
39
        }
40
41 8
        return $orderedResults;
42
    }
43
44 8
    private function getMatchingResponseForRequest(array $responses, RequestObjectInterface $request): ResponseObjectInterface
45
    {
46 8
        $matchingResponse = null;
47
48 8
        foreach ($responses as $response) {
49 8
            if ($request->getId() === $response->getId()) {
50 8
                $matchingResponse = $response;
51
52 8
                break;
53
            }
54
        }
55
56 8
        if (null === $matchingResponse) {
57 1
            throw new NoResponseReceivedException($request);
58
        }
59
60 8
        return $matchingResponse;
61
    }
62
}
63