Completed
Push — master ( f743de...f8a04a )
by Daniel
03:18
created

RpcMessager::parseAMQPMessage()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Rpc;
4
5
use Cmobi\RabbitmqBundle\Rpc\Exception\JsonRpcGenericErrorException;
6
use Cmobi\RabbitmqBundle\Rpc\Exception\JsonRpcParserErrorException;
7
use Cmobi\RabbitmqBundle\Rpc\Request\JsonRpcRequestFactory;
8
use Cmobi\RabbitmqBundle\Rpc\Request\RpcRequestCollectionInterface;
9
use Cmobi\RabbitmqBundle\Rpc\Request\RpcRequestInterface;
10
use Cmobi\RabbitmqBundle\Rpc\Response\JsonRpcResponse;
11
use Cmobi\RabbitmqBundle\Rpc\Response\RpcResponseCollectionInterface;
12
use Cmobi\RabbitmqBundle\Rpc\Response\RpcResponseInterface;
13
use PhpAmqpLib\Message\AMQPMessage;
14
15
class RpcMessager
16
{
17
    private $requestCollection;
18
    private $responseCollection;
19
    private $requestFactory;
20
21
    public function __construct(RpcRequestCollectionInterface $requests, RpcResponseCollectionInterface $responses, JsonRpcRequestFactory $factory)
22
    {
23
        $this->requestCollection = $requests;
24
        $this->responseCollection = $responses;
25
        $this->requestFactory = $factory;
26
    }
27
28
    public function parseAMQPMessage(AMQPMessage $message)
29
    {
30
        $body = $message->body;
31
        try {
32
            $requests = json_decode($body, true);
33
        } catch (\Exception $e) {
34
            throw new JsonRpcParserErrorException();
35
        }
36
37
        if (!isset($requests[0])) {
38
            $this->buildRequest($requests);
39
        } else {
40
41
            foreach ($requests as $request) {
42
                $this->buildRequest($request);
43
            }
44
        }
45
    }
46
47
    public function addRequest($id, RpcRequestInterface $request)
48
    {
49
        $this->requestCollection->add($id, $request);
50
    }
51
52
    public function addResponse($id, RpcResponseInterface $response)
53
    {
54
        $this->responseCollection->add($id, $response);
55
    }
56
57
    private function buildRequest($request)
58
    {
59
        try {
60
            $request = $this->requestFactory->factory($request);
61
            $this->requestCollection->add(uniqid(), $request);
62
        } catch (JsonRpcGenericErrorException $e) {
63
            $response = new JsonRpcResponse([], $e);
64
            $id = uniqid();
65
66
            if (isset($requests['id'])) {
0 ignored issues
show
Bug introduced by
The variable $requests does not exist. Did you mean $request?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
67
                $id = $requests['id'];
68
            }
69
            $this->responseCollection->add($id, $response);
70
        }
71
    }
72
73
    /**
74
     * @return RpcRequestCollectionInterface
75
     */
76
    public function getRequestCollection()
77
    {
78
        return $this->requestCollection;
79
    }
80
81
    /**
82
     * @return RpcResponseCollectionInterface
83
     */
84
    public function getResponseCollection()
85
    {
86
        return $this->responseCollection;
87
    }
88
}