Passed
Pull Request — master (#74)
by Rustam
03:00
created

ProxyLogTrait::log()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 6
dl 0
loc 17
ccs 0
cts 7
cp 0
crap 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Proxy;
6
7
use Yiisoft\Yii\Debug\Event\ProxyMethodCallEvent;
8
use function get_class;
9
10
trait ProxyLogTrait
11
{
12
    private ContainerProxyConfig $config;
13
14 7
    protected function logProxy(
15
        string $service,
16
        object $instance,
17
        string $method,
18
        array $arguments,
19
        $result,
20
        float $timeStart
21
    ): void {
22 7
        $error = $this->getCurrentError();
0 ignored issues
show
Bug introduced by
It seems like getCurrentError() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
        /** @scrutinizer ignore-call */ 
23
        $error = $this->getCurrentError();
Loading history...
23 7
        $this->processLogData($arguments, $result, $error);
24
25 7
        if ($this->config->getCollector() !== null) {
26 6
            $this->logToCollector($service, $instance, $method, $arguments, $result, $error, $timeStart);
27
        }
28
29 7
        if ($this->config->getDispatcher() !== null) {
30 6
            $this->logToEvent($service, $instance, $method, $arguments, $result, $error, $timeStart);
31
        }
32 7
    }
33
34 7
    private function processLogData(array &$arguments, &$result, ?object &$error): void
35
    {
36 7
        if (!($this->config->getLogLevel() & ContainerInterfaceProxy::LOG_ARGUMENTS)) {
37 1
            $arguments = null;
38
        }
39
40 7
        if (!($this->config->getLogLevel() & ContainerInterfaceProxy::LOG_RESULT)) {
41 7
            $result = null;
42
        }
43
44 7
        if (!($this->config->getLogLevel() & ContainerInterfaceProxy::LOG_ERROR)) {
45 7
            $error = null;
46
        }
47 7
    }
48
49 6
    private function logToCollector(
50
        string $service,
51
        object $instance,
52
        string $method,
53
        ?array $arguments,
54
        $result,
55
        ?object $error,
56
        float $timeStart
57
    ): void {
58 6
        $this->config->getCollector()->collect(
59 6
            $service,
60 6
            get_class($instance),
61
            $method,
62
            $arguments,
63
            $result,
64 6
            $this->getCurrentResultStatus(),
0 ignored issues
show
Bug introduced by
It seems like getCurrentResultStatus() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
            $this->/** @scrutinizer ignore-call */ 
65
                   getCurrentResultStatus(),
Loading history...
65
            $error,
66
            $timeStart,
67 6
            microtime(true),
68
        );
69 6
    }
70
71 6
    private function logToEvent(
72
        string $service,
73
        object $instance,
74
        string $method,
75
        ?array $arguments,
76
        $result,
77
        ?object $error,
78
        float $timeStart
79
    ): void {
80 6
        $this->config->getDispatcher()->dispatch(
81 6
            new ProxyMethodCallEvent(
82 6
                $service,
83 6
                get_class($instance),
84
                $method,
85
                $arguments,
86
                $result,
87 6
                $this->getCurrentResultStatus(),
88
                $error,
89
                $timeStart,
90 6
                microtime(true),
91
            )
92
        );
93 6
    }
94
}
95