Shikiryu_Backup/app/code/Scenario.php

110 lines
3.0 KiB
PHP
Raw Permalink Normal View History

2020-04-16 18:02:46 +02:00
<?php declare(strict_types=1);
2015-07-10 19:07:14 +02:00
namespace Shikiryu\Backup;
2015-07-10 23:13:46 +02:00
use Shikiryu\Backup\Backup\BackupAbstract;
use Shikiryu\Backup\Transport\TransportAbstract;
2015-07-10 19:07:14 +02:00
use Shikiryu\Backup\Backup\Factory as BackupFactory;
use Shikiryu\Backup\Transport\Factory as TransportFactory;
2020-04-16 18:02:46 +02:00
use Shikiryu\Exceptions\BackupException;
use Shikiryu\Exceptions\ComposerNotFoundException;
use Shikiryu\Exceptions\ScenarioException;
use Shikiryu\Exceptions\ScenarioNotFoundException;
2015-07-10 19:07:14 +02:00
2020-04-16 18:02:46 +02:00
final class Scenario
2016-07-03 21:54:35 +02:00
{
2015-07-10 23:13:46 +02:00
/* @var $backup BackupAbstract */
2015-07-10 19:07:14 +02:00
private $backup;
2015-07-10 23:13:46 +02:00
/* @var $to TransportAbstract */
private $transport;
2020-04-16 18:02:46 +02:00
/**
* @throws ComposerNotFoundException
*/
2015-07-10 23:13:46 +02:00
public static function register()
{
2020-04-16 18:02:46 +02:00
$autoload_file = __DIR__ . '/../../vendor/autoload.php';
if (file_exists($autoload_file)) {
include $autoload_file;
} else {
throw new ComposerNotFoundException(sprintf('Autoloadfile «%s» not found.', $autoload_file));
}
2015-07-10 23:13:46 +02:00
}
2015-07-10 19:07:14 +02:00
/**
* @param array $scenario
*/
private function __construct(array $scenario)
{
2015-07-10 23:13:46 +02:00
define('TEMP_DIR', __DIR__.'/../temp/');
$this->backup = BackupFactory::build($scenario['backup']);
$this->transport = TransportFactory::build($this->backup, $scenario['transport']);
2015-07-10 19:07:14 +02:00
}
2015-08-18 17:02:37 +02:00
/**
* check if backup is valid and then launch the transfer
*
2020-04-16 18:02:46 +02:00
* @return bool
* @throws BackupException
2015-08-18 17:02:37 +02:00
* @see BackupAbstract::isValid
* @see TransportAbstract::send
*
*/
2020-04-16 18:02:46 +02:00
public function send(): bool
2015-07-10 23:13:46 +02:00
{
2015-08-18 17:02:37 +02:00
if ($this->backup->isValid()) {
2020-04-16 18:02:46 +02:00
return $this->transport->send();
2015-07-11 00:12:31 +02:00
}
2020-04-16 18:02:46 +02:00
throw new BackupException('Backup configuration is invalid.');
2015-07-10 23:13:46 +02:00
}
2015-07-10 19:07:14 +02:00
2015-07-11 00:12:31 +02:00
/**
* Launch the whole job
*
* @param $scenario
*
2020-04-16 18:02:46 +02:00
* @return bool
* @throws BackupException
* @throws ComposerNotFoundException
* @throws ScenarioException
* @throws ScenarioNotFoundException
2015-07-11 00:12:31 +02:00
*/
2020-04-16 18:02:46 +02:00
public static function launch($scenario): bool
2015-07-10 19:07:14 +02:00
{
2015-07-10 23:13:46 +02:00
// add autoloader
static::register();
// check the given scenario
2015-07-10 19:07:14 +02:00
if (is_readable($scenario)) {
$scenario = json_decode(file_get_contents($scenario), true);
2019-01-25 22:19:50 +01:00
if ($scenario !== null && static::isValid($scenario)) {
2015-07-11 00:12:31 +02:00
try {
$scenario = new self($scenario);
2020-04-16 18:02:46 +02:00
return $scenario->send();
} catch (BackupException $e) {
2015-07-11 00:12:31 +02:00
throw $e;
}
2015-07-10 19:07:14 +02:00
}
2020-04-16 18:02:46 +02:00
throw new ScenarioException('invalid scenario.');
2015-07-10 19:07:14 +02:00
}
2020-04-16 18:02:46 +02:00
throw new ScenarioNotFoundException(sprintf('scenario «%s» not found.', $scenario));
2015-07-10 19:07:14 +02:00
}
2015-07-11 00:12:31 +02:00
/**
* Check given scenario validation
*
* @param array $scenario
*
* @return bool
*/
2020-04-16 18:02:46 +02:00
public static function isValid(array $scenario): bool
2015-07-10 19:07:14 +02:00
{
return
2019-01-25 22:19:50 +01:00
isset($scenario['backup'], $scenario['transport']) &&
2015-07-10 23:13:46 +02:00
count($scenario['backup']) === 1 &&
count($scenario['transport']) === 1;
2015-07-10 19:07:14 +02:00
}
2016-07-03 21:54:35 +02:00
}