Skip to content

Commit

Permalink
feat: add exception handling for the FlashBagException from araise/co…
Browse files Browse the repository at this point in the history
…re-bundle
  • Loading branch information
Ruesa18 committed Jul 1, 2024
1 parent 7727b00 commit f7f4bb4
Showing 1 changed file with 57 additions and 37 deletions.
94 changes: 57 additions & 37 deletions src/Controller/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace araise\CrudBundle\Controller;

use araise\CoreBundle\Exception\FlashBagExecption;
use araise\CrudBundle\Block\Block;
use araise\CrudBundle\Content\RelationContent;
use araise\CrudBundle\Definition\DefinitionInterface;
Expand Down Expand Up @@ -171,14 +172,18 @@ public function editAction(Request $request): Response

$form = $view->getEditForm();

$form->handleRequest($request);
try {
$form->handleRequest($request);

if ($form->isSubmitted()) {
$this->dispatchEvent(CrudEvent::PRE_VALIDATE_PREFIX, $entity);
if ($form->isValid()) {
return $this->formSubmittedAndValid($entity, $mode, Page::EDIT);
if ($form->isSubmitted()) {
$this->dispatchEvent(CrudEvent::PRE_VALIDATE_PREFIX, $entity);
if ($form->isValid()) {
return $this->formSubmittedAndValid($entity, $mode, Page::EDIT);
}
throw new FlashBagExecption('error', 'araise_crud.save_error');
}
$this->addFlash('error', 'araise_crud.save_error');
} catch (FlashBagExecption $e) {
$this->addFlash($e->getFlashType(), $e->getFlashMessage());
}

$this->definition->buildBreadcrumbs($entity, Page::EDIT);
Expand Down Expand Up @@ -221,14 +226,18 @@ public function createAction(Request $request): Response

$form = $view->getCreateForm();

$form->handleRequest($request);
try {
$form->handleRequest($request);

if ($form->isSubmitted()) {
$this->dispatchEvent(CrudEvent::PRE_VALIDATE_PREFIX, $entity);
if ($form->isValid()) {
return $this->formSubmittedAndValid($entity, $mode, Page::CREATE);
if ($form->isSubmitted()) {
$this->dispatchEvent(CrudEvent::PRE_VALIDATE_PREFIX, $entity);
if ($form->isValid()) {
return $this->formSubmittedAndValid($entity, $mode, Page::CREATE);
}
throw new FlashBagExecption('error', 'araise_crud.save_error');
}
$this->addFlash('error', 'araise_crud.save_error');
} catch (FlashBagExecption $e) {
$this->addFlash($e->getFlashType(), $e->getFlashMessage());
}

$this->definition->buildBreadcrumbs($entity, Page::CREATE);
Expand Down Expand Up @@ -259,15 +268,15 @@ public function deleteAction(Request $request): Response
try {
$this->entityManager->remove($entity);
$this->dispatchEvent(CrudEvent::PRE_DELETE_PREFIX, $entity);
$this->entityManager->flush();
try {
$this->entityManager->flush();
} catch (\Exception) {
throw new FlashBagExecption('error', 'araise_crud.delete_error');
}
$this->dispatchEvent(CrudEvent::POST_DELETE_PREFIX, $entity);
$this->addFlash('success', 'araise_crud.delete_success');
} catch (\Exception $e) {
$this->addFlash('error', 'araise_crud.delete_error');
$this->container->get(LoggerInterface::class)->warning('Error while deleting: '.$e->getMessage(), [
'entity' => get_class($entity),
'id' => $entity->getId(),
]);
} catch (FlashBagExecption $e) {
$this->addFlash($e->getFlashType(), $e->getFlashMessage());
}

return $this->getDefinition()->getRedirect(Page::DELETE, $entity);
Expand Down Expand Up @@ -329,26 +338,37 @@ public function exportAction(Request $request, TableFactory $tableFactory, Table
if ($request->query->getInt('all', 0) === 1) {
$table->getExtension(PaginationExtension::class)?->setLimit(0);
}
$exporter = $table->getExporter($request->query->getString('exporter', 'table'));
if (!$exporter && count($table->getExporters()) > 0) {
$exporter = $table->getExporter(key($table->getExporters()));
}
if (!$exporter instanceof ExporterInterface) {
$this->addFlash('error', 'araise_crud.export_error');
throw new \RuntimeException('No Exporter found.');
}
$spreadsheet = $exporter->createSpreadsheet($table);
$writer = new Xlsx($spreadsheet);
$response = new StreamedResponse();
$response->setCallback(
function () use ($writer) {
$writer->save('php://output');
}
);

$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$response->headers->set('Content-Disposition', 'attachment; filename="'.$this->definition->getExportFilename().'"');
try {
$exporter = $table->getExporter($request->query->getString('exporter', 'table'));
if (!$exporter && count($table->getExporters()) > 0) {
$exporter = $table->getExporter(key($table->getExporters()));
}
if (!$exporter instanceof ExporterInterface) {
throw new FlashBagExecption('error', 'araise_crud.export_error', 'No Exporter found.');
}
$spreadsheet = $exporter->createSpreadsheet($table);
$writer = new Xlsx($spreadsheet);
$response = new StreamedResponse();
$response->setCallback(
function () use ($writer) {
$writer->save('php://output');
}
);

$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$response->headers->set('Content-Disposition', 'attachment; filename="'.$this->definition->getExportFilename().'"');
} catch (FlashBagExecption $e) {
$this->addFlash($e->getFlashType(), $e->getFlashMessage());

if (isset($entity) && $entity) {
$response = $this->redirectToRoute($this->definition::getRoute(Page::SHOW), [
'id' => $entity->getId(),
]);
} else {
$response = $this->redirectToRoute($this->definition::getRoute(Page::INDEX));
}
}
return $response;
}

Expand Down

0 comments on commit f7f4bb4

Please sign in to comment.