Failed Conditions
Push — issue#767 ( a787f5...dafcb3 )
by Guilherme
05:02
created

LoggedInUserListener::onKernelRequest()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
ccs 11
cts 11
cp 1
crap 4
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\APIBundle\Event;
12
13
use LoginCidadao\OAuthBundle\Entity\AccessTokenRepository;
14
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15
use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken;
16
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17
use SimpleThings\EntityAudit\AuditConfiguration;
18
use Symfony\Component\HttpKernel\HttpKernel;
19
use LoginCidadao\OAuthBundle\Model\ClientUser;
20
21
class LoggedInUserListener
22
{
23
    /** @var AccessTokenRepository */
24
    private $accessTokenRepository;
25
26
    /** @var TokenStorageInterface */
27
    private $tokenStorage;
28
29
    /** @var AuditConfiguration */
30
    private $auditConfig;
31
32 3
    public function __construct(
33
        AccessTokenRepository $accessTokenRepository,
34
        TokenStorageInterface $tokenStorage,
35
        AuditConfiguration $auditConfig
36
    ) {
37 3
        $this->accessTokenRepository = $accessTokenRepository;
38 3
        $this->tokenStorage = $tokenStorage;
39 3
        $this->auditConfig = $auditConfig;
40 3
    }
41
42 3
    public function onKernelRequest(GetResponseEvent $event)
43
    {
44 3
        if (HttpKernel::MASTER_REQUEST !== $event->getRequestType()) {
45
            // don't do anything if it's not the master request
46 1
            return;
47
        }
48
49 2
        $token = $this->tokenStorage->getToken();
50 2
        if (!$token instanceof OAuthToken) {
51 1
            return;
52
        }
53
54 1
        $this->auditConfig->setCurrentUsername("OAuthToken:{$token->getToken()}");
0 ignored issues
show
Deprecated Code introduced by
The function SimpleThings\EntityAudit...n::setCurrentUsername() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

54
        /** @scrutinizer ignore-deprecated */ $this->auditConfig->setCurrentUsername("OAuthToken:{$token->getToken()}");
Loading history...
55
56
        // Handling Client Credentials
57 1
        if ($token->getUser() === null) {
58 1
            $accessToken = $this->accessTokenRepository->findOneBy(['token' => $token->getToken()]);
59 1
            $client = $accessToken->getClient();
60
61 1
            $token->setUser(new ClientUser($client));
62
        }
63 1
    }
64
}
65