Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add exception handling for the FlashBagException from araise/core #64

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
"lasserafn/php-initial-avatar-generator": "^4.3"
},
"require-dev": {
"araise/core-bundle": "dev-develop as 1.1",
"araise/table-bundle": "dev-develop as 1.1",
"araise/search-bundle": "dev-develop as 3.1",
"symfony/phpunit-bridge": "^6.4|^7.0",
"symfony/config": "^6.4|^7.0",
"symfony/dependency-injection": "^6.4|^7.0",
Expand Down
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
Loading