Completed
Pull Request — master (#3)
by Bastien
02:27
created

DeamonLoggerExtraWebProcessor::addInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Deamon\LoggerExtraBundle\Processors\Monolog;
4
5
use Symfony\Bridge\Monolog\Processor\WebProcessor as BaseWebProcessor;
6
use Symfony\Component\DependencyInjection\Container;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
9
use Symfony\Component\Security\Core\User\UserInterface;
10
11
class DeamonLoggerExtraWebProcessor extends BaseWebProcessor
12
{
13
    /**
14
     * @var Container
15
     */
16
    private $container = null;
17
18
    /**
19
     * @var array|null
20
     */
21
    private $displayConfig;
22
23
    /**
24
     * @var string
25
     */
26
    private $channelPrefix;
27
28
    /**
29
     * @var array
30
     */
31
    private $record;
32
33 6
    public function __construct($container = null, array $config = null)
34
    {
35 6
        parent::__construct();
36 6
        $this->container = $container;
37 6
        $this->channelPrefix = $config['channel_prefix'];
38 6
        $this->displayConfig = $config['display'];
39 6
    }
40
41
    /**
42
     * @param array $record
43
     *
44
     * @return array
45
     */
46 6
    public function __invoke(array $record)
47
    {
48 6
        $this->record = parent::__invoke($record);
49
50 6
        if ($this->container === null) {
51 1
            return $this->record;
52
        }
53
54 5
        $this->addContextInfo();
55 5
        $this->addRequestInfo();
56 5
        $this->addUserInfo();
57 5
        $this->addChannelInfo();
58
59 5
        return $this->record;
60
    }
61
62 1
    public function setContainer($container)
63
    {
64 1
        $this->container = $container;
65 1
    }
66
67 5
    private function addContextInfo()
68
    {
69 5
        $this->addInfo('env', $this->container->get('kernel')->getEnvironment());
70
71 5
        $context = $this->container->get('deamon.logger_extra.context');
72
73 5
        $this->addInfo('locale', $context->getLocale());
74 5
        if ($this->configShowExtraInfo('application_name')) {
75 1
            $this->record['extra']['application'] = $context->getApplicationName();
76 1
        }
77 5
    }
78
79 5
    private function addRequestInfo()
80
    {
81 5
        if (null !== $request_stack = $this->container->get('request_stack')) {
82 5
            $request = $request_stack->getCurrentRequest();
83 5
            if ($request instanceof Request) {
84 5
                $this->addInfo('url', $request->getRequestUri());
85 5
                $this->addInfo('route', $request->get('_route'));
86 5
                $this->addInfo('user_agent', $request->server->get('HTTP_USER_AGENT'));
87 5
                $this->addInfo('accept_encoding', $request->headers->get('Accept-Encoding'));
88 5
                $this->addInfo('client_ip', $request->getClientIp());
89 5
            }
90 5
        }
91 5
    }
92
93 5
    private function addUserInfo()
94
    {
95 5
        if ($this->configShowExtraInfo('user')) {
96 1
            $token = $this->container->get('security.token_storage')->getToken();
97 1
            if (($token instanceof TokenInterface) && ($token->getUser() instanceof UserInterface) && null !== $user = $token->getUser()) {
98 1
                if ($this->configShowExtraInfo('user_id') && method_exists($user, 'getId')) {
99 1
                    $this->record['extra']['user_id'] = $user->getId();
0 ignored issues
show
Bug introduced by
The method getId() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100 1
                }
101 1
                if ($this->configShowExtraInfo('user_email') && method_exists($user, 'getEmail')) {
102 1
                    $this->record['extra']['user_email'] = $user->getEmail();
0 ignored issues
show
Bug introduced by
The method getEmail() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103 1
                }
104 1
                if ($this->configShowExtraInfo('user_name') && method_exists($user, 'getUsername')) {
105 1
                    $this->record['extra']['user_name'] = $user->getUsername();
106 1
                }
107 1
            }
108 1
        }
109 5
    }
110
111 5
    private function addChannelInfo()
112
    {
113 5
        $this->addInfo('global_channel', $this->record['channel']);
114
115 5
        if ($this->channelPrefix !== null) {
116 1
            $this->record['channel'] = sprintf('%s.%s', $this->channelPrefix, $this->record['channel']);
117 1
        }
118 5
    }
119
120
    /**
121
     * @param string $key
122
     * @param mixed  $value
123
     */
124 5
    private function addInfo($key, $value)
125
    {
126 5
        if ($this->configShowExtraInfo($key)) {
127 4
            $this->record['extra'][$key] = $value;
128 4
        }
129 5
    }
130
131
    /**
132
     * @param string $extraInfo
133
     *
134
     * @return bool
135
     */
136 5
    private function configShowExtraInfo($extraInfo)
137
    {
138 5
        return isset($this->displayConfig[$extraInfo]) && $this->displayConfig[$extraInfo];
139
    }
140
}
141