Add some PHPCS corrections

This commit is contained in:
Shikiryu 2016-07-03 21:54:35 +02:00
parent dd2a6bd7d7
commit ab7887ce84
13 changed files with 238 additions and 200 deletions

View File

@ -7,7 +7,8 @@ use Shikiryu\Backup\Transport\TransportAbstract;
use Shikiryu\Backup\Backup\Factory as BackupFactory; use Shikiryu\Backup\Backup\Factory as BackupFactory;
use Shikiryu\Backup\Transport\Factory as TransportFactory; use Shikiryu\Backup\Transport\Factory as TransportFactory;
class Scenario { class Scenario
{
/* @var $backup BackupAbstract */ /* @var $backup BackupAbstract */
private $backup; private $backup;
@ -103,5 +104,4 @@ class Scenario {
isset($scenario['transport']) && isset($scenario['transport']) &&
count($scenario['transport']) === 1; count($scenario['transport']) === 1;
} }
} }

View File

@ -7,7 +7,11 @@ use Shikiryu\Backup\Transport\TransportAbstract;
class Dropbox extends TransportAbstract class Dropbox extends TransportAbstract
{ {
/**
* Dropbox Client
*
* @var Client
*/
private $dropbox; private $dropbox;
protected $token = ''; protected $token = '';
@ -26,21 +30,21 @@ class Dropbox extends TransportAbstract
public function send() public function send()
{ {
$sent = true; $sent = true;
$files = $this->backup->getFilesToBackup(); $files = $this->backup->getFilesTobackup();
foreach ($files as $file => $name) { foreach ($files as $file => $name) {
$file = fopen($file, 'r'); $file = fopen($file, 'r');
$upload = $this->dropbox->uploadFile($this->folder.'/'.$name, \Dropbox\WriteMode::force(), $file); $upload = $this->dropbox->uploadFile($this->folder.'/'.$name, \Dropbox\WriteMode::force(), $file);
if (!$upload) { if (!$upload) {
$sent = false; $sent = false;
echo 'DROPBOX upload manquée de '.$file.' vers '.$this->folder.$name; echo 'DROPBOX upload manqu<EFBFBD>e de '.$file.' vers '.$this->folder.$name;
} }
} }
$streams = $this->backup->getStreamsToBackup(); $streams = $this->backup->getStreamsTobackup();
foreach ($streams as $stream => $name) { foreach ($streams as $stream => $name) {
$upload = $this->dropbox->uploadFileFromString($this->folder.'/'.$name, \Dropbox\WriteMode::force(), $stream); $upload = $this->dropbox->uploadFileFromString($this->folder.'/'.$name, \Dropbox\WriteMode::force(), $stream);
if (!$upload) { if (!$upload) {
$sent = false; $sent = false;
echo 'DROPBOX upload manquée de '.$file.' vers '.$this->folder.$name; echo 'DROPBOX upload manqu<EFBFBD>e de '.$file.' vers '.$this->folder.$name;
} }
} }
return $sent; return $sent;

View File

@ -147,8 +147,8 @@ class Email extends TransportAbstract
*/ */
public function __construct(BackupAbstract $backup, array $config) { public function __construct(BackupAbstract $backup, array $config) {
parent::__construct($backup, $config); parent::__construct($backup, $config);
$this->setFiles($this->backup->getFilesToBackup()); $this->setFiles($this->backup->getFilesTobackup());
$this->setStreams($this->backup->getStreamsToBackup()); $this->setStreams($this->backup->getStreamsTobackup());
$this->email_to = $this->config['to']; $this->email_to = $this->config['to'];
$this->email_from = $this->config['from']; $this->email_from = $this->config['from'];
$this->encoding = $this->config['encoding']; $this->encoding = $this->config['encoding'];

View File

@ -1,4 +1,5 @@
<?php <?php
namespace Shikiryu\Backup\Transport; namespace Shikiryu\Backup\Transport;
class Ftp extends TransportAbstract class Ftp extends TransportAbstract
@ -14,7 +15,8 @@ class Ftp extends TransportAbstract
private $files; private $files;
private $streams; private $streams;
public function __construct($backup, $config) { public function __construct($backup, $config)
{
parent::__construct($backup, $config); parent::__construct($backup, $config);
// $this->path = $this->config['path']; // $this->path = $this->config['path'];
@ -29,11 +31,12 @@ class Ftp extends TransportAbstract
$login = @ftp_login($this->connection, $this->login, $this->password); $login = @ftp_login($this->connection, $this->login, $this->password);
if ($login === false) { if ($login === false) {
throw new \Exception(sprintf('Connexion FTP %s refusée avec %s et %s', $this->host, $this->login, $this->password)); $msg = sprintf('Connexion FTP %s refusée avec %s et %s', $this->host, $this->login, $this->password);
throw new \Exception($msg);
} }
$this->setFiles($this->backup->getFilesToBackup()); $this->setFiles($this->backup->getFilesTobackup());
$this->setStreams($this->backup->getStreamsToBackup()); $this->setStreams($this->backup->getStreamsTobackup());
} }
private function setFiles($files = array()) private function setFiles($files = array())
@ -44,7 +47,8 @@ class Ftp extends TransportAbstract
return $this; return $this;
} }
private function setStreams($streams = array()) { private function setStreams($streams = array())
{
if (is_array($streams) && !empty($streams)) { if (is_array($streams) && !empty($streams)) {
$this->streams = $streams; $this->streams = $streams;
} }
@ -70,8 +74,9 @@ class Ftp extends TransportAbstract
if (!empty($this->streams)) { if (!empty($this->streams)) {
foreach ($this->streams as $name => $stream) { foreach ($this->streams as $name => $stream) {
if (count(explode('.', $name)) < 2) if (count(explode('.', $name)) < 2) {
$name = 'backup' . $name . '.txt'; $name = 'backup' . $name . '.txt';
}
file_put_contents($name, $stream); file_put_contents($name, $stream);
$upload = ftp_put($this->connection, $this->folder.$name, $name, FTP_ASCII); $upload = ftp_put($this->connection, $this->folder.$name, $name, FTP_ASCII);
if (!$upload) { if (!$upload) {
@ -93,15 +98,4 @@ class Ftp extends TransportAbstract
{ {
ftp_close($this->connection); ftp_close($this->connection);
} }
} }
?>

View File

@ -2,12 +2,15 @@
namespace Shikiryu\Backup\Transport; namespace Shikiryu\Backup\Transport;
use Shikiryu\Backup\Backup\BackupAbstract; use Shikiryu\Backup\Backup\BackupAbstract;
class Folder extends TransportAbstract class Folder extends TransportAbstract
{ {
/** @var string */ /**
* Folder to backup
*
* @var string
*/
protected $folder; protected $folder;
public function __construct(BackupAbstract $backup, array $config = array()) public function __construct(BackupAbstract $backup, array $config = array())
@ -26,12 +29,12 @@ class Folder extends TransportAbstract
*/ */
public function send() public function send()
{ {
foreach ($this->backup->getFilesToBackup() as $file => $name) { foreach ($this->backup->getFilesTobackup() as $file => $name) {
if (copy($file, $this->folder . $name) === false) { if (copy($file, $this->folder . $name) === false) {
throw new \Exception(sprintf('Copy of %s in %s failed', $name, $this->folder)); throw new \Exception(sprintf('Copy of %s in %s failed', $name, $this->folder));
}; };
} }
foreach ($this->backup->getStreamsToBackup() as $name => $file) { foreach ($this->backup->getStreamsTobackup() as $name => $file) {
if (count(explode('.', $name)) < 2) { if (count(explode('.', $name)) < 2) {
$name = 'backup' . $name . '.txt'; $name = 'backup' . $name . '.txt';
} }

View File

@ -42,23 +42,23 @@ class Sftp extends TransportAbstract
public function send() public function send()
{ {
$sent = true; $sent = true;
$files = $this->backup->getFilesToBackup(); $files = $this->backup->getFilesTobackup();
if (!empty($files)) { if (!empty($files)) {
foreach ($files as $file => $name) { foreach ($files as $file => $name) {
$upload = $this->connection->put($this->folder.'/'.$name, $file, LibSFTP::SOURCE_LOCAL_FILE); $upload = $this->connection->put($this->folder.'/'.$name, $file, LibSFTP::SOURCE_LOCAL_FILE);
if (!$upload) { if (!$upload) {
$sent = false; $sent = false;
echo 'SFTP upload manquée de '.$file.' vers '.$this->folder.$name; echo 'SFTP upload manqu<EFBFBD>e de '.$file.' vers '.$this->folder.$name;
} }
} }
} }
$streams = $this->backup->getStreamsToBackup(); $streams = $this->backup->getStreamsTobackup();
if (!empty($streams)) { if (!empty($streams)) {
foreach ($streams as $name => $stream) { foreach ($streams as $name => $stream) {
$upload = $this->connection->put($this->folder.'/'.$name, $stream); $upload = $this->connection->put($this->folder.'/'.$name, $stream);
if (!$upload) { if (!$upload) {
echo 'SFTP upload manquée de '.$name.' vers '.$this->folder.$name; echo 'SFTP upload manqu<EFBFBD>e de '.$name.' vers '.$this->folder.$name;
$sent = false; $sent = false;
} }
} }

View File

@ -25,5 +25,5 @@ abstract class TransportAbstract
/** /**
* @return bool * @return bool
*/ */
public abstract function send(); abstract public function send();
} }

View File

@ -4,15 +4,29 @@ namespace Shikiryu\Backup\Backup;
abstract class BackupAbstract abstract class BackupAbstract
{ {
/** @var array */ /**
* Options
*
* @var array
*/
protected $options; protected $options;
/** @var string[] */ /**
protected $_filesToBackup = []; * File path to backup
/** @var string[] */ *
protected $_streamsToBackup = []; * @var string[]
*/
protected $files_to_backup = [];
/**
* Streams to backup
*
* @var string[]
*/
protected $streams_to_backup = [];
/** /**
* @param array $config * Constructor
*
* @param array $config array of options and parameters
*/ */
function __construct($config = array()) function __construct($config = array())
{ {
@ -29,45 +43,53 @@ abstract class BackupAbstract
/** /**
* Magic setter method * Magic setter method
* *
* @param $name * @param string $name attribute name
* @param $value * @param mixed $value attribute value
*
* @return $this
*/ */
public function __set($name, $value) public function __set($name, $value)
{ {
$this->$name = $value; $this->$name = $value;
return $this;
} }
/** /**
* Getter
*
* @return array * @return array
*/ */
public function getFilesToBackup() public function getFilesToBackup()
{ {
return $this->_filesToBackup; return $this->files_to_backup;
} }
/** /**
* Getter
*
* @return array * @return array
*/ */
public function getStreamsToBackup() public function getStreamsToBackup()
{ {
return $this->_streamsToBackup; return $this->streams_to_backup;
} }
/** /**
* Check if all files got the minimum given size. * Check if all files got the minimum given size.
* *
* @param int $file_size * @param int $file_size file size
* *
* @return bool * @return bool
*/ */
public function checkMinimumFilesize($file_size) public function checkMinimumFilesize($file_size)
{ {
foreach ($this->_filesToBackup as $file => $name) { foreach ($this->files_to_backup as $file => $name) {
if (filesize($file) < $file_size) { if (filesize($file) < $file_size) {
return false; return false;
} }
} }
foreach ($this->_streamsToBackup as $name => $file) { foreach ($this->streams_to_backup as $name => $file) {
if (mb_strlen($file, 'utf-8') < $file_size) { if (mb_strlen($file, 'utf-8') < $file_size) {
return false; return false;
} }
@ -77,6 +99,8 @@ abstract class BackupAbstract
/** /**
* Initialize everything * Initialize everything
*
* @return $this
*/ */
protected function init() protected function init()
{ {
@ -84,20 +108,31 @@ abstract class BackupAbstract
$this->build(); $this->build();
$this->postBuild(); $this->postBuild();
$this->applyOptions(); $this->applyOptions();
return $this;
} }
/** /**
* Function that can be used to initialize the backup * Function that can be used to initialize the backup
*
* @return void
*/ */
abstract protected function preBuild(); abstract protected function preBuild();
/** /**
* Function that can be used after the backup * Function that can be used after the backup
*
* @return void
*/ */
abstract protected function postBuild(); abstract protected function postBuild();
/** /**
* Mandatory function doing the backup * Mandatory function doing the backup
*
* @return void
*/ */
abstract protected function build(); abstract protected function build();
/** /**
* Check if the backup is valid * Check if the backup is valid
* *
@ -106,13 +141,14 @@ abstract class BackupAbstract
abstract public function isValid(); abstract public function isValid();
/** /**
* Apply options
*
* @return $this * @return $this
*/ */
protected function applyOptions() protected function applyOptions()
{ {
// TODO isValid here ? // TODO isValid here ?
foreach($this->options as $name => $value) foreach ($this->options as $name => $value) {
{
$method = sprintf('setOption%s', ucfirst($name)); $method = sprintf('setOption%s', ucfirst($name));
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
call_user_func([$this, $method], $value); call_user_func([$this, $method], $value);
@ -132,18 +168,18 @@ abstract class BackupAbstract
protected function setOptionZip() protected function setOptionZip()
{ {
$zip = new \ZipArchive(); $zip = new \ZipArchive();
$zip_name = sprintf('%s.zip', (!empty($this->options['name']) ? $this->options['name'] : time())); // Zip name // Zip name
$zip_name = !empty($this->options['name']) ? $this->options['name'] : time();
$zip_name = sprintf('%s.zip', $zip_name);
if (touch(TEMP_DIR . $zip_name) === false) { if (touch(TEMP_DIR . $zip_name) === false) {
throw new \Exception('Backup::Zip::Permission denied.'); throw new \Exception('Backup::Zip::Permission denied.');
} }
if ($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::OVERWRITE)==TRUE) { if ($zip->open(TEMP_DIR . $zip_name, \ZipArchive::OVERWRITE) == TRUE) {
foreach($this->_filesToBackup as $file => $name) foreach ($this->files_to_backup as $file => $name) {
{
$zip->addFile($file, $name); // Adding files into zip $zip->addFile($file, $name); // Adding files into zip
} }
foreach($this->_streamsToBackup as $file => $name) foreach ($this->streams_to_backup as $file => $name) {
{
$zip->addFromString($file, $name); // Adding streams into zip $zip->addFromString($file, $name); // Adding streams into zip
} }
$zip->close(); $zip->close();
@ -151,8 +187,8 @@ abstract class BackupAbstract
throw new \Exception('Backup::Zip::Can\'t zip the given backup.'); throw new \Exception('Backup::Zip::Can\'t zip the given backup.');
} }
$this->_filesToBackup = [TEMP_DIR.$zip_name => $zip_name]; $this->files_to_backup = [TEMP_DIR . $zip_name => $zip_name];
$this->_streamsToBackup = []; $this->streams_to_backup = [];
return $this; return $this;
} }
@ -160,7 +196,7 @@ abstract class BackupAbstract
/** /**
* Add the current date with the given format into the files names * Add the current date with the given format into the files names
* *
* @param string $format * @param string $format date format
* *
* @return $this * @return $this
*/ */
@ -170,20 +206,20 @@ abstract class BackupAbstract
$format = 'Ymd'; $format = 'Ymd';
} }
$tmpFiles = array(); $tmpFiles = array();
foreach ($this->_filesToBackup as $file => $name) { foreach ($this->files_to_backup as $file => $name) {
$nameA = explode('.', $name); $nameA = explode('.', $name);
$nameA[] = end($nameA); $nameA[] = end($nameA);
$nameA[count($nameA) - 2] = date($format); $nameA[count($nameA) - 2] = date($format);
$name = implode('.', $nameA); $name = implode('.', $nameA);
$tmpFiles[$file] = $name; $tmpFiles[$file] = $name;
} }
$this->_filesToBackup = $tmpFiles; $this->files_to_backup = $tmpFiles;
$tmpStream = array(); $tmpStream = array();
foreach ($this->_streamsToBackup as $name => $stream) { foreach ($this->streams_to_backup as $name => $stream) {
$tmpStream[$name . '-' . date($format)] = $stream; $tmpStream[$name . '-' . date($format)] = $stream;
} }
$this->_streamsToBackup = $tmpStream; $this->streams_to_backup = $tmpStream;
return $this; return $this;
} }
@ -191,7 +227,7 @@ abstract class BackupAbstract
/** /**
* Add the current time with the given format into the files names * Add the current time with the given format into the files names
* *
* @param string $format * @param string $format time format
* *
* @return $this * @return $this
*/ */
@ -201,26 +237,28 @@ abstract class BackupAbstract
$format = 'his'; $format = 'his';
} }
$tmpFiles = array(); $tmpFiles = array();
foreach ($this->_filesToBackup as $file => $name) { foreach ($this->files_to_backup as $file => $name) {
$nameA = explode('.', $name); $nameA = explode('.', $name);
$nameA[] = end($nameA); $nameA[] = end($nameA);
$nameA[count($nameA) - 2] = date($format); $nameA[count($nameA) - 2] = date($format);
$name = implode('.', $nameA); $name = implode('.', $nameA);
$tmpFiles[$file] = $name; $tmpFiles[$file] = $name;
} }
$this->_filesToBackup = $tmpFiles; $this->files_to_backup = $tmpFiles;
$tmpStream = array(); $tmpStream = array();
foreach ($this->_streamsToBackup as $name => $stream) { foreach ($this->streams_to_backup as $name => $stream) {
$tmpStream[$name . '-' . date($format)] = $stream; $tmpStream[$name . '-' . date($format)] = $stream;
} }
$this->_streamsToBackup = $tmpStream; $this->streams_to_backup = $tmpStream;
return $this; return $this;
} }
/** /**
* @param $name * Set option name
*
* @param mixed $name option's name
* *
* @throws \Exception * @throws \Exception
* *

View File

@ -16,7 +16,7 @@ class Files extends BackupAbstract
$filesToBackup = $config['files']; $filesToBackup = $config['files'];
if(!empty($filesToBackup) && is_array($filesToBackup)){ if(!empty($filesToBackup) && is_array($filesToBackup)){
$names = array_map("basename",$filesToBackup); $names = array_map("basename",$filesToBackup);
$this->_filesToBackup = array_combine($filesToBackup,$names); $this->files_to_backup = array_combine($filesToBackup,$names);
} }
parent::__construct($config); parent::__construct($config);
} }
@ -31,7 +31,7 @@ class Files extends BackupAbstract
public function isValid() public function isValid()
{ {
$result = true; $result = true;
foreach ($this->_filesToBackup as $file => $name) { foreach ($this->files_to_backup as $file => $name) {
if (!file_exists($file)) { if (!file_exists($file)) {
$result = false; $result = false;
break; break;

View File

@ -4,7 +4,6 @@ namespace Shikiryu\Backup\Backup;
class Folder extends BackupAbstract class Folder extends BackupAbstract
{ {
public function __construct(array $config = array()) public function __construct(array $config = array())
{ {
parent::__construct($config); parent::__construct($config);

View File

@ -22,7 +22,8 @@ class Mysql extends BackupAbstract
/** /**
* @param array $config * @param array $config
*/ */
public function __construct(array $config = array()) { public function __construct(array $config = array())
{
parent::__construct($config); parent::__construct($config);
} }
@ -35,7 +36,7 @@ class Mysql extends BackupAbstract
if (!empty($tables)) { if (!empty($tables)) {
$this->tables = $tables; $this->tables = $tables;
} }
$this->_streamsToBackup[sprintf('db-%s.sql', $this->database)] = $this->getFromTables(); $this->streams_to_backup[sprintf('db-%s.sql', $this->database)] = $this->getFromTables();
return $this; return $this;
} }
@ -44,12 +45,13 @@ class Mysql extends BackupAbstract
* *
* @return $this * @return $this
*/ */
private function everything() { private function everything()
{
$this->tables = array(); $this->tables = array();
foreach ($this->pdo->query('SHOW TABLES') as $table) { foreach ($this->pdo->query('SHOW TABLES') as $table) {
$this->tables[] = $table; $this->tables[] = $table;
} }
$this->_streamsToBackup[sprintf('db-%s.sql', $this->database)] = $this->getFromTables(); $this->streams_to_backup[sprintf('db-%s.sql', $this->database)] = $this->getFromTables();
return $this; return $this;
} }
@ -97,7 +99,7 @@ class Mysql extends BackupAbstract
*/ */
public function isValid() public function isValid()
{ {
return !empty($this->_streamsToBackup); return !empty($this->streams_to_backup);
} }
/** /**
@ -124,5 +126,3 @@ class Mysql extends BackupAbstract
empty($this->tables) || $this->tables == '*' ? $this->everything() : $this->fromTables($this->tables); empty($this->tables) || $this->tables == '*' ? $this->everything() : $this->fromTables($this->tables);
} }
} }
?>