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

JsonRpcRequestFactory::factory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Rpc\Request;
4
5
use Cmobi\RabbitmqBundle\Rpc\Exception\JsonRpcInvalidParamsException;
6
use Cmobi\RabbitmqBundle\Rpc\Exception\JsonRpcUnsupportedProtocolException;
7
8
class JsonRpcRequestFactory
9
{
10
    private $requiredFields = ['id', 'jsonrpc', 'method'];
11
12
    public function factory(array $request)
13
    {
14
        $this->validateRequiredFields($request);
15
16
        $request = new JsonRpcRequest($request['params']);
17
        $request->setId($request['id']);
18
        $request->setMethod($request['method']);
19
20
        return $request;
21
    }
22
23
    public function validateRequiredFields(array $parameters)
24
    {
25
        $keys = array_intersect_key($parameters, array_flip($this->requiredFields));
26
27
        if (count($this->requiredFields) !=  count($keys)) {
28
            throw new JsonRpcInvalidParamsException();
29
        }
30
31
        if ($parameters['jsonrpc'] !== JsonRpcRequest::VERSION) {
32
            throw new JsonRpcUnsupportedProtocolException();
33
        }
34
    }
35
}