PSR12 code standard.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
16
Makefile
16
Makefile
@ -1,12 +1,14 @@
|
||||
analyze-all:
|
||||
@echo PHPMessDetector
|
||||
analyze: analyze-phpmd analyze-phpstan analyze-psalm analyze-phpcs
|
||||
|
||||
analyze-phpmd:
|
||||
-./vendor/bin/phpmd src text cleancode,codesize,controversial,design,naming,unusedcode
|
||||
@echo PHPStan
|
||||
-./vendor/bin/phpstan analyze --level=7 src/ backup
|
||||
@echo Psalm
|
||||
analyze-phpstan:
|
||||
-./vendor/bin/phpstan analyze --level=7 --error-format=raw src/ backup
|
||||
analyze-psalm:
|
||||
-./vendor/bin/psalm
|
||||
@echo PHP_CodeSniffer
|
||||
-./vendor/bin/phpcs src
|
||||
analyze-phpcs:
|
||||
-./vendor/bin/phpcs src backup --report=emacs --standard=PSR12
|
||||
|
||||
install:
|
||||
php composer.phar install --no-dev
|
||||
install-dev:
|
||||
|
10
composer.lock
generated
10
composer.lock
generated
@ -2922,16 +2922,16 @@
|
||||
},
|
||||
{
|
||||
"name": "phpstan/phpstan",
|
||||
"version": "1.10.16",
|
||||
"version": "1.10.18",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/phpstan/phpstan.git",
|
||||
"reference": "352bdbb960bb523e3d71b834862589f910921c23"
|
||||
"reference": "52b6416c579663eebdd2f1d97df21971daf3b43f"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/352bdbb960bb523e3d71b834862589f910921c23",
|
||||
"reference": "352bdbb960bb523e3d71b834862589f910921c23",
|
||||
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/52b6416c579663eebdd2f1d97df21971daf3b43f",
|
||||
"reference": "52b6416c579663eebdd2f1d97df21971daf3b43f",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@ -2980,7 +2980,7 @@
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2023-06-05T08:21:46+00:00"
|
||||
"time": "2023-06-07T22:00:43+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpunit/php-code-coverage",
|
||||
|
@ -6,6 +6,7 @@
|
||||
xmlns="https://getpsalm.org/schema/config"
|
||||
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
|
||||
findUnusedBaselineEntry="true"
|
||||
findUnusedCode="true"
|
||||
>
|
||||
<projectFiles>
|
||||
<directory name="src/" />
|
||||
|
16
src/App.php
16
src/App.php
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Psr\Log\NullLogger;
|
||||
@ -20,32 +20,32 @@ use Psr\Log\NullLogger;
|
||||
class App
|
||||
{
|
||||
protected Logger $logger;
|
||||
protected array $config;
|
||||
protected mixed $config;
|
||||
|
||||
/**
|
||||
* Create a new instance providing a config file
|
||||
*
|
||||
* @param string $configFile Relative or full path to YML config.
|
||||
*/
|
||||
function __construct(string $configFile)
|
||||
public function __construct(string $configFile)
|
||||
{
|
||||
$this->config = Yaml::parseFile($configFile);
|
||||
$parser = new Yaml();
|
||||
$this->config = $parser->parseFile($configFile);
|
||||
$logger = new Logger('app');
|
||||
if (isset($this->config['log'])) {
|
||||
$logger->pushHandler(new StreamHandler($this->getConfig()['log']));
|
||||
}
|
||||
$logger->info("Initialization complete");
|
||||
|
||||
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the full configuration
|
||||
*
|
||||
* @return array Full configuration structure
|
||||
* @return mixed Full configuration structure
|
||||
*/
|
||||
function getConfig() : array
|
||||
public function getConfig(): mixed
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
@ -55,7 +55,7 @@ class App
|
||||
*
|
||||
* @return Logger Instance of logger
|
||||
*/
|
||||
function getLogger() : Logger
|
||||
public function getLogger(): Logger
|
||||
{
|
||||
return $this->logger;
|
||||
}
|
||||
|
@ -1,52 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
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 Ntfy\Server;
|
||||
use Ntfy\Message;
|
||||
use Ntfy\Client;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'backup',
|
||||
description: 'Start backup to assigned buckets.',
|
||||
hidden: false,
|
||||
)]
|
||||
class CommandBackup extends Command
|
||||
{
|
||||
static $defaultName = "backup";
|
||||
static $defaultDescription = "Start backup to assigned buckets";
|
||||
public static $defaultName = "backup";
|
||||
public static $defaultDescription = "Start backup to assigned buckets";
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->addArgument('config', InputArgument::OPTIONAL, 'Configuration file', "config.yml");
|
||||
$this->addArgument(
|
||||
'config',
|
||||
InputArgument::OPTIONAL,
|
||||
'Configuration file',
|
||||
"config.yml"
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->title('Start backup process');
|
||||
$sio = new SymfonyStyle($input, $output);
|
||||
$sio->title('Start backup process');
|
||||
|
||||
try {
|
||||
$app = new App($input->getArgument('config'));
|
||||
}
|
||||
catch (\Throwable $e) {
|
||||
$io->error('Unable to parse the YAML string: '. $e->getMessage());
|
||||
} catch (\Throwable $e) {
|
||||
$sio->error('Configuration error:' . $e->getMessage());
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
|
||||
$rclone = new Rclone\Rclone();
|
||||
$rclone->setLogger($app->getLogger()->withName('rclone'));
|
||||
|
||||
@ -57,7 +51,7 @@ class CommandBackup extends Command
|
||||
$twig = new Environment($loader);
|
||||
$twig->addExtension(new Twig\AppExtension());
|
||||
|
||||
foreach ($io->progressIterate($app->getConfig()['backup']) as $conf) {
|
||||
foreach ($sio->progressIterate($app->getConfig()['backup']) as $conf) {
|
||||
$message = new Message();
|
||||
$message->topic($app->getConfig()['notification']['topic']);
|
||||
$message->title($conf['title']);
|
||||
@ -82,7 +76,7 @@ class CommandBackup extends Command
|
||||
$client->send($message);
|
||||
}
|
||||
|
||||
$io->success("Complete");
|
||||
$sio->success("Complete");
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
@ -1,45 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
|
||||
#[AsCommand(
|
||||
name: 'show',
|
||||
description: 'Show all backup entries.',
|
||||
hidden: false,
|
||||
)]
|
||||
class CommandShow extends Command
|
||||
{
|
||||
static $defaultName = "show";
|
||||
static $defaultDescription = "Show all backup entries";
|
||||
public static $defaultName = "show";
|
||||
public static $defaultDescription = "Show all backup entries";
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->addArgument('config', InputArgument::OPTIONAL, 'Configuration file', "config.yml");
|
||||
$this->addArgument(
|
||||
'config',
|
||||
InputArgument::OPTIONAL,
|
||||
'Configuration file',
|
||||
"config.yml"
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$io->title('List backup entities');
|
||||
$sio = new SymfonyStyle($input, $output);
|
||||
$sio->title('List backup entities');
|
||||
|
||||
try {
|
||||
$app = new App($input->getArgument('config'));
|
||||
}
|
||||
catch (\Throwable $e) {
|
||||
$io->error('Unable to parse the YAML string: '. $e->getMessage());
|
||||
} catch (\Throwable $e) {
|
||||
$sio->error('Configuration error: ' . $e->getMessage());
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$io->table(['Description', 'Source', 'Destination'], $app->getConfig()['backup']);
|
||||
$sio->table(
|
||||
['Description', 'Source', 'Destination'],
|
||||
$app->getConfig()['backup']
|
||||
);
|
||||
|
||||
$io->success("Done");
|
||||
$sio->success("Done");
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
@ -1,15 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rclone;
|
||||
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
use Psr\Log\NullLogger;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Symfony\Component\Process\Exception\ProcessFailedException;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Wrapper for the rclone command line utility
|
||||
*
|
||||
@ -32,59 +31,55 @@ class Rclone
|
||||
protected string $version = "";
|
||||
|
||||
/**
|
||||
* Create a new instance
|
||||
* Create a new instance.
|
||||
*
|
||||
* Default it looks for "rclone" on the path.
|
||||
* But the path can be configured to be absolute.
|
||||
*
|
||||
* @param string $rclonePath Relative or absolute path
|
||||
*/
|
||||
function __construct(string $rclonePath = "rclone")
|
||||
public function __construct(string $rclonePath = "rclone")
|
||||
{
|
||||
$this->rclonePath = $rclonePath;
|
||||
$this->setLogger(new NullLogger);
|
||||
try
|
||||
{
|
||||
$this->setLogger(new NullLogger());
|
||||
|
||||
try {
|
||||
$version = $this->exec('--version');
|
||||
$this->version = explode("\n", $version)[0];
|
||||
}
|
||||
catch(ProcessFailedException $e)
|
||||
{
|
||||
} catch (ProcessFailedException $e) {
|
||||
throw new Exception("Check installation of rclone");
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (!\str_contains($this->version, 'rclone')) {
|
||||
throw new Exception("Rclone not recognized");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the rclone version
|
||||
* Get the rclone version.
|
||||
*
|
||||
* @return string Version string
|
||||
*/
|
||||
function getVersion(): string
|
||||
public function getVersion(): string
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the size of a mount/path
|
||||
* Calculate the size of a mount/path.
|
||||
*
|
||||
* @param string $path mount/path.
|
||||
*
|
||||
* @return int Size in bytes
|
||||
*/
|
||||
function getSize(string $path): int
|
||||
public function getSize(string $path): int
|
||||
{
|
||||
$output = $this->exec('size', ['--json', $path]);
|
||||
return (int)json_decode($output)->bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy from src to dest
|
||||
* Copy from source to destination.
|
||||
*
|
||||
* @param $src Source mount and path
|
||||
* @param $dest Destination mount and path
|
||||
@ -92,7 +87,7 @@ class Rclone
|
||||
*
|
||||
* @return string Stdout from command
|
||||
*/
|
||||
function copy(string $src, string $dest, string $bandwidth = null): string
|
||||
public function copy(string $src, string $dest, string $bandwidth = null): string
|
||||
{
|
||||
$options = array();
|
||||
|
||||
@ -107,7 +102,7 @@ class Rclone
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a command on the rclone binary
|
||||
* Execute a command on the rclone binary.
|
||||
*
|
||||
* @param string $command Top level Rclone command
|
||||
* @param array<String> $options Array of additional options
|
||||
|
@ -20,7 +20,7 @@ class AppExtension extends AbstractExtension
|
||||
/**
|
||||
* Extend the filters
|
||||
*
|
||||
*
|
||||
* @return TwigFilter[]
|
||||
*/
|
||||
public function getFilters(): array
|
||||
{
|
||||
@ -43,5 +43,4 @@ class AppExtension extends AbstractExtension
|
||||
$fact = (int)(floor((strlen((string)$bytes) - 1) / 3));
|
||||
return sprintf("%.{$precision}f", $bytes / pow(1024, $fact)) . $size[$fact];
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user