🎨 Apply PSR2

This commit is contained in:
Clement Desmidt 2019-01-25 21:39:18 +01:00
parent ab7887ce84
commit db51375185
4 changed files with 103 additions and 100 deletions

View File

@ -125,7 +125,8 @@ class Email extends TransportAbstract
* *
* @return string * @return string
*/ */
public static function getMimeType($file) { public static function getMimeType($file)
{
if (function_exists('finfo_open')) { if (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE); $finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = (string) finfo_file($finfo, $file); $type = (string) finfo_file($finfo, $file);
@ -135,17 +136,19 @@ class Email extends TransportAbstract
return mime_content_type($file); return mime_content_type($file);
} else { } else {
$ext = strtolower(array_pop(explode('.', $file))); $ext = strtolower(array_pop(explode('.', $file)));
if (array_key_exists($ext, self::$mimeTypes)) if (array_key_exists($ext, self::$mimeTypes)) {
return self::$mimeTypes[$ext]; return self::$mimeTypes[$ext];
else } else {
return 'application/octet-stream'; return 'application/octet-stream';
}
} }
} }
/** /**
* @param BackupAbstract $backup * @param BackupAbstract $backup
*/ */
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());
@ -157,11 +160,11 @@ class Email extends TransportAbstract
$this->encoding = $this->config['encoding']; $this->encoding = $this->config['encoding'];
} }
/** /**
* @param array $files * @param array $files
* *
* @return $this * @return $this
*/ */
private function setFiles($files = array()) private function setFiles($files = array())
{ {
if (is_array($files) && !empty($files)) { if (is_array($files) && !empty($files)) {
@ -170,11 +173,11 @@ class Email extends TransportAbstract
return $this; return $this;
} }
/** /**
* @param array $streams * @param array $streams
* *
* @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)) {
@ -194,23 +197,22 @@ class Email extends TransportAbstract
{ {
// TODO check if file is empty // TODO check if file is empty
// Checking files are selected // Checking files are selected
$zip = new \ZipArchive(); // Load zip library $zip = new \ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name $zip_name = time().".zip"; // Zip name
if ($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::CREATE)==TRUE) { if ($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::CREATE)==true) {
if(!empty($this->files)) { if (!empty($this->files)) {
foreach($this->files as $file => $name) foreach ($this->files as $file => $name) {
{
$zip->addFile($file, $name); // Adding files into zip $zip->addFile($file, $name); // Adding files into zip
} }
} }
$zip->close(); $zip->close();
} else { } else {
throw new \Exception('Transport::Email::Can\'t zip the given backup.'); throw new \Exception('Transport::Email::Can\'t zip the given backup.');
} }
$this->files = array(TEMP_DIR.$zip_name=>$zip_name); $this->files = array(TEMP_DIR.$zip_name=>$zip_name);
$random_hash = md5(date('r', time())); $random_hash = md5(date('r', time()));
$headers = "From: " . $this->email_from . "\r\nReply-To: " . $this->email_from; $headers = "From: " . $this->email_from . "\r\nReply-To: " . $this->email_from;
@ -223,30 +225,33 @@ Content-Transfer-Encoding: 8bit
" . $this->message . "\r\n"; " . $this->message . "\r\n";
if(!empty($this->files)) if (!empty($this->files)) {
foreach($this->files as $file => $name) { foreach ($this->files as $file => $name) {
$output .= " $output .= "
--$random_hash --$random_hash
Content-Type: " . self::getMimeType($file) . "; name=" . $name . " Content-Type: " . self::getMimeType($file) . "; name=" . $name . "
Content-Transfer-Encoding: base64 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)) if (!empty($this->streams)) {
foreach($this->streams as $name=>$stream) { foreach ($this->streams as $name => $stream) {
if(count(explode('.',$name))<2) $name = 'backup'.$name.'.txt'; if (count(explode('.', $name))<2) {
$output .= " $name = 'backup'.$name.'.txt';
}
$output .= "
--$random_hash --$random_hash
Content-Type: text/plain; name=" . $name . " Content-Type: text/plain; name=" . $name . "
Content-Transfer-Encoding: base64 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--"; $output.="--$random_hash--";
return mail($this->email_to, $this->subject, $output, $headers); return mail($this->email_to, $this->subject, $output, $headers);
} }
} }

View File

@ -6,42 +6,42 @@ use Shikiryu\Backup\Backup\BackupAbstract;
class Folder extends TransportAbstract class Folder extends TransportAbstract
{ {
/** /**
* Folder to backup * Folder to backup
* *
* @var string * @var string
*/ */
protected $folder; protected $folder;
public function __construct(BackupAbstract $backup, array $config = array()) public function __construct(BackupAbstract $backup, array $config = array())
{ {
parent::__construct($backup, $config); parent::__construct($backup, $config);
if (!empty($this->folder)) { if (!empty($this->folder)) {
$this->folder = sprintf('%s/', rtrim($this->folder, '/')); $this->folder = sprintf('%s/', rtrim($this->folder, '/'));
} }
} }
/** /**
* @return bool * @return bool
* *
* @throws \Exception * @throws \Exception
*/ */
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';
} }
if (file_put_contents($this->folder . $name, $file) === false) { if (file_put_contents($this->folder . $name, $file) === false) {
throw new \Exception(sprintf('Saving of %s in %s failed', $name, $this->folder)); throw new \Exception(sprintf('Saving of %s in %s failed', $name, $this->folder));
} }
} }
return true; return true;
} }
} }

