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

UserResourceMetadataFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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