Passed
Push — master ( 63682c...9260c0 )
by Daniel
06:38
created

UserResourceMetadataFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 24
ccs 0
cts 11
cp 0
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 15 2
A __construct() 0 3 1
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\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
17
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
18
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
19
20
/**
21
 * Adds a /me endpoint.
22
 *
23
 * @author Daniel West <[email protected]>
24
 */
25
class UserResourceMetadataFactory implements ResourceMetadataFactoryInterface
26
{
27
    private ResourceMetadataFactoryInterface $decorated;
28
29
    public function __construct(ResourceMetadataFactoryInterface $decorated)
30
    {
31
        $this->decorated = $decorated;
32
    }
33
34
    public function create(string $resourceClass): ResourceMetadata
35
    {
36
        $resourceMetadata = $this->decorated->create($resourceClass);
37
        if (!is_a($resourceClass, AbstractUser::class, true)) {
38
            return $resourceMetadata;
39
        }
40
41
        $itemOperations = $resourceMetadata->getItemOperations() ?? [];
42
        $itemOperations['me'] = array_replace_recursive([], $itemOperations['get'], [
43
            'method' => 'GET',
44
            'path' => '/me.{_format} ',
45
            'security' => 'is_granted("IS_AUTHENTICATED_FULLY")',
46
        ]);
47
48
        return $resourceMetadata->withItemOperations($itemOperations);
49
    }
50
}
51