Passed
Push — main ( ceca55...00ce52 )
by Daniel
03:51
created

createMeOperation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 2
rs 10
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\ApiPlatform\Metadata\Resource;
15
16
use ApiPlatform\Metadata\ApiResource;
17
use ApiPlatform\Metadata\Get;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\Metadata\Operations;
21
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
22
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
23
use Silverback\ApiComponentsBundle\DataProvider\StateProvider\UserStateProvider;
24
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
25
26
/**
27
 * Adds a /me endpoint.
28
 *
29
 * @author Daniel West <[email protected]>
30
 */
31
class UserResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
32
{
33
    public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $decorated)
34
    {
35
    }
36
37
    public function create(string $resourceClass): ResourceMetadataCollection
38
    {
39
        $resourceMetadata = $this->decorated->create($resourceClass);
40
        if (!is_a($resourceClass, AbstractUser::class, true)) {
41
            return $resourceMetadata;
42
        }
43
44
        $input = [];
45
        /** @var ApiResource $resourceMetadatum */
46
        foreach ($resourceMetadata as $resourceMetadatum) {
47
            $operations = $resourceMetadatum->getOperations();
48
            if ($operations) {
49
                $getOperation = $this->findGetOperation($operations);
50
                if ($getOperation) {
51
                    $newOperations = [
52
                        $this->createMeOperation($getOperation),
53
                    ];
54
                    foreach ($newOperations as $newOperation) {
55
                        $operations->add($newOperation->getName(), $newOperation);
56
                    }
57
                }
58
            }
59
            $input[] = $resourceMetadatum;
60
        }
61
62
        return new ResourceMetadataCollection($resourceClass, $input);
63
    }
64
65
    private function findGetOperation(Operations $operations): ?Get
66
    {
67
        foreach ($operations as $operation) {
68
            if ($operation instanceof Get) {
69
                return $operation;
70
            }
71
        }
72
73
        return null;
74
    }
75
76
    private function createMeOperation(Get $operation): Operation
77
    {
78
        return (new HttpOperation(HttpOperation::METHOD_GET, '/me{._format}'))
79
            ->withName('me')
80
            ->withShortName($operation->getShortName())
81
            ->withClass($operation->getClass())
82
            ->withSecurity('is_granted("IS_AUTHENTICATED_FULLY")')
83
            ->withProvider(UserStateProvider::class);
84
    }
85
}
86