Skip to content

Commit

Permalink
Refacto BackupController based on PrestaShopAdminController
Browse files Browse the repository at this point in the history
  • Loading branch information
jolelievre committed Oct 17, 2024
1 parent 1acb06b commit 8d21659
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 99 deletions.
2 changes: 1 addition & 1 deletion src/Adapter/Backup/BackupRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class BackupRemover implements BackupRemoverInterface
/**
* {@inheritdoc}
*/
public function remove(BackupInterface $backup)
public function remove(BackupInterface $backup): bool
{
$legacyBackup = new PrestaShopBackup($backup->getFileName());

Expand Down
3 changes: 2 additions & 1 deletion src/Adapter/Backup/DatabaseBackupCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace PrestaShop\PrestaShop\Adapter\Backup;

use PrestaShop\PrestaShop\Adapter\Entity\PrestaShopBackup;
use PrestaShop\PrestaShop\Core\Backup\BackupInterface;
use PrestaShop\PrestaShop\Core\Backup\Exception\BackupException;
use PrestaShop\PrestaShop\Core\Backup\Exception\DirectoryIsNotWritableException;
use PrestaShop\PrestaShop\Core\Backup\Manager\BackupCreatorInterface;
Expand All @@ -41,7 +42,7 @@ final class DatabaseBackupCreator implements BackupCreatorInterface
/**
* {@inheritdoc}
*/
public function createBackup()
public function createBackup(): BackupInterface
{
ini_set('max_execution_time', '0');

Expand Down
2 changes: 1 addition & 1 deletion src/Core/Backup/Manager/BackupCreatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ interface BackupCreatorInterface
*
* @return BackupInterface
*/
public function createBackup();
public function createBackup(): BackupInterface;
}
2 changes: 1 addition & 1 deletion src/Core/Backup/Manager/BackupRemoverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ interface BackupRemoverInterface
*
* @return bool True if backup were removed successfully or False otherwise
*/
public function remove(BackupInterface $backup);
public function remove(BackupInterface $backup): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,37 @@
namespace PrestaShopBundle\Controller\Admin\Configure\AdvancedParameters;

use PrestaShop\PrestaShop\Adapter\Backup\Backup;
use PrestaShop\PrestaShop\Core\Backup\BackupInterface;
use PrestaShop\PrestaShop\Core\Backup\Exception\BackupException;
use PrestaShop\PrestaShop\Core\Backup\Exception\DirectoryIsNotWritableException;
use PrestaShop\PrestaShop\Core\Backup\Manager\BackupCreatorInterface;
use PrestaShop\PrestaShop\Core\Backup\Manager\BackupRemoverInterface;
use PrestaShop\PrestaShop\Core\Form\FormHandlerInterface;
use PrestaShop\PrestaShop\Core\Grid\GridFactoryInterface;
use PrestaShop\PrestaShop\Core\Search\Filters\BackupFilters;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Controller\Admin\PrestaShopAdminController;
use PrestaShopBundle\Security\Attribute\AdminSecurity;
use PrestaShopBundle\Security\Attribute\DemoRestricted;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Class BackupController is responsible of "Configure > Advanced Parameters > Database > Backup" page.
* Class BackupController is responsible for "Configure > Advanced Parameters > Database > Backup" page.
*/
class BackupController extends FrameworkBundleAdminController
class BackupController extends PrestaShopAdminController
{
/**
* Show backup page.
*
* @param Request $request
* @param BackupFilters $filters
*
* @return Response
*/
#[AdminSecurity("is_granted('read', request.get('_legacy_controller'))", message: 'You do not have permission to update this.', redirectRoute: 'admin_products_index')]
public function indexAction(Request $request, BackupFilters $filters)
{
$backupForm = $this->getBackupFormHandler()->getForm();
public function indexAction(
Request $request,
BackupFilters $filters,
#[Autowire(service: 'prestashop.core.grid.factory.backup')]
GridFactoryInterface $backupsGridFactory,
#[Autowire(service: 'prestashop.admin.backup.form_handler')]
FormHandlerInterface $backupFormHandler,
): Response {
$backupForm = $backupFormHandler->getForm();
$configuration = $this->getConfiguration();

$hasDownloadFile = false;
Expand All @@ -72,7 +72,6 @@ public function indexAction(Request $request, BackupFilters $filters)
];
}

$backupsGridFactory = $this->get('prestashop.core.grid.factory.backup');
$backupGrid = $backupsGridFactory->getGrid($filters);

return $this->render('@PrestaShop/Admin/Configure/AdvancedParameters/Backup/index.html.twig', [
Expand All @@ -85,23 +84,16 @@ public function indexAction(Request $request, BackupFilters $filters)
'help_link' => $this->generateSidebarLink($request->attributes->get('_legacy_controller')),
'multistoreInfoTip' => $this->trans(
'Note that this feature is only available in the "all stores" context. It will be added to all your stores.',
[],
'Admin.Notifications.Info'
),
'multistoreIsUsed' => $this->get('prestashop.adapter.multistore_feature')->isUsed(),
'multistoreIsUsed' => $this->getShopContext()->isMultiShopUsed(),
]);
}

/**
* Show file download view.
*
* @param Request $request
* @param string $downloadFileName
*
* @return Response
*/
#[DemoRestricted(redirectRoute: 'admin_backups_index')]
#[AdminSecurity("is_granted('read', request.get('_legacy_controller'))")]
public function downloadViewAction(Request $request, $downloadFileName)
public function downloadViewAction(Request $request, string $downloadFileName): Response
{
$backup = new Backup($downloadFileName);

Expand All @@ -110,74 +102,57 @@ public function downloadViewAction(Request $request, $downloadFileName)
'url' => $backup->getUrl(),
'size' => $backup->getSize(),
],
'layoutTitle' => $this->trans('Downloading backup %s', 'Admin.Navigation.Menu', [$downloadFileName]),
'layoutTitle' => $this->trans('Downloading backup %s', [$downloadFileName], 'Admin.Navigation.Menu'),
'enableSidebar' => true,
'help_link' => $this->generateSidebarLink($request->attributes->get('_legacy_controller')),
]);
}

