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

LoggableRpcClient   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 38
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A invoke() 0 17 3
1
<?php
2
3
namespace ScayTrase\Api\Rpc\Decorators;
4
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\NullLogger;
7
use ScayTrase\Api\Rpc\RpcClientInterface;
8
use ScayTrase\Api\Rpc\RpcRequestInterface;
9
10
final class LoggableRpcClient implements RpcClientInterface
11
{
12
    /** @var  LoggerInterface */
13
    private $logger;
14
    /** @var  RpcClientInterface */
15
    private $decoratedClient;
16
17
    /**
18
     * LoggableRpcClient constructor.
19
     *
20
     * @param RpcClientInterface $decoratedClient
21
     * @param LoggerInterface $logger
22
     */
23 4
    public function __construct(RpcClientInterface $decoratedClient, LoggerInterface $logger = null)
24
    {
25 4
        $this->decoratedClient = $decoratedClient;
26 4
        $this->logger = $logger ?: new NullLogger();
27 4
    }
28
29
    /** {@inheritdoc} */
30 4
    public function invoke($calls)
31
    {
32
        /** @var RpcRequestInterface[] $loggedCalls */
33 4
        $loggedCalls = $calls;
34 4
        if (!is_array($loggedCalls)) {
35 2
            $loggedCalls = [$loggedCalls];
36 2
        }
37
38 4
        foreach ($loggedCalls as $call) {
39 4
            $this->logger->debug(
40 4
                sprintf('%s Invoking RPC method "%s"', spl_object_hash($call), $call->getMethod()),
41 4
                json_decode(json_encode($call->getParameters()), true)
42 4
            );
43 4
        }
44
45 4
        return new LoggableResponseCollection($this->decoratedClient->invoke($calls), $this->logger);
46
    }
47
}
48