Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
53.85% |
7 / 13 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| BackupDb | |
53.85% |
7 / 13 |
|
50.00% |
1 / 2 |
3.88 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
50.00% |
6 / 12 |
|
0.00% |
0 / 1 |
2.50 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Controller\Admin; |
| 6 | |
| 7 | use App\Service\BackupManager; |
| 8 | use App\Service\EmailHelper; |
| 9 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 10 | use Symfony\Component\Clock\ClockInterface; |
| 11 | use Symfony\Component\HttpFoundation\Response; |
| 12 | use Symfony\Component\Mailer\MailerInterface; |
| 13 | use Symfony\Component\Process\Exception\ProcessFailedException; |
| 14 | use Symfony\Component\Routing\Attribute\Route; |
| 15 | use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 16 | |
| 17 | class BackupDb extends AbstractController |
| 18 | { |
| 19 | public function __construct( |
| 20 | private readonly BackupManager $backupManager, |
| 21 | private readonly EmailHelper $emailHelper, |
| 22 | private readonly MailerInterface $mailer, |
| 23 | private readonly ClockInterface $clock, |
| 24 | ) {} |
| 25 | |
| 26 | #[IsGranted('ROLE_ADMIN')] |
| 27 | #[Route(path: '/admin/backup-db', name: 'admin_backup_db', methods: ['GET'])] |
| 28 | public function index(): Response |
| 29 | { |
| 30 | $date = $this->clock->now()->format('Y-m-d_H-i-s'); |
| 31 | $backupFile = sys_get_temp_dir().sprintf('/backup_%s.sql', $date); |
| 32 | |
| 33 | try { |
| 34 | $this->backupManager->runBackup($backupFile); |
| 35 | |
| 36 | $email = $this->emailHelper |
| 37 | ->createAdminEmail('Backup: '.$date) |
| 38 | ->text('Backup: '.$date) |
| 39 | ->attachFromPath($backupFile); |
| 40 | |
| 41 | $this->mailer->send($email); |
| 42 | $this->addFlash('success', 'Database backup has been sent to your email.'); |
| 43 | } catch (ProcessFailedException) { |
| 44 | $this->addFlash('error', 'Database backup failed'); |
| 45 | } |
| 46 | |
| 47 | return $this->render('admin/tasks.html.twig'); |
| 48 | } |
| 49 | } |