Completed
Pull Request — master (#9)
by Bastien
02:21
created

DeamonLoggerExtraWebProcessor::setEnvironment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Deamon\LoggerExtraBundle\Processors\Monolog;
4
5
use Deamon\LoggerExtraBundle\Services\DeamonLoggerExtraContext;
6
use Symfony\Bridge\Monolog\Processor\WebProcessor as BaseWebProcessor;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\RequestStack;
9
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
10
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
11
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
12
13
class DeamonLoggerExtraWebProcessor extends BaseWebProcessor
14
{
15
    /**
16
     * @var string
17
     */
18
    private $environment = null;
19
20
    /**
21
     * @var DeamonLoggerExtraContext
22
     */
23
    private $loggerExtraContext = null;
24
25
    /**
26
     * @var TokenStorageInterface
27
     */
28
    private $tokenStorage = null;
29
30
    /**
31
     * @var RequestStack
32
     */
33
    private $requestStack = null;
34
35
    /**
36
     * @var array|null
37
     */
38
    private $displayConfig;
39
40
    /**
41
     * @var string
42
     */
43
    private $channelPrefix;
44
45
    /** @var string */
46
    private $userClass;
47
48
    /** @var array */
49
    private $userMethods;
50
51
    /**
52
     * @var array
53
     */
54
    private $record;
55
56 9
    public function __construct(array $config = null)
57
    {
58 9
        parent::__construct();
59 9
        $this->channelPrefix = $config['channel_prefix'];
60 9
        $this->displayConfig = $config['display'];
61 9
        $this->userClass = $config['user_class'];
62 9
        $this->userMethods = $config['user_methods'];
63 9
    }
64
65
    /**
66
     * @param array $record
67
     *
68
     * @return array
69
     */
70 9
    public function __invoke(array $record)
71
    {
72 9
        $this->record = parent::__invoke($record);
73
74 9
        $this->addContextInfo();
75 9
        $this->addRequestInfo();
76 9
        $this->addUserInfo();
77 9
        $this->addChannelInfo();
78
79 9
        return $this->record;
80
    }
81
82
    /**
83
     * Add extra info about the context of the generated log.
84
     */
85 9
    private function addContextInfo()
86
    {
87 9
        if (null !== $this->environment) {
88 1
            $this->addInfo('env', $this->environment);
89 1
        }
90
91 9
        if (null !== $this->loggerExtraContext) {
92 1
            $this->addInfo('locale', $this->loggerExtraContext->getLocale());
93 1
            if ($this->configShowExtraInfo('application_name')) {
94 1
                $this->record['extra']['application'] = $this->loggerExtraContext->getApplicationName();
95 1
            }
96 1
        }
97 9
    }
98
99
    /**
100
     * Add extra info about the request generating the log.
101
     */
102 9
    private function addRequestInfo()
103
    {
104 9
        if (null !== $this->requestStack) {
105 1
            $request = $this->requestStack->getCurrentRequest();
106 1
            if ($request instanceof Request) {
107 1
                $this->addInfo('url', $request->getRequestUri());
108 1
                $this->addInfo('route', $request->get('_route'));
109 1
                $this->addInfo('user_agent', $request->server->get('HTTP_USER_AGENT'));
110 1
                $this->addInfo('accept_encoding', $request->headers->get('Accept-Encoding'));
111 1
                $this->addInfo('client_ip', $request->getClientIp());
112 1
            }
113 1
        }
114 9
    }
115
116
    /**
117
     * Add extra info on the user generating the log.
118
     */
119 9
    private function addUserInfo()
120
    {
121 9
        if ($this->configShowExtraInfo('user')) {
122 4
            if (!class_exists($this->userClass) && !interface_exists($this->userClass)) {
123 1
                return;
124
            }
125
126 3
            if (!$this->tokenStorage instanceof TokenStorage) {
127 1
                return;
128
            }
129
130 2
            $token = $this->tokenStorage->getToken();
131 2
            if (($token instanceof TokenInterface) && ($token->getUser() instanceof $this->userClass) && null !== $user = $token->getUser()) {
132 1
                foreach ($this->userMethods as $name => $method) {
133 1
                    if (method_exists($user, $method)) {
134 1
                        $this->record['extra'][$name] = $user->$method();
135 1
                    }
136 1
                }
137 1
            }
138 2
        }
139 7
    }
140
141
    /**
142
     * Add channel info to ease the log interpretation.
143
     */
144 9
    private function addChannelInfo()
145
    {
146 9
        $this->addInfo('global_channel', $this->record['channel']);
147
148 9
        if ($this->channelPrefix !== null) {
149 1
            $this->record['channel'] = sprintf('%s.%s', $this->channelPrefix, $this->record['channel']);
150 1
        }
151 9
    }
152
153
    /**
154
     * Add the extra info if configured to.
155
     *
156
     * @param string $key
157
     * @param mixed  $value
158
     */
159 9
    private function addInfo($key, $value)
160
    {
161 9
        if ($this->configShowExtraInfo($key)) {
162 4
            $this->record['extra'][$key] = $value;
163 4
        }
164 9
    }
165
166
    /**
167
     * Tells if the config to display the extra info is enabled or not.
168
     *
169
     * @param string $extraInfo
170
     *
171
     * @return bool
172
     */
173 9
    private function configShowExtraInfo($extraInfo)
174
    {
175 9
        return isset($this->displayConfig[$extraInfo]) && $this->displayConfig[$extraInfo];
176
    }
177
178
    /**
179
     * @param DeamonLoggerExtraContext $loggerExtraContext
180
     */
181 1
    public function setLoggerExtraContext(DeamonLoggerExtraContext $loggerExtraContext)
182
    {
183 1
        $this->loggerExtraContext = $loggerExtraContext;
184 1
    }
185
186
    /**
187
     * @param string $environment
188
     */
189 1
    public function setEnvironment($environment)
190
    {
191 1
        $this->environment = $environment;
192 1
    }
193
194
    /**
195
     * @param TokenStorageInterface $tokenStorage
196
     */
197 2
    public function setTokenStorage(TokenStorageInterface $tokenStorage)
198
    {
199 2
        $this->tokenStorage = $tokenStorage;
200 2
    }
201
202
    /**
203
     * @param RequestStack $requestStack
204
     */
205 1
    public function setRequestStack(RequestStack $requestStack)
206
    {
207 1
        $this->requestStack = $requestStack;
208 1
    }
209
}
210