Skip to content

Instantly share code, notes, and snippets.

@cgsmith
Created July 26, 2015 23:48
Show Gist options
  • Select an option

  • Save cgsmith/ebe128362cf4675d9e51 to your computer and use it in GitHub Desktop.

Select an option

Save cgsmith/ebe128362cf4675d9e51 to your computer and use it in GitHub Desktop.
<?php
class TransferHandler
{
/**
* @var string ftp host
*/
protected $targetFTPHost = "sftp.customer.com";
/**
* @var string ftp user
*/
protected $targetFTPUser = "user1";
/**
* @var string ftp password
*/
protected $targetFTPPass = 'hi';
/**
* @var string ftp destination folder
*/
protected $targetFTPDestinationFolder = 'to-synx';
/**
* @var string where to rsync the files
*/
protected $transferDestination = '/';
/**
* @var string imagePath
*/
protected $imagePath = null;
/**
* @var string manifestPath
*/
protected $manifestPath = null;
/**
* @var bool useSFTP determines if files should transmit via SFTP
*/
protected $useSFTP = null;
/**
* @var string list of comma separated emails to send confirmation to
*/
public $emailAddress = 'email@mrs.com';
/**
* @var string email from name
*/
protected $emailFromName = '**MRS Script**';
/**
* @var string from email that is sent out
*/
protected $emailFrom = 'it@mrs.com';
/**
* @const METHOD_SFTP 100 used in setMethod()
*/
const METHOD_SFTP = 100;
public function sendFiles()
{
return ($this->useSFTP) ? $this->secftpUWPackage() : $this->syncPayloads();
}
/**
* Uses bin/syncPayloads, which just aliases rsync, to sync the files to destination
* @todo Use a better method that can verify successful sync
*/
protected function syncPayloads()
{
system("syncPayloads $this->manifestPath $this->transferDestination");
system("syncPayloads $this->imagePath $this->transferDestination");
return true;
}
/**
* This function leverages built in PHP support for SSH to SCP the document payload to the remote server.
* @return bool
* @throws Exception
* @todo remove the Net_SFTP dependency ^_^ - cgsmith
*/
protected static function secftpUWPackage()
{
define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);
$sftpConnection = new Net_SFTP($this->targetFTPHost);
if (!$sftpConnection->login($this->targetFTPUser, $this->targetFTPPass)) {
throw new Exception('SFTP Login Failed on transferGreatWestLife.php');
}
return $sftpConnection->put(
$this->targetFTPDestinationFolder . '/' . strtolower(basename($this->imagePath)),
$this->imagePath,
NET_SFTP_LOCAL_FILE);
}
/**
* @param $subject
* @param $body
* @return bool
*/
public static function phpEmail($subject, $body)
{
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/class.phpmailer.php");
$mail = new PHPMailer();
$mail->AddAddress($this->emailAddresses);
$mail->FromName($this->emailFromName);
$mail->From = $this->emailFrom;
$mail->Sender = $this->emailFrom;
$mail->AddReplyTo($this->emailFrom);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->WordWrap = 200;
return $mail->Send();
}
/**
* Verifies files exists and sets protected variables for other methods
*
* @param $imagePath
* @param $manifestPath
* @return bool
* @throws Exception
*/
public function verifyFiles($imagePath, $manifestPath)
{
if (is_null($this->useSFTP)) {
throw new Exception('setMethod() must be called first to determined verifyFiles()');
}
if ($this->useSFTP) {
$this->imagePath = $imagePath;
return is_file($imagePath);
} else {
$this->imagePath = $imagePath;
$this->manifestPath = $manifestPath;
return is_file($imagePath) && is_file($manifestPath);
}
}
/**
* Tells other methods to utilize SFTP if count is higher than constant
*
* @param $count
*/
public function setMethod($count)
{
$this->useSFTP = ($count > self::METHOD_SFTP) ? true : false;
}
/**
* @return bool
*/
public function getMethod()
{
return $this->useSFTP;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment