Completed
Push — master ( d2992a...8fe16f )
by Michał
06:47
created

EventLog::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 1
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
    public function __construct(array $logConfig)
36
    {
37
        $this->options = $logConfig;
38
39
        if ($this->options['log_object']
40
            && $this->options['log_object'] instanceof \SimpleLog\LogInterface
41
        ) {
42
            $this->loggerInstance = $this->options['log_object'];
43
        } else {
44
            $this->loggerInstance = new Log($this->options['log_config']);
45
        }
46
    }
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
    public function makeLogEvent($name, $eventListener, $status)
57
    {
58
        if ($this->options['log_events']
59
            && ($this->options['log_all_events']
60
                || in_array($name, $this->logEvents, true)
61
            )
62
        ) {
63
            $this->loggerInstance->makeLog(
64
                [
65
                    'event_name' => $name,
66
                    'listener' => $this->getListenerData($eventListener),
67
                    'status' => $status
68
                ]
69
            );
70
        }
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param mixed $eventListener
77
     * @return string
78
     */
79
    protected function getListenerData($eventListener)
80
    {
81
        switch (true) {
82
            case $eventListener instanceof \Closure:
83
                return 'Closure';
84
85
            case is_array($eventListener):
86
                return $eventListener[0] . '::' . $eventListener[1];
87
88
            default:
89
                return $eventListener;
90
        }
91
    }
92
}
93