Passed
Pull Request — master (#133)
by Rustam
03:14
created

ProxyLogTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 11
eloc 37
dl 0
loc 92
ccs 28
cts 29
cp 0.9655
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A logToEvent() 0 20 1
A processLogData() 0 12 4
A logProxy() 0 17 3
A logToCollector() 0 19 1
A getCurrentResultStatus() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
use Yiisoft\Yii\Debug\Event\ProxyMethodCallEvent;
8
9
use function get_class;
10
11
trait ProxyLogTrait
12
{
13
    private ContainerProxyConfig $config;
14
15 6
    protected function logProxy(
16
        string $service,
17
        object $instance,
18
        string $method,
19
        array $arguments,
20
        $result,
21
        float $timeStart
22
    ): void {
23 6
        $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

23
        /** @scrutinizer ignore-call */ 
24
        $error = $this->getCurrentError();
Loading history...
24 6
        $this->processLogData($arguments, $result, $error);
25
26 6
        if ($this->config->getCollector() !== null) {
27 6
            $this->logToCollector($service, $instance, $method, $arguments, $result, $error, $timeStart);
28
        }
29
30 6
        if ($this->config->getDispatcher() !== null) {
31 6
            $this->logToEvent($service, $instance, $method, $arguments, $result, $error, $timeStart);
32
        }
33
    }
34
35 6
    private function processLogData(array &$arguments, &$result, ?object &$error): void
36
    {
37 6
        if (!($this->config->getLogLevel() & ContainerInterfaceProxy::LOG_ARGUMENTS)) {
38
            $arguments = null;
39
        }
40
41 6
        if (!($this->config->getLogLevel() & ContainerInterfaceProxy::LOG_RESULT)) {
42 6
            $result = null;
43
        }
44
45 6
        if (!($this->config->getLogLevel() & ContainerInterfaceProxy::LOG_ERROR)) {
46 6
            $error = null;
47
        }
48
    }
49
50 6
    private function logToCollector(
51
        string $service,
52
        object $instance,
53
        string $method,
54
        ?array $arguments,
55
        $result,
56
        ?object $error,
57
        float $timeStart
58
    ): void {
59 6
        $this->config->getCollector()?->collect(
60
            $service,
61 6
            get_class($instance),
62
            $method,
63
            $arguments,
64
            $result,
65 6
            $this->getCurrentResultStatus(),
66
            $error,
67
            $timeStart,
68 6
            microtime(true),
0 ignored issues
show
Bug introduced by
It seems like microtime(true) can also be of type string; however, parameter $timeEnd of Yiisoft\Yii\Debug\Collec...iceCollector::collect() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

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

68
            /** @scrutinizer ignore-type */ microtime(true),
Loading history...
69
        );
70
    }
71
72 6
    private function logToEvent(
73
        string $service,
74
        object $instance,
75
        string $method,
76
        ?array $arguments,
77
        $result,
78
        ?object $error,
79
        float $timeStart
80
    ): void {
81 6
        $this->config->getDispatcher()?->dispatch(
82 6
            new ProxyMethodCallEvent(
83
                $service,
84 6
                get_class($instance),
85
                $method,
86
                $arguments,
87
                $result,
88 6
                $this->getCurrentResultStatus(),
89
                $error,
90
                $timeStart,
91 6
                microtime(true),
0 ignored issues
show
Bug introduced by
It seems like microtime(true) can also be of type string; however, parameter $timeEnd of Yiisoft\Yii\Debug\Event\...allEvent::__construct() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

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

91
                /** @scrutinizer ignore-type */ microtime(true),
Loading history...
92
            )
93
        );
94
    }
95
96 6
    private function getCurrentResultStatus(): string
97
    {
98 6
        if (!$this->hasCurrentError()) {
0 ignored issues
show
Bug introduced by
It seems like hasCurrentError() 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

98
        if (!$this->/** @scrutinizer ignore-call */ hasCurrentError()) {
Loading history...
99 6
            return 'success';
100
        }
101
102 1
        return 'failed';
103
    }
104
}
105