Skip to content

Commit

Permalink
all classes, methods and code related to sending backups via email ha…
Browse files Browse the repository at this point in the history
…d been deprecated, and now they have been

  removed. So, the `BackupManager::send()` method (and, consequently, also the internal
  `BackupManager::getEmailInstance()` method), the `BackupExport::send()` method, the `SendCommand` class and the
  `send` option for the `ExportCommand` have been removed.
  • Loading branch information
mirko-pagliai committed Feb 13, 2025
2 parents d4c8347 + 1d3bfca commit 59093ac
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 408 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
# 2.x branch
## 2.14 branch
### 2.14.0
* all classes, methods and code related to sending backups via email had been deprecated, and now they have been
removed. So, the `BackupManager::send()` method (and, consequently, also the internal
`BackupManager::getEmailInstance()` method), the `BackupExport::send()` method, the `SendCommand` class and the
`send` option for the `ExportCommand` have been removed.

## 2.13 branch
### 2.13.4
* fixed [bug #119](https://github.com/mirko-pagliai/cakephp-database-backup/issues/119): `BackupManager` ignored the
timezone of backup files, and consequently also `IndexCommand`;
* fixed [bug #111](https://github.com/mirko-pagliai/cakephp-database-backup/issues/111): for Mysql it first looks for
`mariadb` and `mariadb-dump` executables, otherwise `mysql` and `mysqldump` executables;
* all classes, methods and code related to sending backups via email are now deprecated. So, the `BackupManager::send()`
method (and, consequently, also the internal `BackupManager::getEmailInstance()` method), the `BackupExport::send()`
method, the `SendCommand` class and the `send` option for the `ExportCommand` are deprecated. All of these will be
removed in a later release. No replacement is provided.
* setting the `DatabaseBackup.mailSender` value of the configuration is deprecated (bootstrap checks that the value
has not been set by the user);
* the `DeleteAllCommand` is deprecated. Will be removed in a future release;
* added tests for php 8.4;
* all chainable methods of `BackupExport` and `BackupImport` classes now have the typehint for returning self. Updated
descriptions;
Expand Down
64 changes: 0 additions & 64 deletions src/Command/DeleteAllCommand.php

This file was deleted.

29 changes: 9 additions & 20 deletions src/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
'will be deleted'),
'short' => 'r',
],
'send' => [
'help' => __d('database_backup', 'Sends the backup file via email. You have ' .
'to indicate the recipient\'s email address'),
'short' => 's',
],
'timeout' => [
'help' => __d(
'database_backup',
Expand All @@ -85,7 +80,7 @@ protected function getBackupExport(): BackupExport
/**
* Exports a database backup.
*
* This command uses `RotateCommand` and `SendCommand`.
* This command uses `RotateCommand`.
*
* @param \Cake\Console\Arguments $args The command arguments
* @param \Cake\Console\ConsoleIo $io The console io
Expand Down Expand Up @@ -119,21 +114,15 @@ public function execute(Arguments $args, ConsoleIo $io): void
}
$io->success(__d('database_backup', 'Backup `{0}` has been exported', rtr($file)));

//Sends via email and/or rotates. It keeps options `verbose` and `quiet`.
$extraOptions = [];
foreach (['verbose', 'quiet'] as $option) {
if ($args->getOption($option)) {
$extraOptions[] = '--' . $option;
}
}
if ($args->getOption('send')) {
$this->executeCommand(
SendCommand::class,
array_merge([$file, (string)$args->getOption('send')], $extraOptions),
$io
);
}
//Rotates. It keeps options `verbose` and `quiet`.
if ($args->getOption('rotate')) {
$extraOptions = [];
foreach (['verbose', 'quiet'] as $option) {
if ($args->getOption($option)) {
$extraOptions[] = '--' . $option;
}
}

$this->executeCommand(
RotateCommand::class,
array_merge([(string)$args->getOption('rotate')], $extraOptions),
Expand Down
73 changes: 0 additions & 73 deletions src/Command/SendCommand.php

This file was deleted.

7 changes: 2 additions & 5 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@

use Cake\Console\CommandCollection;
use Cake\Core\BasePlugin;
use DatabaseBackup\Command\DeleteAllCommand;
use DatabaseBackup\Command\ExportCommand;
use DatabaseBackup\Command\ImportCommand;
use DatabaseBackup\Command\IndexCommand;
use DatabaseBackup\Command\RotateCommand;
use DatabaseBackup\Command\SendCommand;

/**
* Plugin class.
Expand All @@ -38,11 +36,10 @@ class Plugin extends BasePlugin
*/
public function console(CommandCollection $commands): CommandCollection
{
return $commands->add('database_backup.delete_all', DeleteAllCommand::class)
return $commands
->add('database_backup.export', ExportCommand::class)
->add('database_backup.import', ImportCommand::class)
->add('database_backup.index', IndexCommand::class)
->add('database_backup.rotate', RotateCommand::class)
->add('database_backup.send', SendCommand::class);
->add('database_backup.rotate', RotateCommand::class);
}
}
23 changes: 0 additions & 23 deletions src/Utility/BackupExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* Utility to export databases.
*
* @property ?string $compression
* @property ?string $emailRecipient
* @property string $extension
* @property int $rotate
*/
Expand All @@ -40,11 +39,6 @@ class BackupExport extends AbstractBackupUtility
*/
private string $defaultExtension = 'sql';

/**
* @var string|null
*/
protected ?string $emailRecipient = null;

/**
* @var string
*/
Expand Down Expand Up @@ -144,20 +138,6 @@ public function rotate(int $rotate): self
return $this;
}

