This commit is contained in:
@ -4,17 +4,20 @@ pipeline:
|
|||||||
commands:
|
commands:
|
||||||
- composer install --no-dev
|
- composer install --no-dev
|
||||||
run:
|
run:
|
||||||
image: php
|
image: php:cli-bookworm
|
||||||
commands:
|
commands:
|
||||||
- ./backup show config.example.yml
|
- ./backup show config.example.yml
|
||||||
test:
|
dependencies:
|
||||||
image: composer
|
image: composer
|
||||||
commands:
|
commands:
|
||||||
|
- composer install
|
||||||
|
analyze:
|
||||||
|
image: php:cli-bookworm
|
||||||
|
commands:
|
||||||
|
- make analyze
|
||||||
|
test:
|
||||||
|
image: php:cli-bookworm
|
||||||
|
commands:
|
||||||
- apt update
|
- apt update
|
||||||
- apt install rclone
|
- apt install rclone
|
||||||
- composer install
|
- vendor/bin/phpunit tests
|
||||||
- vendor/bin/phpunit tests
|
|
||||||
tools:
|
|
||||||
image: php
|
|
||||||
commands:
|
|
||||||
- make analyze
|
|
@ -8,11 +8,10 @@ use Symfony\Component\Console\Input\InputInterface;
|
|||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
use Twig\Environment;
|
use App\Template\Twig;
|
||||||
use Twig\Loader\ArrayLoader;
|
|
||||||
use DateTime;
|
|
||||||
use App\Notification\Notification;
|
use App\Notification\Notification;
|
||||||
use App\Rclone\Rclone;
|
use App\Rclone\Rclone;
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
#[AsCommand(
|
#[AsCommand(
|
||||||
name: 'backup',
|
name: 'backup',
|
||||||
@ -47,9 +46,7 @@ class CommandBackup extends Command
|
|||||||
$notification = new Notification();
|
$notification = new Notification();
|
||||||
$notification->loadMany($app->getConfig()['notification']);
|
$notification->loadMany($app->getConfig()['notification']);
|
||||||
|
|
||||||
$loader = new ArrayLoader($app->getConfig()['templates']);
|
$render = new Twig($app->getConfig()['templates']);
|
||||||
$twig = new Environment($loader);
|
|
||||||
$twig->addExtension(new Twig\AppExtension());
|
|
||||||
|
|
||||||
foreach ($sio->progressIterate($app->getConfig()['backup']) as $conf) {
|
foreach ($sio->progressIterate($app->getConfig()['backup']) as $conf) {
|
||||||
$title = $conf['title'];
|
$title = $conf['title'];
|
||||||
@ -66,7 +63,7 @@ class CommandBackup extends Command
|
|||||||
$template['destination_size_after'] = $rclone->getSize($conf['destination']);
|
$template['destination_size_after'] = $rclone->getSize($conf['destination']);
|
||||||
$template['end'] = new DateTime();
|
$template['end'] = new DateTime();
|
||||||
|
|
||||||
$message = $twig->render('notify', $template);
|
$message = $render->render('notify', $template);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$message = $e->getMessage();
|
$message = $e->getMessage();
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ class Notification
|
|||||||
*
|
*
|
||||||
* @param string $key Notification class
|
* @param string $key Notification class
|
||||||
* @param string[] $config Implementation specific configuration
|
* @param string[] $config Implementation specific configuration
|
||||||
|
* @SuppressWarnings(PHPMD)
|
||||||
*/
|
*/
|
||||||
public function loadSingle(string $key, array $config): void
|
public function loadSingle(string $key, array $config): void
|
||||||
{
|
{
|
||||||
@ -35,7 +36,7 @@ class Notification
|
|||||||
case 'ntfy':
|
case 'ntfy':
|
||||||
case 'Ntfy':
|
case 'Ntfy':
|
||||||
case 'NTFY':
|
case 'NTFY':
|
||||||
$this->notifiers[] = new Ntfy($config);
|
$this->notifiers[] = Ntfy::factory($config);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -8,27 +8,41 @@ use Ntfy\Client;
|
|||||||
|
|
||||||
class Ntfy implements NotificationInterface
|
class Ntfy implements NotificationInterface
|
||||||
{
|
{
|
||||||
/** @var string[] $config */
|
|
||||||
private array $config;
|
|
||||||
|
|
||||||
private Client $client;
|
private Client $client;
|
||||||
|
private string $topic = 'default';
|
||||||
/**
|
/**
|
||||||
* Initialize with configuration.
|
* Initialize with configuration.
|
||||||
*
|
*
|
||||||
* @param string[] $config 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
|
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 = new Message();
|
||||||
$msg->topic($this->config['topic']);
|
$msg->topic($this->topic);
|
||||||
$msg->title($title);
|
$msg->title($title);
|
||||||
$msg->body($message);
|
$msg->body($message);
|
||||||
$this->client->send($msg);
|
$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
|
<?php
|
||||||
|
|
||||||
namespace App\Twig;
|
namespace App\Template;
|
||||||
|
|
||||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
use Twig\Extension\AbstractExtension;
|
use Twig\Extension\AbstractExtension;
|
||||||
@ -10,12 +10,8 @@ use Twig\TwigFilter;
|
|||||||
* Twig extension
|
* Twig extension
|
||||||
*
|
*
|
||||||
* Additional formatters for templates
|
* 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
|
* Extend the filters
|
@ -1,6 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace App\Tests;
|
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Component\Console\Application;
|
use Symfony\Component\Console\Application;
|
||||||
use Symfony\Component\Console\Tester\CommandTester;
|
use Symfony\Component\Console\Tester\CommandTester;
|
||||||
|
24
tests/Notification/NtfyTest.php
Normal file
24
tests/Notification/NtfyTest.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use App\Notification\Ntfy;
|
||||||
|
|
||||||
|
use Ntfy\Server;
|
||||||
|
use Ntfy\Client;
|
||||||
|
use Ntfy\Message;
|
||||||
|
final class NtfyTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @doesNotPerformAssertions
|
||||||
|
*/
|
||||||
|
public function testDoesSomething(): void
|
||||||
|
{
|
||||||
|
//Dependency
|
||||||
|
$client = $this->createStub(Client::class);
|
||||||
|
|
||||||
|
$sut = new Ntfy($client);
|
||||||
|
|
||||||
|
$client->expects($this->once())->method('send');
|
||||||
|
$sut->send('title','text');
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
<?php declare(strict_types=1);
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
use App\Rclone\Rclone;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
final class RcloneTest extends TestCase
|
final class RcloneTest extends TestCase
|
||||||
@ -18,26 +19,26 @@ final class RcloneTest extends TestCase
|
|||||||
public function testRclonePath(): void
|
public function testRclonePath(): void
|
||||||
{
|
{
|
||||||
$this->expectException(\Exception::class);
|
$this->expectException(\Exception::class);
|
||||||
$rclone = new \App\Rclone\Rclone('invalid');
|
$rclone = new Rclone('invalid');
|
||||||
$this->assertString('', $rclone->getVersion());
|
$this->assertString('', $rclone->getVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRcloneInvalidVersion(): void
|
public function testRcloneInvalidVersion(): void
|
||||||
{
|
{
|
||||||
$this->expectException(\Exception::class);
|
$this->expectException(\Exception::class);
|
||||||
$rclone = new \App\Rclone\Rclone('uname');
|
$rclone = new Rclone('uname');
|
||||||
$this->assertString('', $rclone->getVersion());
|
$this->assertString('', $rclone->getVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRcloneValidVersion(): void
|
public function testRcloneValidVersion(): void
|
||||||
{
|
{
|
||||||
$rclone = new \App\Rclone\Rclone();
|
$rclone = new Rclone();
|
||||||
$this->assertStringContainsString('rclone', $rclone->getVersion());
|
$this->assertStringContainsString('rclone', $rclone->getVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRcloneSize(): void
|
public function testRcloneSize(): void
|
||||||
{
|
{
|
||||||
$rclone = new \App\Rclone\Rclone();
|
$rclone = new Rclone();
|
||||||
$size = $rclone->getSize('temp/source');
|
$size = $rclone->getSize('temp/source');
|
||||||
$this->assertGreaterThan(10000, $size);
|
$this->assertGreaterThan(10000, $size);
|
||||||
|
|
||||||
@ -49,11 +50,11 @@ final class RcloneTest extends TestCase
|
|||||||
|
|
||||||
public function testRcloneCopy(): void
|
public function testRcloneCopy(): void
|
||||||
{
|
{
|
||||||
$rclone = new \App\Rclone\Rclone();
|
$rclone = new Rclone();
|
||||||
$result = $rclone->copy('temp/source','temp/destination');
|
$result = $rclone->copy('temp/source','temp/destination');
|
||||||
$this->assertDirectoryExists('temp/destination');
|
$this->assertDirectoryExists('temp/destination');
|
||||||
|
|
||||||
$rclone = new \App\Rclone\Rclone();
|
$rclone = new Rclone();
|
||||||
$result = $rclone->copy('temp/source','temp/destination',['bwlimit'=>'6M']);
|
$result = $rclone->copy('temp/source','temp/destination',['bwlimit'=>'6M']);
|
||||||
$this->assertDirectoryExists('temp/destination');
|
$this->assertDirectoryExists('temp/destination');
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user