Add custom project api resource state processor
This commit is contained in:
@@ -6,9 +6,11 @@ use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
|
||||
use ApiPlatform\Doctrine\Orm\State\Options;
|
||||
use ApiPlatform\Metadata as API;
|
||||
use App\ApiResource\Accounting\AccountingApiResource;
|
||||
use App\ApiResource\User\UserApiResource;
|
||||
use App\Entity\Project\Project;
|
||||
use App\Entity\Project\ProjectStatus;
|
||||
use App\State\ApiResourceStateProvider;
|
||||
use App\State\Project\ProjectStateProcessor;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
/**
|
||||
@@ -18,6 +20,7 @@ use Symfony\Component\Validator\Constraints as Assert;
|
||||
shortName: 'Project',
|
||||
stateOptions: new Options(entityClass: Project::class),
|
||||
provider: ApiResourceStateProvider::class,
|
||||
processor: ProjectStateProcessor::class
|
||||
)]
|
||||
#[API\GetCollection()]
|
||||
#[API\Post(security: 'is_granted("ROLE_USER")')]
|
||||
@@ -35,11 +38,16 @@ class ProjectApiResource
|
||||
#[API\ApiProperty(writable: false)]
|
||||
public AccountingApiResource $accounting;
|
||||
|
||||
/**
|
||||
* The User who owns this Project.
|
||||
*/
|
||||
#[API\ApiProperty(writable: false)]
|
||||
public UserApiResource $owner;
|
||||
|
||||
/**
|
||||
* Main title for the Project.
|
||||
*/
|
||||
#[API\ApiFilter(filterClass: SearchFilter::class, strategy: 'partial')]
|
||||
#[API\ApiProperty(securityPostDenormalize: 'is_granted("PROJECT_EDIT")')]
|
||||
#[Assert\NotBlank()]
|
||||
public string $title;
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\State\Project;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\ApiResource\Project\ProjectApiResource;
|
||||
use App\ApiResource\User\UserApiResource;
|
||||
use App\Entity\Project\Project;
|
||||
use App\Mapping\AutoMapper;
|
||||
use App\Repository\User\UserRepository;
|
||||
use App\State\ApiResourceStateProcessor;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
class ProjectStateProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private Security $security,
|
||||
private UserRepository $userRepository,
|
||||
private AutoMapper $autoMapper,
|
||||
private ApiResourceStateProcessor $stateProcessor
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param ProjectApiResource $data
|
||||
*
|
||||
* @return Project|null
|
||||
*/
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []) {
|
||||
if (!isset($data->id)) {
|
||||
$user = $this->security->getUser();
|
||||
$owner = $this->userRepository->findOneByIdentifier($user->getUserIdentifier());
|
||||
|
||||
$data->owner = $this->autoMapper->map($owner, UserApiResource::class);
|
||||
}
|
||||
|
||||
return $this->stateProcessor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user