/**
* Sets the recipient's email address to send the backup file via mail.
*
* @param string|null $recipient Recipient's email address or `null` to disable
* @return self
* @since 1.1.0
*/
public function send(?string $recipient = null): self
{
$this->emailRecipient = $recipient;

return $this;
}

/**
* Exports the database.
*
Expand Down Expand Up @@ -200,9 +180,6 @@ public function export(): string|false
//Dispatches the `Backup.afterExport` event implemented by the driver
$this->getDriver()->dispatchEvent('Backup.afterExport');

if ($this->emailRecipient) {
BackupManager::send($filename, $this->emailRecipient);
}
if ($this->rotate) {
BackupManager::rotate($this->rotate);
}
Expand Down
41 changes: 0 additions & 41 deletions src/Utility/BackupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Cake\Collection\CollectionInterface;
use Cake\Core\Configure;
use Cake\I18n\DateTime;
use Cake\Mailer\Mailer;
use DatabaseBackup\BackupTrait;
use LogicException;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -113,44 +112,4 @@ public static function rotate(int $rotate): array

return $backupsToBeDeleted->toList();
}

/**
* Internal method to get an email instance with all options to send a backup file via email.
*
* @param string $backup Backup you want to send
* @param string $recipient Recipient's email address
* @return \Cake\Mailer\Mailer
* @since 1.1.0
* @throws \LogicException
*/
protected static function getEmailInstance(string $backup, string $recipient): Mailer
{
$filename = self::getAbsolutePath($backup);
if (!is_readable($filename)) {
throw new LogicException(__d('database_backup', 'File or directory `{0}` is not readable', $filename));
}
$server = env('SERVER_NAME', 'localhost');

return (new Mailer())
->setFrom(Configure::readOrFail('DatabaseBackup.mailSender'))
->setTo($recipient)
->setSubject(__d('database_backup', 'Database backup {0} from {1}', basename($filename), $server))
->setAttachments([
basename($filename) => ['file' => $filename, 'mimetype' => mime_content_type($filename)],
]);
}

/**
* Sends a backup file via email.
*
* @param string $filename Backup filename you want to send via email. The path can be relative to the backup directory
* @param string $recipient Recipient's email address
* @return array{headers: string, message: string}
* @throws \LogicException
* @since 1.1.0
*/
public static function send(string $filename, string $recipient): array
{
return self::getEmailInstance($filename, $recipient)->send();
}
}
53 changes: 0 additions & 53 deletions tests/TestCase/Command/DeleteAllCommandTest.php

This file was deleted.

Loading

0 comments on commit 59093ac

Please sign in to comment.