Passed
Branch master (c2d422)
by Bastien
14:08 queued 05:51
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 5
            return;
123
        }
124
125 4
        if (!class_exists($this->userClass) && !interface_exists($this->userClass)) {
126 1
            return;
127
        }
128
129 3
        if (!$this->tokenStorage instanceof TokenStorage) {
130 1
            return;
131
        }
132
133 2
        $token = $this->tokenStorage->getToken();
134 2
        if ($this->isUserInstanceValid($token) && null !== $user = $token->getUser()) {
135 1
            $this->appendUserMethodInfo($user);
136 1
        }
137 2
    }
138
139
    /**
140
     * append method result of user object
141
     *
142
     * @param $user
143
     */
144 1
    private function appendUserMethodInfo($user)
145
    {
146 1
        foreach ($this->userMethods as $name => $method) {
147 1
            if (method_exists($user, $method)) {
148 1
                $this->record['extra'][$name] = $user->$method();
149 1
            }
150 1
        }
151 1
    }
152
153
    /**
154
     * Check if passed token is an instance of TokenInterface and an instance of config UserClass
155
     *
156
     * @param $token
157
     *
158
     * @return bool
159
     */
160 2
    private function isUserInstanceValid($token)
161
    {
162 2
        return $token instanceof TokenInterface && $token->getUser() instanceof $this->userClass;
163
    }
164
165
    /**
166
     * Add channel info to ease the log interpretation.
167
     */
168 9
    private function addChannelInfo()
169
    {
170 9
        $this->addInfo('global_channel', $this->record['channel']);
171
172 9
        if ($this->channelPrefix !== null) {
173 1
            $this->record['channel'] = sprintf('%s.%s', $this->channelPrefix, $this->record['channel']);
174 1
        }
175 9
    }
176
177
    /**
178
     * Add the extra info if configured to.
179
     *
180
     * @param string $key
181
     * @param mixed  $value
182
     */
183 9
    private function addInfo($key, $value)
184
    {
185 9
        if ($this->configShowExtraInfo($key)) {
186 4
            $this->record['extra'][$key] = $value;
187 4
        }
188 9
    }
189
190
    /**
191
     * Tells if the config to display the extra info is enabled or not.
192
     *
193
     * @param string $extraInfo
194
     *
195
     * @return bool
196
     */
197 9
    private function configShowExtraInfo($extraInfo)
198
    {
199 9
        return isset($this->displayConfig[$extraInfo]) && $this->displayConfig[$extraInfo];
200
    }
201
202
    /**
203
     * @param DeamonLoggerExtraContext $loggerExtraContext
204
     */
205 1
    public function setLoggerExtraContext(DeamonLoggerExtraContext $loggerExtraContext)
206
    {
207 1
        $this->loggerExtraContext = $loggerExtraContext;
208 1
    }
209
210
    /**
211
     * @param string $environment
212
     */
213 1
    public function setEnvironment($environment)
214
    {
215 1
        $this->environment = $environment;
216 1
    }
217
218
    /**
219
     * @param TokenStorageInterface $tokenStorage
220
     */
221 2
    public function setTokenStorage(TokenStorageInterface $tokenStorage)
222
    {
223 2
        $this->tokenStorage = $tokenStorage;
224 2
    }
225
226
    /**
227
     * @param RequestStack $requestStack
228
     */
229 1
    public function setRequestStack(RequestStack $requestStack)
230
    {
231 1
        $this->requestStack = $requestStack;
232 1
    }
233
}
234