UserStateProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 19
ccs 0
cts 8
cp 0
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A provide() 0 8 3
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\DataProvider\StateProvider;
15
16
use ApiPlatform\Metadata\Operation;
17
use ApiPlatform\State\ProviderInterface;
18
use Silverback\ApiComponentsBundle\Repository\User\UserRepositoryInterface;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
21
/**
22
 * @author Daniel West <[email protected]>
23
 */
24
class UserStateProvider implements ProviderInterface
25
{
26
    private UserRepositoryInterface $userRepository;
27
    private RequestStack $requestStack;
28
29
    public function __construct(UserRepositoryInterface $userRepository, RequestStack $requestStack)
30
    {
31
        $this->userRepository = $userRepository;
32
        $this->requestStack = $requestStack;
33
    }
34
35
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
36
    {
37
        $request = $this->requestStack->getCurrentRequest();
38
        if (!$request || !($id = $request->attributes->get('id'))) {
39
            return null;
40
        }
41
42
        return $this->userRepository->loadUserByIdentifier($id);
43
    }
44
}
45