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

UserResourceMetadataFactory::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 15
ccs 0
cts 8
cp 0
crap 6
rs 9.9666
c 1
b 0
f 0
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