-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
BookPersistProcessor.php
48 lines (40 loc) · 1.33 KB
/
BookPersistProcessor.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
<?php
declare(strict_types=1);
namespace App\State\Processor;
use ApiPlatform\Doctrine\Common\State\PersistProcessor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\BookRepository\BookRepositoryInterface;
use App\Entity\Book;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* @implements ProcessorInterface<Book, Book>
*/
final readonly class BookPersistProcessor implements ProcessorInterface
{
/**
* @param PersistProcessor $persistProcessor
*/
public function __construct(
#[Autowire(service: PersistProcessor::class)]
private ProcessorInterface $persistProcessor,
private BookRepositoryInterface $bookRepository,
) {
}
/**
* @param Book $data
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Book
{
$book = $this->bookRepository->find($data->book);
// this should never happen
if (!$book instanceof Book) {
throw new NotFoundHttpException();
}
$data->title = $book->title;
$data->author = $book->author;
// save entity
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
}
}