33 lines
985 B
PHP
33 lines
985 B
PHP
<?php
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\State\ProcessorInterface;
|
|
use App\Entity\Thread;
|
|
use App\Entity\User;
|
|
use Symfony\Bundle\SecurityBundle\Security;
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
|
|
|
class ThreadOwnerProcessor implements ProcessorInterface
|
|
{
|
|
public function __construct(
|
|
private Security $security,
|
|
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
|
private ProcessorInterface $persistProcessor
|
|
) {
|
|
}
|
|
|
|
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
|
|
{
|
|
if ($data instanceof Thread && null === $data->getAuthor()) {
|
|
$user = $this->security->getUser();
|
|
if ($user instanceof User) {
|
|
$data->setAuthor($user);
|
|
}
|
|
}
|
|
|
|
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
|
}
|
|
}
|