View File

@ -28,7 +28,7 @@ abstract class BackupAbstract
* *
* @param array $config array of options and parameters * @param array $config array of options and parameters
*/ */
function __construct($config = array()) public function __construct($config = array())
{ {
$this->options = !empty($config['options']) ? $config['options'] : []; $this->options = !empty($config['options']) ? $config['options'] : [];
@ -45,7 +45,7 @@ abstract class BackupAbstract
* *
* @param string $name attribute name * @param string $name attribute name
* @param mixed $value attribute value * @param mixed $value attribute value
* *
* @return $this * @return $this
*/ */
public function __set($name, $value) public function __set($name, $value)
@ -99,7 +99,7 @@ abstract class BackupAbstract
/** /**
* Initialize everything * Initialize everything
* *
* @return $this * @return $this
*/ */
protected function init() protected function init()
@ -114,21 +114,21 @@ abstract class BackupAbstract
/** /**
* Function that can be used to initialize the backup * Function that can be used to initialize the backup
* *
* @return void * @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 * @return void
*/ */
abstract protected function postBuild(); abstract protected function postBuild();
/** /**
* Mandatory function doing the backup * Mandatory function doing the backup
* *
* @return void * @return void
*/ */
abstract protected function build(); abstract protected function build();
@ -142,7 +142,7 @@ abstract class BackupAbstract
/** /**
* Apply options * Apply options
* *
* @return $this * @return $this
*/ */
protected function applyOptions() protected function applyOptions()
@ -174,7 +174,7 @@ abstract class BackupAbstract
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->files_to_backup 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
} }
@ -257,7 +257,7 @@ abstract class BackupAbstract
/** /**
* Set option name * Set option name
* *
* @param mixed $name option's name * @param mixed $name option's name
* *
* @throws \Exception * @throws \Exception
@ -270,5 +270,4 @@ abstract class BackupAbstract
throw new \Exception('name option is for zip only.'); throw new \Exception('name option is for zip only.');
} }
} }
} }

View File

@ -14,20 +14,20 @@ class Files extends BackupAbstract
throw new \Exception('Files needs a "files" configuration.'); throw new \Exception('Files needs a "files" configuration.');
} }
$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->files_to_backup = array_combine($filesToBackup,$names); $this->files_to_backup = array_combine($filesToBackup, $names);
} }
parent::__construct($config); parent::__construct($config);
} }
/** /**
* Check if the backup is valid * Check if the backup is valid
* *
* @return bool * @return bool
* *
* @SuppressWarnings("unused") * @SuppressWarnings("unused")
*/ */
public function isValid() public function isValid()
{ {
$result = true; $result = true;
@ -39,29 +39,28 @@ class Files extends BackupAbstract
} }
return $result; return $result;
} }
/** /**
* Function that can be used to initialize the backup * Function that can be used to initialize the backup
*/ */
protected function preBuild() protected function preBuild()
{ {
// TODO: Implement preBuild() method. // TODO: Implement preBuild() method.
} }
/** /**
* Function that can be used after the backup * Function that can be used after the backup
*/ */
protected function postBuild() protected function postBuild()
{ {
// TODO: Implement postBuild() method. // TODO: Implement postBuild() method.
} }
/** /**
* Mandatory function doing the backup * Mandatory function doing the backup
*/ */
protected function build() protected function build()
{ {
// TODO: Implement build() method. // TODO: Implement build() method.
} }
} }
?>