Completed
Push — master ( 98184f...a2057a )
by Pavel
06:17
created

LoggableRpcClient   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 58
ccs 21
cts 21
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A invoke() 0 17 3
A getContext() 0 8 2
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
     * @var bool
18
     */
19
    private $debug;
20
21
    /**
22
     * LoggableRpcClient constructor.
23
     *
24
     * @param RpcClientInterface $decoratedClient
25
     * @param LoggerInterface    $logger
26
     * @param bool               $debug
27
     */
28 5
    public function __construct(RpcClientInterface $decoratedClient, LoggerInterface $logger = null, $debug = false)
29
    {
30 5
        $this->decoratedClient = $decoratedClient;
31 5
        $this->logger          = $logger ?: new NullLogger();
32 5
        $this->debug           = $debug;
33 5
    }
34
35
    /** {@inheritdoc} */
36 5
    public function invoke($calls)
37
    {
38
        /** @var RpcRequestInterface[] $loggedCalls */
39 5
        $loggedCalls = $calls;
40 5
        if (!is_array($loggedCalls)) {
41 2
            $loggedCalls = [$loggedCalls];
42 2
        }
43
44 5
        foreach ($loggedCalls as $call) {
45 5
            $this->logger->info(
46 5
                sprintf('%s Invoking RPC method "%s"', spl_object_hash($call), $call->getMethod()),
47 5
                $this->getContext($call)
48 5
            );
49 5
        }
50
51 5
        return new LoggableResponseCollection($this->decoratedClient->invoke($calls), $this->logger, $this->debug);
52
    }
53
54
    /**
55
     * @param $call
56
     *
57
     * @return array
58
     */
59 5
    private function getContext($call)
60
    {
61 5
        if (!$this->debug) {
62 4
            return [];
63
        }
64
65 1
        return json_decode(json_encode($call->getParameters()), true);
66
    }
67
}
68