Skip to content

Commit f720071

Browse files
feat: use mercure option
1 parent 63093e3 commit f720071

20 files changed

+45
-540
lines changed

api/.php-cs-fixer.dist.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
'phpdoc_to_comment' => false,
3434
'void_return' => true,
3535
'concat_space' => ['spacing' => 'one'],
36-
'braces' => ['allow_single_line_closure' => true],
3736
'nullable_type_declaration' => true,
3837
])
3938
->setFinder($finder)

api/src/DataFixtures/Factory/BookmarkFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Zenstruck\Foundry\ModelFactory;
1010
use Zenstruck\Foundry\Proxy;
1111
use Zenstruck\Foundry\RepositoryProxy;
12+
1213
use function Zenstruck\Foundry\lazy;
1314

1415
/**

api/src/DataFixtures/Factory/ReviewFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Zenstruck\Foundry\ModelFactory;
1111
use Zenstruck\Foundry\Proxy;
1212
use Zenstruck\Foundry\RepositoryProxy;
13+
1314
use function Zenstruck\Foundry\lazy;
1415

1516
/**

api/src/Entity/Book.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use ApiPlatform\Metadata\GetCollection;
1616
use ApiPlatform\Metadata\Post;
1717
use ApiPlatform\Metadata\Put;
18+
use ApiPlatform\Metadata\UrlGeneratorInterface;
1819
use App\Enum\BookCondition;
1920
use App\Repository\BookRepository;
2021
use App\State\Processor\BookPersistProcessor;
@@ -44,7 +45,6 @@
4445
paginationClientItemsPerPage: true
4546
),
4647
new Post(
47-
// Mercure publish is done manually in MercureProcessor through BookPersistProcessor
4848
processor: BookPersistProcessor::class,
4949
itemUriTemplate: '/admin/books/{id}{._format}'
5050
),
@@ -54,12 +54,10 @@
5454
// https://github.com/api-platform/admin/issues/370
5555
new Put(
5656
uriTemplate: '/admin/books/{id}{._format}',
57-
// Mercure publish is done manually in MercureProcessor through BookPersistProcessor
5857
processor: BookPersistProcessor::class
5958
),
6059
new Delete(
6160
uriTemplate: '/admin/books/{id}{._format}',
62-
// Mercure publish is done manually in MercureProcessor through BookRemoveProcessor
6361
processor: BookRemoveProcessor::class
6462
),
6563
],
@@ -71,7 +69,13 @@
7169
AbstractNormalizer::GROUPS => ['Book:write'],
7270
],
7371
collectDenormalizationErrors: true,
74-
security: 'is_granted("OIDC_ADMIN")'
72+
security: 'is_granted("OIDC_ADMIN")',
73+
mercure: [
74+
'topics' => [
75+
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/admin/books/{id}{._format}"))',
76+
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/books/{id}{._format}"))',
77+
],
78+
]
7579
)]
7680
#[ApiResource(
7781
types: ['https://schema.org/Book', 'https://schema.org/Offer'],

api/src/Entity/Review.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use ApiPlatform\Metadata\Patch;
1515
use ApiPlatform\Metadata\Post;
1616
use ApiPlatform\Metadata\Put;
17+
use ApiPlatform\Metadata\UrlGeneratorInterface;
1718
use ApiPlatform\State\CreateProvider;
1819
use App\Repository\ReviewRepository;
1920
use App\Security\Voter\OidcTokenPermissionVoter;
@@ -56,12 +57,10 @@
5657
// https://github.com/api-platform/admin/issues/370
5758
new Put(
5859
uriTemplate: '/admin/reviews/{id}{._format}',
59-
// Mercure publish is done manually in MercureProcessor through ReviewPersistProcessor
6060
processor: ReviewPersistProcessor::class
6161
),
6262
new Delete(
6363
uriTemplate: '/admin/reviews/{id}{._format}',
64-
// Mercure publish is done manually in MercureProcessor through ReviewRemoveProcessor
6564
processor: ReviewRemoveProcessor::class
6665
),
6766
],
@@ -77,7 +76,13 @@
7776
AbstractNormalizer::GROUPS => ['Review:write', 'Review:write:admin'],
7877
],
7978
collectDenormalizationErrors: true,
80-
security: 'is_granted("OIDC_ADMIN")'
79+
security: 'is_granted("OIDC_ADMIN")',
80+
mercure: [
81+
'topics' => [
82+
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/admin/reviews/{id}{._format}"))',
83+
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/books/{bookId}/reviews/{id}{._format}"))',
84+
],
85+
]
8186
)]
8287
#[ApiResource(
8388
types: ['https://schema.org/Review'],
@@ -100,7 +105,6 @@
100105
),
101106
new Post(
102107
security: 'is_granted("OIDC_USER")',
103-
// Mercure publish is done manually in MercureProcessor through ReviewPersistProcessor
104108
processor: ReviewPersistProcessor::class,
105109
provider: CreateProvider::class,
106110
itemUriTemplate: '/books/{bookId}/reviews/{id}{._format}',
@@ -114,7 +118,6 @@
114118
],
115119
/** @see OidcTokenPermissionVoter */
116120
security: 'is_granted("OIDC_USER", request.getRequestUri())',
117-
// Mercure publish is done manually in MercureProcessor through ReviewPersistProcessor
118121
processor: ReviewPersistProcessor::class
119122
),
120123
new Delete(
@@ -125,7 +128,6 @@
125128
],
126129
/** @see OidcTokenPermissionVoter */
127130
security: 'is_granted("OIDC_USER", request.getRequestUri())',
128-
// Mercure publish is done manually in MercureProcessor through ReviewRemoveProcessor
129131
processor: ReviewRemoveProcessor::class
130132
),
131133
],

