45 lines
1.2 KiB
PHP
45 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Element\UseCases\GetElement;
|
|
|
|
use App\Element\ElementRepository;
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Exceptions\NotFoundException;
|
|
|
|
class GetElement
|
|
{
|
|
public function __construct(private ElementRepository $elementRepository)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @throws BadRequestException
|
|
* @throws NotFoundException
|
|
*/
|
|
public function execute(GetElementRequest $request): GetElementResult
|
|
{
|
|
if ($request->id === null) {
|
|
throw new BadRequestException('id is required');
|
|
}
|
|
|
|
$element = $this->elementRepository->find($request->id);
|
|
if ($element === null) {
|
|
throw new NotFoundException('Element not found');
|
|
}
|
|
|
|
$parentElement = $element->getParentElement();
|
|
if ($parentElement === null) {
|
|
$siblingElements = [$element];
|
|
} else {
|
|
$siblingElements = $this->elementRepository
|
|
->findByParentElement($parentElement);
|
|
}
|
|
|
|
return new GetElementResult(
|
|
element: $element,
|
|
childElements: $this->elementRepository
|
|
->findByParentElement($element),
|
|
siblingElements: $siblingElements,
|
|
);
|
|
}
|
|
}
|