Failed Conditions
Push — master ( 349866...67c1d1 )
by Florent
10:56 queued 06:08
created

AccessTokenResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\SecurityBundle\Resolver;
15
16
use OAuth2Framework\SecurityBundle\Security\Authentication\Token\OAuth2Token;
17
use OAuth2Framework\Component\Core\AccessToken\AccessToken;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
20
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
21
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
22
23
final class AccessTokenResolver implements ArgumentValueResolverInterface
24
{
25
    /**
26
     * @var TokenStorageInterface
27
     */
28
    private $tokenStorage;
29
30
    /**
31
     * AccessTokenResolver constructor.
32
     *
33
     * @param TokenStorageInterface $tokenStorage
34
     */
35
    public function __construct(TokenStorageInterface $tokenStorage)
36
    {
37
        $this->tokenStorage = $tokenStorage;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function supports(Request $request, ArgumentMetadata $argument)
44
    {
45
        if (AccessToken::class !== $argument->getType()) {
46
            return false;
47
        }
48
49
        return $this->tokenStorage->getToken() instanceof OAuth2Token;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function resolve(Request $request, ArgumentMetadata $argument)
56
    {
57
        $token = $this->tokenStorage->getToken();
58
        if ($token instanceof OAuth2Token) {
59
            yield $token->getAccessToken();
60
        }
61
    }
62
}
63