Event   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 6
dl 0
loc 178
c 0
b 0
f 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 1
A logEvents() 0 12 4
A isLoggableEvent() 0 19 4
A logEvent() 0 20 2
A getObject() 0 10 3
A getAll() 0 4 1
A getObjectName() 0 6 2
A getClassId() 0 9 2
A getEventId() 0 9 2
A makeEventArray() 0 11 2
1
<?php
2
3
namespace PragmaRX\Tracker\Data\Repositories;
4
5
use PragmaRX\Support\Config;
6
use PragmaRX\Tracker\Eventing\EventStorage;
7
8
class Event extends Repository
9
{
10
    /**
11
     * @var EventLog
12
     */
13
    private $eventLogRepository;
14
15
    /**
16
     * @var SystemClass
17
     */
18
    private $systemClassRepository;
19
20
    /**
21
     * @var Log
22
     */
23
    private $logRepository;
24
25
    /**
26
     * @var \PragmaRX\Support\Config
27
     */
28
    private $config;
29
30
    /**
31
     * @var \PragmaRX\Tracker\Eventing\EventStorage
32
     */
33
    private $eventStorage;
34
35
    public function __construct(
36
        $model,
37
        EventStorage $eventStorage,
38
        EventLog $eventLogRepository,
39
        SystemClass $systemClassRepository,
40
        Log $logRepository,
41
        Config $config
42
    ) {
43
        parent::__construct($model);
44
45
        $this->eventStorage = $eventStorage;
46
47
        $this->eventLogRepository = $eventLogRepository;
48
49
        $this->systemClassRepository = $systemClassRepository;
50
51
        $this->logRepository = $logRepository;
52
53
        $this->config = $config;
54
    }
55
56
    public function logEvents()
57
    {
58
        if (!$this->logRepository->getCurrentLogId()) {
59
            return;
60
        }
61
62
        foreach ($this->eventStorage->popAll() as $event) {
63
            if ($this->isLoggableEvent($event)) {
64
                $this->logEvent($event);
65
            }
66
        }
67
    }
68
69
    private function isLoggableEvent($event)
70
    {
71
        $forbidden = $this->config->get('do_not_log_events');
72
73
        // Illuminate Query may cause infinite recursion
74
        $forbidden[] = 'illuminate.query';
75
76
        return
77
            $event['event'] != $this->getObject($event['object'])
78
79
            &&
80
81
            !in_array_wildcard($event['event'], $forbidden)
82
83
            &&
84
85
            !$this->config->get('log_only_events')
86
                || in_array($event['event'], $this->config->get('log_only_events'));
87
    }
88
89
    public function logEvent($event)
90
    {
91
        $event = $this->makeEventArray($event);
92
93
        $evenId = $this->getEventId($event);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $evenId is correct as $this->getEventId($event) (which targets PragmaRX\Tracker\Data\Re...ies\Event::getEventId()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
94
95
        if ($evenId) {
96
            $objectName = $this->getObjectName($event);
97
98
            $classId = $this->getClassId($objectName);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $classId is correct as $this->getClassId($objectName) (which targets PragmaRX\Tracker\Data\Re...ies\Event::getClassId()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
99
100
            $this->eventLogRepository->create(
101
                [
102
                    'log_id'   => $this->logRepository->getCurrentLogId(),
103
                    'event_id' => $evenId,
104
                    'class_id' => $classId,
105
                ]
106
            );
107
        }
108
    }
109
110
    private function getObject($object)
111
    {
112
        if (is_object($object)) {
113
            $object = get_class($object);
114
        } elseif (is_array($object)) {
115
            $object = serialize($object);
116
        }
117
118
        return $object;
119
    }
120
121
    public function getAll($minutes, $results)
122
    {
123
        return $this->getModel()->allInThePeriod($minutes, $results);
124
    }
125
126
    /**
127
     * Get the object name from an event.
128
     *
129
     * @param $event
130
     *
131
     * @return null|string
132
     */
133
    private function getObjectName($event)
134
    {
135
        return isset($event['object'])
136
            ? $this->getObject($event['object'])
137
            : null;
138
    }
139
140
    /**
141
     * Get the system class id by object name.
142
     *
143
     * @param null|string $objectName
144
     *
145
     * @return null
146
     */
147
    private function getClassId($objectName)
148
    {
149
        return $objectName
150
            ? $this->systemClassRepository->findOrCreate(
151
                ['name' => $objectName],
152
                ['name']
153
            )
154
            : null;
155
    }
156
157
    /**
158
     * Get the event id.
159
     *
160
     * @param $event
161
     *
162
     * @return null
163
     */
164
    private function getEventId($event)
165
    {
166
        return $event['event']
167
            ? $this->findOrCreate(
168
                ['name' => $event['event']],
169
                ['name']
170
            )
171
            : null;
172
    }
173
174
    private function makeEventArray($event)
175
    {
176
        if (is_string($event)) {
177
            $event = [
178
                'event'  => $event,
179
                'object' => null,
180
            ];
181
        }
182
183
        return $event;
184
    }
185
}
186