🎨 Reformat some code

This commit is contained in:
Clement Desmidt 2019-01-25 22:19:50 +01:00
parent db51375185
commit 43e7dd2808
10 changed files with 63 additions and 52 deletions

View File

@ -54,7 +54,7 @@ class Scenario
if ($this->backup->isValid()) {
$this->transport->send();
} else {
throw new \Exception("Backup configuration is invalid.");
throw new \Exception('Backup configuration is invalid.');
}
}
@ -75,7 +75,7 @@ class Scenario
// check the given scenario
if (is_readable($scenario)) {
$scenario = json_decode(file_get_contents($scenario), true);
if (!is_null($scenario) && static::isValid($scenario)) {
if ($scenario !== null && static::isValid($scenario)) {
try {
$scenario = new self($scenario);
$scenario->send();
@ -99,9 +99,8 @@ class Scenario
public static function isValid(array $scenario)
{
return
isset($scenario['backup']) &&
isset($scenario['backup'], $scenario['transport']) &&
count($scenario['backup']) === 1 &&
isset($scenario['transport']) &&
count($scenario['transport']) === 1;
}
}

View File

@ -30,21 +30,25 @@ class Dropbox extends TransportAbstract
public function send()
{
$sent = true;
$files = $this->backup->getFilesTobackup();
$files = $this->backup->getFilesToBackup();
foreach ($files as $file => $name) {
$file = fopen($file, 'r');
$file = fopen($file, 'rb');
$upload = $this->dropbox->uploadFile($this->folder.'/'.$name, \Dropbox\WriteMode::force(), $file);
if (!$upload) {
$sent = false;
echo 'DROPBOX upload manqu<71>e de '.$file.' vers '.$this->folder.$name;
}
}
$streams = $this->backup->getStreamsTobackup();
$streams = $this->backup->getStreamsToBackup();
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) {
$sent = false;
echo 'DROPBOX upload manqu<71>e de '.$file.' vers '.$this->folder.$name;
echo 'DROPBOX upload manquée de ' . $file . ' vers '.$this->folder.$name;
}
}
return $sent;

View File

@ -132,17 +132,20 @@ class Email extends TransportAbstract
$type = (string) finfo_file($finfo, $file);
finfo_close($finfo);
return $type;
} else if (function_exists('mime_content_type')) {
}
if (function_exists('mime_content_type')) {
return mime_content_type($file);
} else {
$ext = strtolower(array_pop(explode('.', $file)));
}
$file_info = explode('.', $file);
$ext = strtolower(array_pop($file_info));
if (array_key_exists($ext, self::$mimeTypes)) {
return self::$mimeTypes[$ext];
} else {
}
return 'application/octet-stream';
}
}
}
/**
* @param BackupAbstract $backup
@ -150,8 +153,8 @@ class Email extends TransportAbstract
public function __construct(BackupAbstract $backup, array $config)
{
parent::__construct($backup, $config);
$this->setFiles($this->backup->getFilesTobackup());
$this->setStreams($this->backup->getStreamsTobackup());
$this->setFiles($this->backup->getFilesToBackup());
$this->setStreams($this->backup->getStreamsToBackup());
$this->email_to = $this->config['to'];
$this->email_from = $this->config['from'];
$this->encoding = $this->config['encoding'];
@ -200,8 +203,8 @@ class Email extends TransportAbstract
// Checking files are selected
$zip = new \ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name
if ($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::CREATE)==true) {
$zip_name = time(). '.zip'; // Zip name
if ($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::CREATE)===true) {
if (!empty($this->files)) {
foreach ($this->files as $file => $name) {
$zip->addFile($file, $name); // Adding files into zip
@ -214,9 +217,9 @@ class Email extends TransportAbstract
$this->files = array(TEMP_DIR.$zip_name=>$zip_name);
$random_hash = md5(date('r', time()));
$headers = "From: " . $this->email_from . "\r\nReply-To: " . $this->email_from;
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"" . $random_hash . "\"";
$random_hash = md5(date('r'));
$headers = 'From: ' . $this->email_from . "\r\nReply-To: " . $this->email_from;
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"" . $random_hash . '"';
$output = "
--$random_hash
@ -229,26 +232,26 @@ Content-Transfer-Encoding: 8bit
foreach ($this->files as $file => $name) {
$output .= "
--$random_hash
Content-Type: " . self::getMimeType($file) . "; name=" . $name . "
Content-Type: " . self::getMimeType($file) . '; name=' . $name . '
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=" . $name . "
Content-Disposition: attachment; filename=' . $name . '
" . chunk_split(base64_encode(file_get_contents($file)));
' . chunk_split(base64_encode(file_get_contents($file)));
}
}
if (!empty($this->streams)) {
foreach ($this->streams as $name => $stream) {
if (count(explode('.', $name))<2) {
if (substr_count($name, '.') + 1 <2) {
$name = 'backup'.$name.'.txt';
}
$output .= "
--$random_hash
Content-Type: text/plain; name=" . $name . "
Content-Type: text/plain; name=" . $name . '
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=" . $name . "
Content-Disposition: attachment; filename=' . $name . '
" . chunk_split(base64_encode($stream));
' . chunk_split(base64_encode($stream));
}
}
$output.="--$random_hash--";

View File

@ -25,7 +25,7 @@ class Ftp extends TransportAbstract
}
$this->connection = ftp_connect($this->host);
if ($this->connection == false) {
if ($this->connection === false) {
throw new \Exception(sprintf('I can\'t connect to the FTP %s', $this->host));
}
@ -35,8 +35,8 @@ class Ftp extends TransportAbstract
throw new \Exception($msg);
}
$this->setFiles($this->backup->getFilesTobackup());
$this->setStreams($this->backup->getStreamsTobackup());
$this->setFiles($this->backup->getFilesToBackup());
$this->setStreams($this->backup->getStreamsToBackup());
}
private function setFiles($files = array())
@ -57,6 +57,7 @@ class Ftp extends TransportAbstract
/**
* @return bool
* @throws \Exception
*/
public function send()
{
@ -74,7 +75,7 @@ class Ftp extends TransportAbstract
if (!empty($this->streams)) {
foreach ($this->streams as $name => $stream) {
if (count(explode('.', $name)) < 2) {
if (substr_count($name, '.') + 1 < 2) {
$name = 'backup' . $name . '.txt';
}
file_put_contents($name, $stream);

View File

@ -29,13 +29,13 @@ class Folder extends TransportAbstract
*/
public function send()
{
foreach ($this->backup->getFilesTobackup() as $file => $name) {
foreach ($this->backup->getFilesToBackup() as $file => $name) {
if (copy($file, $this->folder . $name) === false) {
throw new \Exception(sprintf('Copy of %s in %s failed', $name, $this->folder));
};
}
foreach ($this->backup->getStreamsTobackup() as $name => $file) {
if (count(explode('.', $name)) < 2) {
foreach ($this->backup->getStreamsToBackup() as $name => $file) {
if (substr_count($name, '.') + 1 < 2) {
$name = 'backup' . $name . '.txt';
}
if (file_put_contents($this->folder . $name, $file) === false) {

View File

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

View File

@ -151,7 +151,7 @@ abstract class BackupAbstract
foreach ($this->options as $name => $value) {
$method = sprintf('setOption%s', ucfirst($name));
if (method_exists($this, $method)) {
call_user_func([$this, $method], $value);
$this->$method($value);
}
}
@ -174,7 +174,7 @@ abstract class BackupAbstract
if (touch(TEMP_DIR . $zip_name) === false) {
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->files_to_backup as $file => $name) {
$zip->addFile($file, $name); // Adding files into zip
}

View File

@ -15,7 +15,7 @@ class Files extends BackupAbstract
}
$filesToBackup = $config['files'];
if (!empty($filesToBackup) && is_array($filesToBackup)) {
$names = array_map("basename", $filesToBackup);
$names = array_map('basename', $filesToBackup);
$this->files_to_backup = array_combine($filesToBackup, $names);
}
parent::__construct($config);

View File

@ -60,17 +60,15 @@ class Mysql extends BackupAbstract
*/
private function getFromTables()
{
$return = "";
$return = '';
foreach ($this->tables as $table) {
if (is_array($table)) {
$table = $table[0];
}
$result = $this->pdo->prepare('SELECT * FROM ' . $table);
$result->execute();
$result = $this->pdo->query('SELECT * FROM ' . $table);
$num_fields = $result->columnCount();
$return .= 'DROP TABLE IF EXISTS ' . $table . ';';
$result2 = $this->pdo->prepare('SHOW CREATE TABLE ' . $table);
$result2->execute();
$result2 = $this->pdo->query('SHOW CREATE TABLE ' . $table);
$row2 = $result2->fetch();
$return.= "\n\n" . $row2[1] . ";\n\n";
foreach ($result as $row) {
@ -123,6 +121,6 @@ class Mysql extends BackupAbstract
*/
protected function build()
{
empty($this->tables) || $this->tables == '*' ? $this->everything() : $this->fromTables($this->tables);
empty($this->tables) || $this->tables === '*' ? $this->everything() : $this->fromTables($this->tables);
}
}

View File

@ -12,7 +12,13 @@
],
"require": {
"phpseclib/phpseclib": ">=1.0.0",
"dropbox/dropbox-sdk": "~1.1"
"dropbox/dropbox-sdk": "~1.1",
"ext-fileinfo": "*",
"ext-zip": "*",
"ext-ftp": "*",
"ext-pdo": "*",
"ext-mbstring": "*",
"ext-json": "*"
},
"autoload": {
"classmap": ["app/code/", "vendor/"]