api/src/State/Processor/BookPersistProcessor.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020
{
2121
/**
2222
* @param PersistProcessor $persistProcessor
23-
* @param MercureProcessor $mercureProcessor
2423
*/
2524
public function __construct(
2625
#[Autowire(service: PersistProcessor::class)]
2726
private ProcessorInterface $persistProcessor,
28-
#[Autowire(service: MercureProcessor::class)]
29-
private ProcessorInterface $mercureProcessor,
3027
private HttpClientInterface $client,
3128
private DecoderInterface $decoder
3229
) {
@@ -49,21 +46,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
4946
}
5047

5148
// save entity
52-
$data = $this->persistProcessor->process($data, $operation, $uriVariables, $context);
53-
54-
// publish on Mercure
55-
foreach (['/admin/books/{id}{._format}', '/books/{id}{._format}'] as $uriTemplate) {
56-
$this->mercureProcessor->process(
57-
$data,
58-
$operation,
59-
$uriVariables,
60-
$context + [
61-
'item_uri_template' => $uriTemplate,
62-
]
63-
);
64-
}
65-
66-
return $data;
49+
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
6750
}
6851

6952
private function getData(string $uri): array

api/src/State/Processor/BookRemoveProcessor.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
namespace App\State\Processor;
66

77
use ApiPlatform\Doctrine\Common\State\RemoveProcessor;
8-
use ApiPlatform\Metadata\IriConverterInterface;
98
use ApiPlatform\Metadata\Operation;
10-
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
11-
use ApiPlatform\Metadata\UrlGeneratorInterface;
129
use ApiPlatform\State\ProcessorInterface;
1310
use App\Entity\Book;
1411
use Symfony\Component\DependencyInjection\Attribute\Autowire;
@@ -19,16 +16,11 @@
1916
final readonly class BookRemoveProcessor implements ProcessorInterface
2017
{
2118
/**
22-
* @param RemoveProcessor $removeProcessor
23-
* @param MercureProcessor $mercureProcessor
19+
* @param RemoveProcessor $removeProcessor
2420
*/
2521
public function __construct(
2622
#[Autowire(service: RemoveProcessor::class)]
2723
private ProcessorInterface $removeProcessor,
28-
#[Autowire(service: MercureProcessor::class)]
29-
private ProcessorInterface $mercureProcessor,
30-
private ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
31-
private IriConverterInterface $iriConverter
3224
) {
3325
}
3426

@@ -37,27 +29,7 @@ public function __construct(
3729
*/
3830
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): void
3931
{
40-
$object = clone $data;
41-
4232
// remove entity
4333
$this->removeProcessor->process($data, $operation, $uriVariables, $context);
44-
45-
// publish on Mercure
46-
foreach (['/admin/books/{id}{._format}', '/books/{id}{._format}'] as $uriTemplate) {
47-
$iri = $this->iriConverter->getIriFromResource(
48-
$object,
49-
UrlGeneratorInterface::ABS_URL,
50-
$this->resourceMetadataCollectionFactory->create(Book::class)->getOperation($uriTemplate)
51-
);
52-
$this->mercureProcessor->process(
53-
$object,
54-
$operation,
55-
$uriVariables,
56-
$context + [
57-
'item_uri_template' => $uriTemplate,
58-
MercureProcessor::DATA => json_encode(['@id' => $iri]),
59-
]
60-
);
61-
}
6234
}
6335
}

