src/Rest/Traits/Methods/FindOneMethod.php
<?php
declare(strict_types = 1);
/**
* /src/Rest/Traits/Methods/FindOneMethod.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 FindOneMethod
{
/**
* Generic 'findOneMethod' method for REST resources.
*
* @param array<int, string>|null $allowedHttpMethods
*
* @throws Throwable
*/
public function findOneMethod(Request $request, string $id, ?array $allowedHttpMethods = null): Response
{
$resource = $this->getResourceForMethod($request, $allowedHttpMethods ?? [Request::METHOD_GET]);
try {
// Fetch data from database
return $this
->getResponseHandler()
->createResponse($request, $resource->findOne($id, true), $resource);
} catch (Throwable $exception) {
throw $this->handleRestMethodException($exception, $id);
}
}
}