Failed Conditions
Push — master ( 1bae70...6a9de1 )
by Florent
40:14
created

SymfonyUserAccountDiscovery::getCurrentAccount()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\ServerBundle\Tests\TestBundle\Service;
15
16
use OAuth2Framework\Component\AuthorizationEndpoint\User\UserAccountDiscovery;
17
use OAuth2Framework\Component\Core\UserAccount\UserAccount;
18
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19
20
final class SymfonyUserAccountDiscovery implements UserAccountDiscovery
21
{
22
    private $tokenStorage;
23
24
    public function __construct(TokenStorageInterface $tokenStorage)
25
    {
26
        $this->tokenStorage = $tokenStorage;
27
    }
28
29
    public function getCurrentAccount(): ?UserAccount
30
    {
31
        $token = $this->tokenStorage->getToken();
32
        if (null === $token) {
33
            throw new \InvalidArgumentException('Unable to retrieve the current user.');
34
        }
35
36
        $user = $token->getUser();
37
        if (!$user instanceof UserAccount) {
38
            throw new \InvalidArgumentException('Unable to retrieve the current user.');
39
        }
40
41
        return $user;
42
    }
43
}
44