|
13 | 13 |
|
14 | 14 | namespace Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource; |
15 | 15 |
|
16 | | -use ApiPlatform\Metadata\ApiResource; |
17 | 16 | use ApiPlatform\Metadata\Get; |
18 | | -use ApiPlatform\Metadata\HttpOperation; |
19 | 17 | use ApiPlatform\Metadata\Operation; |
20 | | -use ApiPlatform\Metadata\Operations; |
21 | 18 | use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
22 | 19 | use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
23 | 20 | use Silverback\ApiComponentsBundle\DataProvider\StateProvider\UserStateProvider; |
|
28 | 25 | * |
29 | 26 | * @author Daniel West <daniel@silverback.is> |
30 | 27 | */ |
31 | | -class UserResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface |
| 28 | +readonly class UserResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface |
32 | 29 | { |
33 | | - public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $decorated) |
| 30 | + public function __construct(private ResourceMetadataCollectionFactoryInterface $decorated) |
34 | 31 | { |
35 | 32 | } |
36 | 33 |
|
37 | 34 | public function create(string $resourceClass): ResourceMetadataCollection |
38 | 35 | { |
39 | | - $resourceMetadata = $this->decorated->create($resourceClass); |
| 36 | + $resourceMetadataCollection = $this->decorated->create($resourceClass); |
| 37 | + |
40 | 38 | if (!is_a($resourceClass, AbstractUser::class, true)) { |
41 | | - return $resourceMetadata; |
| 39 | + return $resourceMetadataCollection; |
42 | 40 | } |
43 | 41 |
|
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 | | - } |
| 42 | + foreach ($resourceMetadataCollection as $i => $resource) { |
| 43 | + $resource = $resource->withCacheHeaders([ |
| 44 | + 'public' => false, |
| 45 | + 'shared_max_age' => 0, |
| 46 | + 'max_age' => 0, |
| 47 | + ]); |
| 48 | + $operations = $resource->getOperations(); |
| 49 | + foreach ($operations as $key => $operation) { |
| 50 | + if ($operation instanceof Get) { |
| 51 | + $meOperation = $this->createMeOperation($operation); |
| 52 | + $operations->add( |
| 53 | + '_api_me', |
| 54 | + $meOperation |
| 55 | + ); |
57 | 56 | } |
| 57 | + $operations->add( |
| 58 | + $key, |
| 59 | + $operation->withCacheHeaders([ |
| 60 | + 'public' => false, |
| 61 | + 'shared_max_age' => 0, |
| 62 | + 'max_age' => 0, |
| 63 | + ]) |
| 64 | + ); |
58 | 65 | } |
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 | | - } |
| 66 | + $resourceMetadataCollection[$i] = $resource->withOperations($operations); |
71 | 67 | } |
72 | 68 |
|
73 | | - return null; |
| 69 | + return $resourceMetadataCollection; |
74 | 70 | } |
75 | 71 |
|
76 | 72 | private function createMeOperation(Get $operation): Operation |
77 | 73 | { |
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); |
| 74 | + return new Get( |
| 75 | + uriTemplate: '/me{._format}', |
| 76 | + routePrefix: $operation->getRoutePrefix(), |
| 77 | + cacheHeaders: [ |
| 78 | + 'public' => false, |
| 79 | + 'shared_max_age' => 0, |
| 80 | + 'max_age' => 0, |
| 81 | + ], |
| 82 | + shortName: '__api_me', |
| 83 | + class: $operation->getClass(), |
| 84 | + security: 'is_granted("IS_AUTHENTICATED_FULLY")', |
| 85 | + name: '_api_me', |
| 86 | + provider: UserStateProvider::class |
| 87 | + ); |
84 | 88 | } |
85 | 89 | } |
0 commit comments