src/Rest/Traits/Methods/DeleteMethod.php
<?php
declare(strict_types = 1);
/**
* /src/Rest/Traits/Methods/DeleteMethod.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
namespace App\Rest\Traits\Methods;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Throwable;
/**
* @package App\Rest\Traits\Methods
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
*/
trait DeleteMethod
{
/**
* Generic 'deleteMethod' method for REST resources.
*
* @param array<int, string>|null $allowedHttpMethods
*
* @throws Throwable
*/
public function deleteMethod(Request $request, string $id, ?array $allowedHttpMethods = null): Response
{
$resource = $this->getResourceForMethod($request, $allowedHttpMethods ?? [Request::METHOD_DELETE]);
try {
// Fetch data from database
return $this
->getResponseHandler()
->createResponse($request, $resource->delete($id), $resource);
} catch (Throwable $exception) {
throw $this->handleRestMethodException($exception, $id);
}
}
}