Passed
Push — main ( 964870...8374cd )
by Daniel
08:40 queued 03:10
created

RouteStateProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 22
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A provide() 0 11 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\Doctrine\Orm\State\CollectionProvider;
17
use ApiPlatform\Doctrine\Orm\State\ItemProvider;
18
use ApiPlatform\Metadata\CollectionOperationInterface;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\State\ProviderInterface;
21
use Silverback\ApiComponentsBundle\Repository\Core\RouteRepository;
22
23
/**
24
 * @author Daniel West <[email protected]>
25
 */
26
class RouteStateProvider implements ProviderInterface
27
{
28
    private RouteRepository $routeRepository;
29
    private ProviderInterface $defaultProvider;
30
31
    public function __construct(RouteRepository $routeRepository, ProviderInterface $defaultProvider)
32
    {
33
        $this->routeRepository = $routeRepository;
34
        $this->defaultProvider = $defaultProvider;
35
    }
36
37
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
38
    {
39
        if ($operation instanceof CollectionOperationInterface) {
40
            return $this->defaultProvider->provide($operation->withProvider(CollectionProvider::class), $uriVariables, $context);
41
        }
42
        $id = $uriVariables['id'];
43
        if (!\is_string($id)) {
44
            return $this->defaultProvider->provide($operation->withProvider(ItemProvider::class), $uriVariables, $context);
45
        }
46
47
        return $this->routeRepository->findOneByIdOrPath($id);
48
    }
49
}
50