EventLog::makeLogEvent()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 2
nop 3
dl 0
loc 18
ccs 8
cts 8
cp 1
crap 4
rs 9.9666
c 2
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BlueEvent\Event\Base;
6
7
use SimpleLog\Log;
8
9
class EventLog
10
{
11
    /**
12
     * store all event names to log
13
     *
14
     * @var array
15
     */
16
    public $logEvents = [];
17
18
    /**
19
     * store logger instance
20
     *
21
     * @var \SimpleLog\LogInterface
22
     */
23
    protected $loggerInstance;
24
25
    /**
26
     * store default options for event dispatcher
27
     *
28
     * @var array
29
     */
30
    protected $options = [];
31
32
    /**
33
     * EventLog constructor.
34
     *
35
     * @param array $logConfig
36
     * @throws \ReflectionException
37
     */
38 24
    public function __construct(array $logConfig)
39
    {
40 24
        $this->options = $logConfig;
41
42
        if (
43 24
            $this->options['log_object']
44 24
            && $this->options['log_object'] instanceof \SimpleLog\LogInterface
45
        ) {
46 1
            $this->loggerInstance = $this->options['log_object'];
47
        } else {
48 23
            $this->loggerInstance = new Log($this->options['log_config']);
49
        }
50
    }
51
52
    /**
53
     * check that event data can be logged and create log message
54
     *
55
     * @param string $name
56
     * @param mixed $eventListener
57
     * @param bool|string $status
58
     * @return $this
59
     */
60 16
    public function makeLogEvent(string $name, $eventListener, $status): self
61
    {
62
        if (
63 16
            $this->options['log_events']
64 4
            && ($this->options['log_all_events']
65 16
                || \in_array($name, $this->logEvents, true)
66
            )
67
        ) {
68 4
            $this->loggerInstance->makeLog(
69
                [
70 4
                    'event_name' => $name,
71 4
                    'listener' => $this->getListenerData($eventListener),
72
                    'status' => $status
73
                ]
74
            );
75
        }
76
77 16
        return $this;
78
    }
79
80
    /**
81
     * @param mixed $eventListener
82
     * @return string
83
     */
84 4
    protected function getListenerData($eventListener): string
85
    {
86
        switch (true) {
87 4
            case $eventListener instanceof \Closure:
88 2
                return 'Closure';
89
90 4
            case \is_array($eventListener):
91 2
                return $eventListener[0] . '::' . $eventListener[1];
92
93
            default:
94 3
                return $eventListener;
95
        }
96
    }
97
}
98