Completed
Branch develop (343ae8)
by Michał
02:22
created

EventLog::makeLogEvent()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

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