Shikiryu_Backup/app/code/Scenario.php
2015-07-12 20:34:54 +02:00

102 lines
2.6 KiB
PHP

<?php
namespace Shikiryu\Backup;
use Shikiryu\Backup\Backup\BackupAbstract;
use Shikiryu\Backup\Transport\TransportAbstract;
use Shikiryu\Backup\Backup\Factory as BackupFactory;
use Shikiryu\Backup\Transport\Factory as TransportFactory;
class Scenario {
/* @var $backup BackupAbstract */
private $backup;
/* @var $to TransportAbstract */
private $transport;
public static function register()
{
spl_autoload_register(array(__CLASS__, 'autoload'));
}
public static function autoload($class)
{
if (strpos($class, __NAMESPACE__.'\\') === 0)
{
$relative_NS = str_replace(__NAMESPACE__, '', $class);
$translated_path = str_replace('\\', '/', $relative_NS);
require __DIR__ . '/' . $translated_path . '.php';
}
}
/**
* @param array $scenario
*/
private function __construct(array $scenario)
{
define('TEMP_DIR', __DIR__.'/../temp/');
$this->backup = BackupFactory::build($scenario['backup']);
$this->transport = TransportFactory::build($this->backup, $scenario['transport']);
}
public function send()
{
try {
if ($this->backup->isValid()) {
$this->transport->send();
} else {
throw new \Exception("Backup configuration is invalid.");
}
} catch (\Exception $e) {
throw $e;
}
}
/**
* Launch the whole job
*
* @param $scenario
*
* @throws \Exception
*/
public static function launch($scenario)
{
// add autoloader
static::register();
$scenario = __DIR__.'/../scenario/'.$scenario;
// check the given scenario
if (is_readable($scenario)) {
$scenario = json_decode(file_get_contents($scenario), true);
if (!is_null($scenario) && static::isValid($scenario)) {
try {
$scenario = new self($scenario);
$scenario->send();
} catch (\Exception $e) {
throw $e;
}
exit;
}
throw new \Exception('invalid scenario.');
}
throw new \Exception('scenario not found.');
}
/**
* Check given scenario validation
*
* @param array $scenario
*
* @return bool
*/
public static function isValid(array $scenario)
{
return
isset($scenario['backup']) &&
count($scenario['backup']) === 1 &&
isset($scenario['transport']) &&
count($scenario['transport']) === 1;
}
}