Part-DB-server/src/State/Mcp/GetPartByIdProcessor.php

60 lines
2.2 KiB
PHP

<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\State\Mcp;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\Parts\Part;
use App\Mcp\DTO\ElementByIdInput;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class GetPartByIdProcessor implements ProcessorInterface
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly AuthorizationCheckerInterface $authorizationChecker,
) {
}
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
{
if (!$data instanceof ElementByIdInput) {
throw new \InvalidArgumentException('Expected PartByIdInput');
}
$part = $this->entityManager->find(Part::class, $data->id);
if (!$part instanceof Part) {
throw new NotFoundHttpException(sprintf('Part with id %d not found', $data->id));
}
if (!$this->authorizationChecker->isGranted('read', $part)) {
throw new AccessDeniedException(sprintf('Access denied to part with id %d', $data->id));
}
return $part;
}
}