Completed
Push — master ( 339dcc...fa58da )
by Daniel
13:39
created

Handler::handle()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 2
Metric Value
c 6
b 1
f 2
dl 0
loc 33
rs 8.5806
cc 4
eloc 23
nc 4
nop 1
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Rpc;
4
5
use Cmobi\RabbitmqBundle\Rpc\Controller\RpcControllerResolver;
6
use Cmobi\RabbitmqBundle\Rpc\Exception\InvalidBodyAMQPMessageException;
7
use Cmobi\RabbitmqBundle\Rpc\Exception\RpcInternalErrorException;
8
use Cmobi\RabbitmqBundle\Rpc\Request\RpcRequestCollectionInterface;
9
use Cmobi\RabbitmqBundle\Rpc\Response\RpcResponse;
10
use Cmobi\RabbitmqBundle\Rpc\Response\RpcResponseCollection;
11
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
12
13
class Handler
14
{
15
    use ContainerAwareTrait;
16
17
    private $resolver;
18
19
    public function __construct()
20
    {
21
        $this->resolver = new RpcControllerResolver();
22
    }
23
24
    /**
25
     * @param RpcRequestCollectionInterface $requests
26
     * @return RpcResponseCollection
27
     */
28
    public function handle(RpcRequestCollectionInterface $requests)
29
    {
30
        $responses = new RpcResponseCollection();
31
32
        foreach ($requests as $request) {
0 ignored issues
show
Bug introduced by
The expression $requests of type object<Cmobi\RabbitmqBun...estCollectionInterface> is not traversable.
Loading history...
33
34
            if (!$request->attributes->get('error')) {
35
36
                $controller = $this->getResolver()->getController($request);
37
                $arguments = $this->getResolver()->getArguments($request, $controller);
38
                $response = call_user_func_array($controller, $arguments);
39
40
                if (!is_array($response)) {
41
                    $previous = new InvalidBodyAMQPMessageException('Invalid Body: Content should be array.');
42
                    $exception = new RpcInternalErrorException($previous);
43
                    $error = new RpcResponse($request->id, $request->method, $request->attributes, $exception);
44
                    $responses->add($error);
45
                } else {
46
                    $response = new RpcResponse($response);
47
                    $response->setId($request->id);
48
                    $response->setMethod($request->method);
49
                    $responses->add($response);
50
                }
51
            } else {
52
                $error = $request->attributes->get('error');
53
                $request->attributes->remove('error');
54
                $response = new RpcResponse($request->id, $request->method, $request, $error);
55
                $responses->add($response);
56
            }
57
        }
58
59
        return $responses;
60
    }
61
62
    /**
63
     * @return RpcControllerResolver
64
     */
65
    public function getResolver()
66
    {
67
        return $this->resolver;
68
    }
69
70
    /**
71
     * @return \Symfony\Component\DependencyInjection\ContainerInterface
72
     */
73
    public function getContainer()
74
    {
75
        return $this->container;
76
    }
77
}