/**
* Return a backup content as a download.
*
* @param string $downloadFileName
*
* @return BinaryFileResponse
*/
#[DemoRestricted(redirectRoute: 'admin_backup')]
#[AdminSecurity("is_granted('read', request.get('_legacy_controller'))")]
public function downloadContentAction($downloadFileName)
public function downloadContentAction(string $downloadFileName): BinaryFileResponse
{
$backup = new Backup($downloadFileName);

return new BinaryFileResponse($backup->getFilePath());
}

/**
* Process backup options saving.
*
* @param Request $request
*
* @return RedirectResponse
*/
#[DemoRestricted(redirectRoute: 'admin_backups_index')]
#[AdminSecurity("is_granted('update', request.get('_legacy_controller')) && is_granted('create', request.get('_legacy_controller')) && is_granted('delete', request.get('_legacy_controller'))", message: 'You do not have permission to update this.', redirectRoute: 'admin_backups_index')]
public function saveOptionsAction(Request $request)
{
$backupFormHandler = $this->getBackupFormHandler();

public function saveOptionsAction(
Request $request,
#[Autowire(service: 'prestashop.admin.backup.form_handler')]
FormHandlerInterface $backupFormHandler,
): RedirectResponse {
$backupForm = $backupFormHandler->getForm();
$backupForm->handleRequest($request);

if ($backupForm->isSubmitted()) {
$errors = $backupFormHandler->save($backupForm->getData());

if (!empty($errors)) {
$this->flashErrors($errors);
$this->addFlashErrors($errors);
} else {
$this->addFlash('success', $this->trans('Update successful', 'Admin.Notifications.Success'));
$this->addFlash('success', $this->trans('Update successful', [], 'Admin.Notifications.Success'));
}
}

return $this->redirectToRoute('admin_backups_index');
}

/**
* Create new backup.
*
* @return RedirectResponse
*/
#[DemoRestricted(redirectRoute: 'admin_backups_index')]
#[AdminSecurity("is_granted('create', request.get('_legacy_controller'))", message: 'You do not have permission to create this.', redirectRoute: 'admin_backups_index')]
public function createAction()
{
public function createAction(
BackupCreatorInterface $backupCreator,
): RedirectResponse {
try {
$backupCreator = $this->get(BackupInterface::class);
$backup = $backupCreator->createBackup();

$this->addFlash(
'success',
$this->trans(
'It appears the backup was successful, however you must download and carefully verify the backup file before proceeding.',
[],
'Admin.Advparameters.Notification'
)
);
Expand All @@ -188,73 +163,61 @@ public function createAction()
'error',
$this->trans(
'The "Backups" directory located in the admin directory must be writable (CHMOD 755 / 777).',
[],
'Admin.Advparameters.Notification'
)
);
} catch (BackupException $e) {
$this->addFlash('error', $this->trans('The backup file does not exist', 'Admin.Advparameters.Notification'));
$this->addFlash('error', $this->trans('The backup file does not exist', [], 'Admin.Advparameters.Notification'));
}

return $this->redirectToRoute('admin_backups_index');
}

/**
* Process backup file deletion.
*
* @param string $deleteFileName
*
* @return RedirectResponse
*/
#[DemoRestricted(redirectRoute: 'admin_backups_index')]
#[AdminSecurity("is_granted('delete', request.get('_legacy_controller'))", message: 'You do not have permission to delete this.', redirectRoute: 'admin_backups_index')]
public function deleteAction($deleteFileName)
{
public function deleteAction(
string $deleteFileName,
BackupRemoverInterface $backupRemover,
): RedirectResponse {
$backup = new Backup($deleteFileName);
$backupRemover = $this->get(BackupRemoverInterface::class);

if (!$backupRemover->remove($backup)) {
$this->addFlash(
'error',
sprintf(
'%s "%s"',
$this->trans('Error deleting', 'Admin.Advparameters.Notification'),
$this->trans('Error deleting', [], 'Admin.Advparameters.Notification'),
$backup->getFileName()
)
);

return $this->redirectToRoute('admin_backups_index');
}

$this->addFlash('success', $this->trans('Successful deletion', 'Admin.Notifications.Success'));
$this->addFlash('success', $this->trans('Successful deletion', [], 'Admin.Notifications.Success'));

return $this->redirectToRoute('admin_backups_index');
}

/**
* Process bulk backup deletion.
*
* @param Request $request
*
* @return RedirectResponse
*/
#[DemoRestricted(redirectRoute: 'admin_backups_index')]
#[AdminSecurity("is_granted('delete', request.get('_legacy_controller'))", message: 'You do not have permission to delete this.', redirectRoute: 'admin_backups_index')]
public function bulkDeleteAction(Request $request)
{
public function bulkDeleteAction(
Request $request,
BackupRemoverInterface $backupRemover,
): RedirectResponse {
$backupsToDelete = $request->request->all('backup_backup_bulk_file_names');

if (empty($backupsToDelete)) {
$this->addFlash(
'error',
$this->trans('You must select at least one element to delete.', 'Admin.Notifications.Error')
$this->trans('You must select at least one element to delete.', [], 'Admin.Notifications.Error')
);

return $this->redirectToRoute('admin_backups_index');
}

$backupRemover = $this->get(BackupRemoverInterface::class);
$failedBackups = [];

foreach ($backupsToDelete as $backupFileName) {
$backup = new Backup($backupFileName);

Expand All @@ -266,13 +229,13 @@ public function bulkDeleteAction(Request $request)
if (!empty($failedBackups)) {
$this->addFlash(
'error',
$this->trans('An error occurred while deleting this selection.', 'Admin.Notifications.Error')
$this->trans('An error occurred while deleting this selection.', [], 'Admin.Notifications.Error')
);

foreach ($failedBackups as $backupFileName) {
$this->addFlash(
'error',
$this->trans('Can\'t delete #%id%', 'Admin.Notifications.Error', ['%id%' => $backupFileName])
$this->trans('Can\'t delete #%id%', ['%id%' => $backupFileName], 'Admin.Notifications.Error')
);
}

Expand All @@ -281,19 +244,9 @@ public function bulkDeleteAction(Request $request)

$this->addFlash(
'success',
$this->trans('The selection has been successfully deleted.', 'Admin.Notifications.Success')
$this->trans('The selection has been successfully deleted.', [], 'Admin.Notifications.Success')
);

return $this->redirectToRoute('admin_backups_index');
}

/**
* Get backup form handler.
*
* @return FormHandlerInterface
*/
protected function getBackupFormHandler(): FormHandlerInterface
{
return $this->get('prestashop.admin.backup.form_handler');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ services:
_defaults:
autowire: true

PrestaShop\PrestaShop\Core\Backup\BackupInterface:
PrestaShop\PrestaShop\Core\Backup\Manager\BackupCreatorInterface:
class: PrestaShop\PrestaShop\Adapter\Backup\DatabaseBackupCreator
public: true

Expand Down

0 comments on commit 8d21659

Please sign in to comment.