Shikiryu_Backup/app/code/Scenario.php

93 lines
2.3 KiB
PHP
Raw Normal View History

2015-07-10 19:07:14 +02:00
<?php
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;
2016-07-03 21:54:35 +02:00
class Scenario
{
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;
public static function register()
{
2015-08-17 13:16:28 +02:00
include __DIR__.'/../../vendor/autoload.php';
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
*
* @see BackupAbstract::isValid
* @see TransportAbstract::send
*
* @throws \Exception
*/
2015-07-10 23:13:46 +02:00
public function send()
{
2015-08-18 17:02:37 +02:00
if ($this->backup->isValid()) {
$this->transport->send();
} else {
2019-01-25 22:19:50 +01:00
throw new \Exception('Backup configuration is invalid.');
2015-07-11 00:12:31 +02:00
}
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
*
* @throws \Exception
*/
2015-07-10 19:07:14 +02:00
public static function launch($scenario)
{
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);
$scenario->send();
} catch (\Exception $e) {
throw $e;
}
exit;
2015-07-10 19:07:14 +02:00
}
throw new \Exception('invalid scenario.');
}
throw new \Exception('scenario not found.');
}
2015-07-11 00:12:31 +02:00
/**
* Check given scenario validation
*
* @param array $scenario
*
* @return bool
*/
2015-07-10 23:13:46 +02:00
public static function isValid(array $scenario)
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
}