From 4ffac17b6210369a0b67405e9c17f170625241dd Mon Sep 17 00:00:00 2001 From: Jens True Date: Tue, 13 Jun 2023 08:15:29 +0000 Subject: [PATCH] Static analysis cleanup. --- config.example.yml | 2 +- src/CommandBackup.php | 7 +++++-- src/CommandShow.php | 5 ++--- src/Notification/Notification.php | 32 +++++++++++++++++++++++++------ src/Notification/Ntfy.php | 14 +++++++++----- src/Rclone/Rclone.php | 9 ++++----- 6 files changed, 47 insertions(+), 22 deletions(-) diff --git a/config.example.yml b/config.example.yml index 9f41f19..d5d4664 100644 --- a/config.example.yml +++ b/config.example.yml @@ -1,5 +1,5 @@ notification: - Ntfy: + - type: Ntfy domain: https://ntfy.jcktrue.dk topic: backup log: output.log diff --git a/src/CommandBackup.php b/src/CommandBackup.php index 68fbd5a..6ca06dc 100644 --- a/src/CommandBackup.php +++ b/src/CommandBackup.php @@ -54,7 +54,6 @@ class CommandBackup extends Command foreach ($sio->progressIterate($app->getConfig()['backup']) as $conf) { $title = $conf['title']; - $message = ""; try { $template = array(); $template['config'] = $conf; @@ -63,7 +62,11 @@ class CommandBackup extends Command $template['rclone_version'] = $rclone->getVersion(); $template['destination_size_before'] = $rclone->getSize($conf['destination']); - $template['stdout'] = $rclone->copy($conf['source'], $conf['destination'], $app->getConfig()['rclone']['options']); + $template['stdout'] = $rclone->copy( + $conf['source'], + $conf['destination'], + $app->getConfig()['rclone']['options'] + ); $template['destination_size_after'] = $rclone->getSize($conf['destination']); $template['end'] = new DateTime(); diff --git a/src/CommandShow.php b/src/CommandShow.php index 921ad2b..1848ee2 100644 --- a/src/CommandShow.php +++ b/src/CommandShow.php @@ -19,9 +19,8 @@ class CommandShow extends Command { $this->addArgument( 'config', - InputArgument::OPTIONAL, - 'Configuration file', - "config.yml" + InputArgument::REQUIRED, + 'Configuration file' ); } diff --git a/src/Notification/Notification.php b/src/Notification/Notification.php index 2443a2f..344092c 100644 --- a/src/Notification/Notification.php +++ b/src/Notification/Notification.php @@ -2,24 +2,44 @@ namespace App\Notification; +use App\Notification\Ntfy; + class Notification { /** - * @type NotificationInterface[] $notifiers + * @var NotificationInterface[] $notifiers */ public array $notifiers = array(); + + /** + * Load multiple configurations + * + * @param array> $config Array of notifier configurations. + */ public function loadMany(array $config): void { - foreach ($config as $key => $conf) { - $this->loadSingle($key, $conf); + foreach ($config as $conf) { + $this->loadSingle($conf['type'], $conf); } } + /** + * Load a single configuration + * + * @param string $key Notification class + * @param string[] $config Implementation specific configuration + */ public function loadSingle(string $key, array $config): void { - $class = "\App\Notification\\" . $key; - - $this->notifiers[$key] = new $class($config); + switch ($key) { + case 'ntfy': + case 'Ntfy': + case 'NTFY': + $this->notifiers[] = new Ntfy($config); + break; + default: + break; + } } public function send(string $title, string $message): void diff --git a/src/Notification/Ntfy.php b/src/Notification/Ntfy.php index dd1294c..eea38dc 100644 --- a/src/Notification/Ntfy.php +++ b/src/Notification/Ntfy.php @@ -8,17 +8,21 @@ use Ntfy\Client; class Ntfy implements NotificationInterface { + /** @var string[] $config */ private array $config; - private \Ntfy\Server $server; - private \Ntfy\Client $client; - + private Client $client; + + /** + * Initialize with configuration. + * + * @param string[] $config Configuration + */ public function __construct(array $config) { $this->config = $config; - $this->server = new Server($config['domain']); - $this->client = new Client($this->server); + $this->client = new Client(new Server($config['domain'])); } public function send(string $title, string $message): void diff --git a/src/Rclone/Rclone.php b/src/Rclone/Rclone.php index 2c2f843..d100bed 100644 --- a/src/Rclone/Rclone.php +++ b/src/Rclone/Rclone.php @@ -81,9 +81,9 @@ class Rclone /** * Copy from source to destination. * - * @param $src Source mount and path - * @param $dest Destination mount and path - * @param $additionalOptions strings[] Bandwidth limit provided as string + * @param $src Source mount and path + * @param $dest Destination mount and path + * @param string[] $additionalOptions Additional options * * @return string Stdout from command */ @@ -93,6 +93,7 @@ class Rclone $options[] = $src; $options[] = $dest; + foreach ($additionalOptions as $key => $value) { if (strlen($key) == 1) { $options[] = '-' . $key; @@ -100,8 +101,6 @@ class Rclone } elseif (strlen($key) > 2) { $options[] = '--' . $key; $options[] = $value; - } else { - $options[] = $value; } }