MonologLogger::getContextArray()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 17
rs 10
cc 4
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the XiideaEasyAuditBundle package.
5
 *
6
 * (c) Xiidea <http://www.xiidea.net>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Xiidea\EasyAuditBundle\Logger;
13
14
use ReflectionProperty;
15
use Symfony\Component\PropertyAccess\PropertyAccess;
16
use Xiidea\EasyAuditBundle\Model\BaseAuditLog as AuditLog;
17
18
class MonologLogger implements LoggerInterface
19
{
20
    private static $ignoreProperties = array('description', 'id', 'level');
21
22
    public function __construct(private \Psr\Log\LoggerInterface $logger)
23
    {
24
    }
25
26
    #[\Override]
27
    public function log(AuditLog $event = null)
28
    {
29
        if (null === $event) {
30
            return;
31
        }
32
33
        $this->logger->log($event->getLevel(), $event->getDescription(), $this->getContextArray($event));
34
    }
35
36
    /**
37
     * @param \ReflectionObject $refObject
38
     *
39
     * @return ReflectionProperty[]
40
     */
41
    protected function getAllProperties(\ReflectionObject $refObject)
42
    {
43
        return $refObject->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_STATIC);
44
    }
45
46
    /**
47
     * @param AuditLog $event
48
     *
49
     * @return array
50
     */
51
    protected function getContextArray(AuditLog $event)
52
    {
53
        $accessor = PropertyAccess::createPropertyAccessor();
54
55
        $refObject = new \ReflectionObject($event);
56
57
        $arr = array();
58
59
        foreach ($this->getAllProperties($refObject) as $property) {
60
            if (!$accessor->isReadable($event, $property->getName()) || in_array($property->getName(), self::$ignoreProperties)) {
61
                continue;
62
            }
63
64
            $arr[$property->getName()] = $accessor->getValue($event, $property->getName());
65
        }
66
67
        return $arr;
68
    }
69
}
70