/
BookPersistProcessor.php
77 lines (67 loc) · 2.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?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\Entity\Book;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* @implements ProcessorInterface<Book, Book>
*/
final readonly class BookPersistProcessor implements ProcessorInterface
{
/**
* @param PersistProcessor $persistProcessor
* @param MercureProcessor $mercureProcessor
*/
public function __construct(
#[Autowire(service: PersistProcessor::class)]
private ProcessorInterface $persistProcessor,
#[Autowire(service: MercureProcessor::class)]
private ProcessorInterface $mercureProcessor,
private HttpClientInterface $client,
private DecoderInterface $decoder
) {
}
/**
* @param Book $data
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Book
{
$book = $this->getData($data->book);
$data->title = $book['title'];
$data->author = null;
if (isset($book['authors'][0]['key'])) {
$author = $this->getData('https://openlibrary.org' . $book['authors'][0]['key'] . '.json');
if (isset($author['name'])) {
$data->author = $author['name'];
}
}
// save entity
$data = $this->persistProcessor->process($data, $operation, $uriVariables, $context);
// publish on Mercure
foreach (['/admin/books/{id}{._format}', '/books/{id}{._format}'] as $uriTemplate) {
$this->mercureProcessor->process(
$data,
$operation,
$uriVariables,
$context + [
'item_uri_template' => $uriTemplate,
]
);
}
return $data;
}
private function getData(string $uri): array
{
return $this->decoder->decode($this->client->request(Request::METHOD_GET, $uri, [
'headers' => [
'Accept' => 'application/json',
],
])->getContent(), 'json');
}
}