-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathReviewPersistProcessor.php
More file actions
59 lines (51 loc) · 1.93 KB
/
ReviewPersistProcessor.php
File metadata and controls
59 lines (51 loc) · 1.93 KB
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
<?php
declare(strict_types=1);
namespace App\State\Processor;
use ApiPlatform\Doctrine\Common\State\PersistProcessor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Post;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\Review;
use App\Security\Http\Protection\ResourceHandlerInterface;
use Psr\Clock\ClockInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
/**
* @implements ProcessorInterface<Review, Review>
*/
final readonly class ReviewPersistProcessor implements ProcessorInterface
{
/**
* @param PersistProcessor $persistProcessor
*/
public function __construct(
#[Autowire(service: PersistProcessor::class)]
private ProcessorInterface $persistProcessor,
private Security $security,
private ClockInterface $clock,
private ResourceHandlerInterface $resourceHandler,
) {
}
/**
* @param Review $data
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Review
{
// prevent overriding user, for instance from admin
if ($operation instanceof Post) {
/** @phpstan-ignore-next-line */
$data->user = $this->security->getUser();
$data->publishedAt = $this->clock->now();
}
// save entity
$data = $this->persistProcessor->process($data, $operation, $uriVariables, $context);
// create resource on OIDC server
// project specification: only create resource on OIDC server for known users (john.doe and chuck.norris)
if ($operation instanceof Post && \in_array($data->user->email, ['john.doe@example.com', 'chuck.norris@example.com'], true)) {
$this->resourceHandler->create($data, $data->user, [
'operation_name' => '/books/{bookId}/reviews/{id}{._format}',
]);
}
return $data;
}
}