Completed
Push — master ( ad6910...d77fda )
by Tarmo
18s queued 12s
created

LoggedInUserValueResolver::supports()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 10
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 20
ccs 11
cts 11
cp 1
crap 6
rs 9.2222
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/ArgumentResolver/LoggedInUserValueResolver.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\ArgumentResolver;
10
11
use App\Entity\User;
12
use App\Security\UserTypeIdentification;
13
use Generator;
14
use Lexik\Bundle\JWTAuthenticationBundle\Exception\MissingTokenException;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
17
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
18
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
20
use Throwable;
21
22
/**
23
 * Class LoggedInUserValueResolver
24
 *
25
 * Example how to use this within your controller;
26
 *
27
 *  #[Route(path: 'some-path')]
28
 *  #[Security('is_granted("IS_AUTHENTICATED_FULLY")')]
29
 *  public function someMethod(\App\Entity\User $loggedInUser): Response
30
 *  {
31
 *      ...
32
 *  }
33
 *
34
 * This will automatically convert your security user to actual User entity that
35
 * you can use within your controller as you like.
36
 *
37
 * @package App\ArgumentResolver
38
 * @author TLe, Tarmo Leppänen <[email protected]>
39
 */
40
class LoggedInUserValueResolver implements ArgumentValueResolverInterface
41
{
42
    private ?TokenInterface $token = null;
43
44 126
    public function __construct(
45
        private TokenStorageInterface $tokenStorage,
46
        private UserTypeIdentification $userService,
47
    ) {
48 126
    }
49
50 125
    public function supports(Request $request, ArgumentMetadata $argument): bool
51
    {
52 125
        $output = false;
53 125
        $this->token = $this->tokenStorage->getToken();
54
55
        // only security user implementations are supported
56 125
        if ($this->token instanceof TokenInterface
57 125
            && $argument->getName() === 'loggedInUser'
58 125
            && $argument->getType() === User::class
59
        ) {
60 75
            $securityUser = $this->userService->getSecurityUser();
61
62 75
            if ($securityUser === null && $argument->isNullable() === false) {
63 23
                throw new MissingTokenException('JWT Token not found');
64
            }
65
66 52
            $output = true;
67
        }
68
69 102
        return $output;
70
    }
71
72
    /**
73
     * @throws Throwable
74
     *
75
     * @return Generator<User|null>
76
     */
77 54
    public function resolve(Request $request, ArgumentMetadata $argument): Generator
78
    {
79 54
        if ($this->token === null) {
80 1
            throw new MissingTokenException('JWT Token not found');
81
        }
82
83 53
        yield $this->userService->getUser();
84 52
    }
85
}
86