Passed
Push — master ( a2057a...f86d9b )
by Pavel
03:59 queued 10s
created

LoggableRpcClient::getContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 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
    /** @var LoggerInterface */
17
    private $debugLogger;
18
19
    /**
20
     * LoggableRpcClient constructor.
21
     *
22
     * @param RpcClientInterface $decoratedClient
23
     * @param LoggerInterface    $logger
24
     * @param bool               $debug
25
     */
26 5
    public function __construct(RpcClientInterface $decoratedClient, LoggerInterface $logger = null, $debug = false)
27
    {
28 5
        $this->decoratedClient = $decoratedClient;
29 5
        $this->logger          = $logger ?: new NullLogger();
30 5
        $this->debugLogger     = $debug ? $this->logger : new NullLogger();
31 5
    }
32
33
    /** {@inheritdoc} */
34 5
    public function invoke($calls)
35
    {
36
        /** @var RpcRequestInterface[] $loggedCalls */
37 5
        $loggedCalls = $calls;
38 5
        if (!is_array($loggedCalls)) {
39 2
            $loggedCalls = [$loggedCalls];
40 2
        }
41
42 5
        foreach ($loggedCalls as $call) {
43 5
            $this->logger->info(sprintf('%s Invoking RPC method "%s"', spl_object_hash($call), $call->getMethod()));
44 5
            $this->debugLogger->debug(
45 5
                sprintf(
46 5
                    '%s RPC parameters',
47 5
                    spl_object_hash($call)
48 5
                ),
49 5
                json_decode(
50 5
                    json_encode($call->getParameters()),
51
                    true
52 5
                )
53 5
            );
54 5
        }
55
56 5
        return new LoggableResponseCollection(
57 5
            $this->decoratedClient->invoke($calls), $this->logger, $this->debugLogger
58 5
        );
59
    }
60
}
61