mirror of
https://github.com/Chouchen/Shikiryu_Backup.git
synced 2021-06-30 16:02:14 +02:00
✅ Add first tests
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<?php
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Shikiryu\Backup;
|
||||
|
||||
@@ -6,17 +6,29 @@ use Shikiryu\Backup\Backup\BackupAbstract;
|
||||
use Shikiryu\Backup\Transport\TransportAbstract;
|
||||
use Shikiryu\Backup\Backup\Factory as BackupFactory;
|
||||
use Shikiryu\Backup\Transport\Factory as TransportFactory;
|
||||
use Shikiryu\Exceptions\BackupException;
|
||||
use Shikiryu\Exceptions\ComposerNotFoundException;
|
||||
use Shikiryu\Exceptions\ScenarioException;
|
||||
use Shikiryu\Exceptions\ScenarioNotFoundException;
|
||||
|
||||
class Scenario
|
||||
final class Scenario
|
||||
{
|
||||
/* @var $backup BackupAbstract */
|
||||
private $backup;
|
||||
/* @var $to TransportAbstract */
|
||||
private $transport;
|
||||
|
||||
/**
|
||||
* @throws ComposerNotFoundException
|
||||
*/
|
||||
public static function register()
|
||||
{
|
||||
include __DIR__.'/../../vendor/autoload.php';
|
||||
$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));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,18 +44,19 @@ class Scenario
|
||||
/**
|
||||
* check if backup is valid and then launch the transfer
|
||||
*
|
||||
* @return bool
|
||||
* @throws BackupException
|
||||
* @see BackupAbstract::isValid
|
||||
* @see TransportAbstract::send
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function send()
|
||||
public function send(): bool
|
||||
{
|
||||
if ($this->backup->isValid()) {
|
||||
$this->transport->send();
|
||||
} else {
|
||||
throw new \Exception('Backup configuration is invalid.');
|
||||
return $this->transport->send();
|
||||
}
|
||||
|
||||
throw new BackupException('Backup configuration is invalid.');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,9 +64,13 @@ class Scenario
|
||||
*
|
||||
* @param $scenario
|
||||
*
|
||||
* @throws \Exception
|
||||
* @return bool
|
||||
* @throws BackupException
|
||||
* @throws ComposerNotFoundException
|
||||
* @throws ScenarioException
|
||||
* @throws ScenarioNotFoundException
|
||||
*/
|
||||
public static function launch($scenario)
|
||||
public static function launch($scenario): bool
|
||||
{
|
||||
// add autoloader
|
||||
static::register();
|
||||
@@ -64,15 +81,15 @@ class Scenario
|
||||
if ($scenario !== null && static::isValid($scenario)) {
|
||||
try {
|
||||
$scenario = new self($scenario);
|
||||
$scenario->send();
|
||||
} catch (\Exception $e) {
|
||||
return $scenario->send();
|
||||
} catch (BackupException $e) {
|
||||
throw $e;
|
||||
}
|
||||
exit;
|
||||
}
|
||||
throw new \Exception('invalid scenario.');
|
||||
throw new ScenarioException('invalid scenario.');
|
||||
}
|
||||
throw new \Exception('scenario not found.');
|
||||
|
||||
throw new ScenarioNotFoundException(sprintf('scenario «%s» not found.', $scenario));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,7 +99,7 @@ class Scenario
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isValid(array $scenario)
|
||||
public static function isValid(array $scenario): bool
|
||||
{
|
||||
return
|
||||
isset($scenario['backup'], $scenario['transport']) &&
|
||||
|
10
app/code/exceptions/BackupException.php
Normal file
10
app/code/exceptions/BackupException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class BackupException extends Exception
|
||||
{
|
||||
|
||||
}
|
10
app/code/exceptions/ComposerNotFoundException.php
Normal file
10
app/code/exceptions/ComposerNotFoundException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ComposerNotFoundException extends Exception
|
||||
{
|
||||
|
||||
}
|
10
app/code/exceptions/DistantException.php
Normal file
10
app/code/exceptions/DistantException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class DistantException extends Exception
|
||||
{
|
||||
|
||||
}
|
10
app/code/exceptions/LocalException.php
Normal file
10
app/code/exceptions/LocalException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class LocalException extends Exception
|
||||
{
|
||||
|
||||
}
|
10
app/code/exceptions/ScenarioException.php
Normal file
10
app/code/exceptions/ScenarioException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ScenarioException extends Exception
|
||||
{
|
||||
|
||||
}
|
10
app/code/exceptions/ScenarioNotFoundException.php
Normal file
10
app/code/exceptions/ScenarioNotFoundException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class ScenarioNotFoundException extends Exception
|
||||
{
|
||||
|
||||
}
|
10
app/code/exceptions/TransportException.php
Normal file
10
app/code/exceptions/TransportException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class TransportException extends Exception
|
||||
{
|
||||
|
||||
}
|
Reference in New Issue
Block a user