This commit is contained in:
		@@ -8,11 +8,10 @@ use Symfony\Component\Console\Input\InputInterface;
 | 
			
		||||
use Symfony\Component\Console\Input\InputArgument;
 | 
			
		||||
use Symfony\Component\Console\Output\OutputInterface;
 | 
			
		||||
use Symfony\Component\Console\Style\SymfonyStyle;
 | 
			
		||||
use Twig\Environment;
 | 
			
		||||
use Twig\Loader\ArrayLoader;
 | 
			
		||||
use DateTime;
 | 
			
		||||
use App\Template\Twig;
 | 
			
		||||
use App\Notification\Notification;
 | 
			
		||||
use App\Rclone\Rclone;
 | 
			
		||||
use DateTime;
 | 
			
		||||
 | 
			
		||||
#[AsCommand(
 | 
			
		||||
    name: 'backup',
 | 
			
		||||
@@ -47,9 +46,7 @@ class CommandBackup extends Command
 | 
			
		||||
        $notification = new Notification();
 | 
			
		||||
        $notification->loadMany($app->getConfig()['notification']);
 | 
			
		||||
 | 
			
		||||
        $loader = new ArrayLoader($app->getConfig()['templates']);
 | 
			
		||||
        $twig = new Environment($loader);
 | 
			
		||||
        $twig->addExtension(new Twig\AppExtension());
 | 
			
		||||
        $render = new Twig($app->getConfig()['templates']);
 | 
			
		||||
 | 
			
		||||
        foreach ($sio->progressIterate($app->getConfig()['backup']) as $conf) {
 | 
			
		||||
            $title = $conf['title'];
 | 
			
		||||
@@ -66,7 +63,7 @@ class CommandBackup extends Command
 | 
			
		||||
                $template['destination_size_after'] = $rclone->getSize($conf['destination']);
 | 
			
		||||
                $template['end'] = new DateTime();
 | 
			
		||||
 | 
			
		||||
                $message = $twig->render('notify', $template);
 | 
			
		||||
                $message = $render->render('notify', $template);
 | 
			
		||||
            } catch (\Exception $e) {
 | 
			
		||||
                $message = $e->getMessage();
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
@@ -28,6 +28,7 @@ class Notification
 | 
			
		||||
     *
 | 
			
		||||
     * @param string $key      Notification class
 | 
			
		||||
     * @param string[] $config Implementation specific configuration
 | 
			
		||||
     *  @SuppressWarnings(PHPMD)
 | 
			
		||||
     */
 | 
			
		||||
    public function loadSingle(string $key, array $config): void
 | 
			
		||||
    {
 | 
			
		||||
@@ -35,7 +36,7 @@ class Notification
 | 
			
		||||
            case 'ntfy':
 | 
			
		||||
            case 'Ntfy':
 | 
			
		||||
            case 'NTFY':
 | 
			
		||||
                $this->notifiers[] = new Ntfy($config);
 | 
			
		||||
                $this->notifiers[] = Ntfy::factory($config);
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                break;
 | 
			
		||||
 
 | 
			
		||||
@@ -8,27 +8,41 @@ use Ntfy\Client;
 | 
			
		||||
 | 
			
		||||
class Ntfy implements NotificationInterface
 | 
			
		||||
{
 | 
			
		||||
    /** @var string[] $config */
 | 
			
		||||
    private array $config;
 | 
			
		||||
 | 
			
		||||
    private Client $client;
 | 
			
		||||
 | 
			
		||||
    private string $topic = 'default';
 | 
			
		||||
    /**
 | 
			
		||||
     * Initialize with configuration.
 | 
			
		||||
     *
 | 
			
		||||
     * @param string[] $config Configuration
 | 
			
		||||
     */
 | 
			
		||||
    public function __construct(array $config)
 | 
			
		||||
    public static function factory(array $config): self
 | 
			
		||||
    {
 | 
			
		||||
        $this->config = $config;
 | 
			
		||||
        $instance = new self(new Client(new Server($config['domain'])));
 | 
			
		||||
        if (isset($config['topic'])) {
 | 
			
		||||
            $instance->setTopic($config['topic']);
 | 
			
		||||
        }
 | 
			
		||||
        return $instance;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        $this->client = new Client(new Server($config['domain']));
 | 
			
		||||
    public function __construct(Client $client)
 | 
			
		||||
    {
 | 
			
		||||
        $this->client = $client;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setTopic(string $topic): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->topic = $topic;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function send(string $title, string $message): void
 | 
			
		||||
    {
 | 
			
		||||
        assert(strlen($title) > 0);
 | 
			
		||||
        assert(strlen($title) < 256);
 | 
			
		||||
        assert(strlen($message) > 0);
 | 
			
		||||
        assert(strlen($message) < 4096);
 | 
			
		||||
 | 
			
		||||
        $msg = new Message();
 | 
			
		||||
        $msg->topic($this->config['topic']);
 | 
			
		||||
        $msg->topic($this->topic);
 | 
			
		||||
        $msg->title($title);
 | 
			
		||||
        $msg->body($message);
 | 
			
		||||
        $this->client->send($msg);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										20
									
								
								src/Template/Twig.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/Template/Twig.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Template;
 | 
			
		||||
 | 
			
		||||
use Twig\Environment;
 | 
			
		||||
use Twig\Loader\ArrayLoader;
 | 
			
		||||
use App\Template\TwigExtension;
 | 
			
		||||
 | 
			
		||||
class Twig extends Environment
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @param string[] $templates Array of templates
 | 
			
		||||
     */
 | 
			
		||||
    public function __construct(array $templates)
 | 
			
		||||
    {
 | 
			
		||||
        $loader = new ArrayLoader($templates);
 | 
			
		||||
        parent::__construct($loader);
 | 
			
		||||
        $this->addExtension(new TwigExtension());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Twig;
 | 
			
		||||
namespace App\Template;
 | 
			
		||||
 | 
			
		||||
use Symfony\Component\DependencyInjection\ContainerInterface;
 | 
			
		||||
use Twig\Extension\AbstractExtension;
 | 
			
		||||
@@ -10,12 +10,8 @@ use Twig\TwigFilter;
 | 
			
		||||
 * Twig extension
 | 
			
		||||
 *
 | 
			
		||||
 * Additional formatters for templates
 | 
			
		||||
 *
 | 
			
		||||
 * @author  Jens True <jens.chr.true@gmail.com>
 | 
			
		||||
 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
 | 
			
		||||
 * @link    https://jcktrue.dk
 | 
			
		||||
 */
 | 
			
		||||
class AppExtension extends AbstractExtension
 | 
			
		||||
class TwigExtension extends AbstractExtension
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Extend the filters
 | 
			
		||||
		Reference in New Issue
	
	Block a user