api/src/State/Processor/MercureProcessor.php

Lines changed: 0 additions & 73 deletions
This file was deleted.

api/src/State/Processor/ReviewPersistProcessor.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@
2121
{
2222
/**
2323
* @param PersistProcessor $persistProcessor
24-
* @param MercureProcessor $mercureProcessor
2524
*/
2625
public function __construct(
2726
#[Autowire(service: PersistProcessor::class)]
2827
private ProcessorInterface $persistProcessor,
29-
#[Autowire(service: MercureProcessor::class)]
30-
private ProcessorInterface $mercureProcessor,
3128
private Security $security,
3229
private ClockInterface $clock,
3330
private ResourceHandlerInterface $resourceHandler,
@@ -65,18 +62,6 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
6562
}
6663
}
6764

68-
// publish on Mercure
69-
foreach (['/admin/reviews/{id}{._format}', '/books/{bookId}/reviews/{id}{._format}'] as $uriTemplate) {
70-
$this->mercureProcessor->process(
71-
$data,
72-
$operation,
73-
$uriVariables,
74-
$context + [
75-
'item_uri_template' => $uriTemplate,
76-
]
77-
);
78-
}
79-
8065
return $data;
8166
}
8267
}

api/src/State/Processor/ReviewRemoveProcessor.php

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
namespace App\State\Processor;
66

77
use ApiPlatform\Doctrine\Common\State\RemoveProcessor;
8-
use ApiPlatform\Metadata\IriConverterInterface;
98
use ApiPlatform\Metadata\Operation;
10-
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
11-
use ApiPlatform\Metadata\UrlGeneratorInterface;
129
use ApiPlatform\State\ProcessorInterface;
1310
use App\Entity\Review;
1411
use App\Security\Http\Protection\ResourceHandlerInterface;
@@ -20,16 +17,11 @@
2017
final readonly class ReviewRemoveProcessor implements ProcessorInterface
2118
{
2219
/**
23-
* @param RemoveProcessor $removeProcessor
24-
* @param MercureProcessor $mercureProcessor
20+
* @param RemoveProcessor $removeProcessor
2521
*/
2622
public function __construct(
2723
#[Autowire(service: RemoveProcessor::class)]
2824
private ProcessorInterface $removeProcessor,
29-
#[Autowire(service: MercureProcessor::class)]
30-
private ProcessorInterface $mercureProcessor,
31-
private ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
32-
private IriConverterInterface $iriConverter,
3325
private ResourceHandlerInterface $resourceHandler,
3426
) {
3527
}
@@ -50,23 +42,5 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
5042
'operation_name' => '/books/{bookId}/reviews/{id}{._format}',
5143
]);
5244
}
53-
54-
// publish on Mercure
55-
foreach (['/admin/reviews/{id}{._format}', '/books/{bookId}/reviews/{id}{._format}'] as $uriTemplate) {
56-
$iri = $this->iriConverter->getIriFromResource(
57-
$object,
58-
UrlGeneratorInterface::ABS_URL,
59-
$this->resourceMetadataCollectionFactory->create(Review::class)->getOperation($uriTemplate)
60-
);
61-
$this->mercureProcessor->process(
62-
$object,
63-
$operation,
64-
$uriVariables,
65-
$context + [
66-
'item_uri_template' => $uriTemplate,
67-
MercureProcessor::DATA => json_encode(['@id' => $iri]),
68-
]
69-
);
70-
}
7145
}
7246
}

0 commit comments

Comments
 (0)