-
-
Notifications
You must be signed in to change notification settings - Fork 192
/
BookRemoveProcessor.php
63 lines (56 loc) · 2.08 KB
/
BookRemoveProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
namespace App\State\Processor;
use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\Doctrine\Common\State\RemoveProcessor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\Book;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
/**
* @implements ProcessorInterface<Book, void>
*/
final readonly class BookRemoveProcessor implements ProcessorInterface
{
/**
* @param RemoveProcessor $removeProcessor
* @param MercureProcessor $mercureProcessor
*/
public function __construct(
#[Autowire(service: RemoveProcessor::class)]
private ProcessorInterface $removeProcessor,
#[Autowire(service: MercureProcessor::class)]
private ProcessorInterface $mercureProcessor,
private ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
private IriConverterInterface $iriConverter
) {
}
/**
* @param Book $data
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): void
{
$object = clone $data;
// remove entity
$this->removeProcessor->process($data, $operation, $uriVariables, $context);
// publish on Mercure
foreach (['/admin/books/{id}{._format}', '/books/{id}{._format}'] as $uriTemplate) {
$iri = $this->iriConverter->getIriFromResource(
$object,
UrlGeneratorInterface::ABS_URL,
$this->resourceMetadataCollectionFactory->create(Book::class)->getOperation($uriTemplate)
);
$this->mercureProcessor->process(
$object,
$operation,
$uriVariables,
$context + [
'item_uri_template' => $uriTemplate,
MercureProcessor::DATA => json_encode(['@id' => $iri]),
]
);
}
}
}