Completed
Push — master ( 66bdc9...03e971 )
by Roni
01:25
created

MonologLogger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\Entity\BaseAuditLog as AuditLog;
17
18
class MonologLogger implements LoggerInterface
19
{
20
    /**
21
     * @var \Psr\Log\LoggerInterface
22
     */
23
    private $logger;
24
25
    private static $ignoreProperties = array('description', 'id', 'level');
26
27
    public function __construct(\Psr\Log\LoggerInterface $logger)
28
    {
29
        $this->logger = $logger;
30
    }
31
32
    public function log(AuditLog $event = NULL)
33
    {
34
        if ($event === NULL) {
35
            return;
36
        }
37
38
        $this->logger->log($event->getLevel(), $event->getDescription(), $this->getContextArray($event));
39
    }
40
41
    /**
42
     * @param \ReflectionObject $refObject
43
     *
44
     * @return ReflectionProperty[]
45
     */
46
    protected function getAllProperties(\ReflectionObject $refObject)
47
    {
48
        return $refObject->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_STATIC);
49
    }
50
51
    /**
52
     * @param AuditLog $event
53
     *
54
     * @return array
55
     */
56
    protected function getContextArray(AuditLog $event)
57
    {
58
        $accessor = PropertyAccess::createPropertyAccessor();
59
60
        $refObject = new \ReflectionObject($event);
61
62
        $arr = array();
63
64
        foreach ($this->getAllProperties($refObject) as $property) {
65
            if (in_array($property->getName(), self::$ignoreProperties)) {
66
                continue;
67
            }
68
            try {
69
                $arr[$property->getName()] = $accessor->getValue($event, $property->getName());
70
            } catch (\Exception $exception) {
71
                continue;
72
            }
73
        }
74
75
        return $arr;
76
    }
77
}
78