Passed
Push — master ( 3f23ea...c594c9 )
by Jan
05:15 queued 11s
created

EventLogger   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 106
rs 10
wmc 18

5 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldBeAdded() 0 28 6
A __construct() 0 7 1
A logAndFlush() 0 5 1
A isObjectClassInArray() 0 15 4
A log() 0 18 6
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\Services\LogSystem;
23
24
25
use App\Entity\LogSystem\AbstractLogEntry;
26
use App\Entity\UserSystem\User;
27
use Doctrine\ORM\EntityManagerInterface;
28
use Symfony\Component\Security\Core\Security;
29
30
class EventLogger
31
{
32
    protected $minimum_log_level;
33
    protected $blacklist;
34
    protected $whitelist;
35
    protected $em;
36
    protected $security;
37
38
    public function __construct(int $minimum_log_level, array $blacklist, array $whitelist, EntityManagerInterface $em, Security $security)
39
    {
40
        $this->minimum_log_level = $minimum_log_level;
41
        $this->blacklist = $blacklist;
42
        $this->whitelist = $whitelist;
43
        $this->em = $em;
44
        $this->security = $security;
45
    }
46
47
    /**
48
     * Adds the given log entry to the Log, if the entry fullfills the global configured criterias.
49
     * The change will not be flushed yet.
50
     * @param  AbstractLogEntry  $logEntry
51
     * @return bool Returns true, if the event was added to log.
52
     */
53
    public function log(AbstractLogEntry $logEntry): bool
54
    {
55
        $user = $this->security->getUser();
56
        //If the user is not specified explicitly, set it to the current user
57
        if (($user === null || $user instanceof User) && $logEntry->getUser() === null) {
58
            if ($user === null) {
0 ignored issues
show
introduced by
The condition $user === null is always true.
Loading history...
59
                $repo = $this->em->getRepository(User::class);
60
                $user = $repo->getAnonymousUser();
61
            }
62
            $logEntry->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of App\Entity\LogSystem\AbstractLogEntry::setUser() does only seem to accept App\Entity\UserSystem\User, 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

62
            $logEntry->setUser(/** @scrutinizer ignore-type */ $user);
Loading history...
63
        }
64
65
        if ($this->shouldBeAdded($logEntry)) {
66
            $this->em->persist($logEntry);
67
            return true;
68
        }
69
70
        return false;
71
    }
72
73
    /**
74
     * Adds the given log entry to the Log, if the entry fullfills the global configured criterias and flush afterwards.
75
     * @param  AbstractLogEntry  $logEntry
76
     * @return bool Returns true, if the event was added to log.
77
     */
78
    public function logAndFlush(AbstractLogEntry $logEntry): bool
79
    {
80
        $tmp = $this->log($logEntry);
81
        $this->em->flush();
82
        return $tmp;
83
    }
84
85
    public function shouldBeAdded(
86
        AbstractLogEntry $logEntry,
87
        ?int $minimum_log_level = null,
88
        ?array $blacklist = null,
89
        ?array $whitelist = null
90
    ): bool {
91
        //Apply the global settings, if nothing was specified
92
        $minimum_log_level = $minimum_log_level ?? $this->minimum_log_level;
93
        $blacklist = $blacklist ?? $this->blacklist;
94
        $whitelist = $whitelist ?? $this->whitelist;
95
96
        //Dont add the entry if it does not reach the minimum level
97
        if ($logEntry->getLevel() > $minimum_log_level) {
98
            return false;
99
        }
100
101
        //Check if the event type is black listed
102
        if (!empty($blacklist) && $this->isObjectClassInArray($logEntry, $blacklist)) {
103
            return false;
104
        }
105
106
        //Check for whitelisting
107
        if (!empty($whitelist) && !$this->isObjectClassInArray($logEntry, $whitelist)) {
108
            return false;
109
        }
110
111
        // By default all things should be added
112
        return true;
113
    }
114
115
    /**
116
     * Check if the object type is given in the classes array. This also works for inherited types
117
     * @param  object  $object The object which should be checked
118
     * @param  string[]  $classes The list of class names that should be used for checking.
119
     * @return bool
120
     */
121
    protected function isObjectClassInArray(object $object, array $classes): bool
122
    {
123
        //Check if the class is directly in the classes array
124
        if (in_array(get_class($object), $classes)) {
125
            return true;
126
        }
127
128
        //Iterate over all classes and check for inheritance
129
        foreach ($classes as $class) {
130
            if (is_a($object, $class)) {
131
                return true;
132
            }
133
        }
134
135
        return false;
136
    }
137
}