| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Starts a capistrano script for either retrieving data from an environment, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * or pushing data into an environment. Initiated by {@link DNDataTransfer}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @package deploynaut | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @subpackage jobs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class DataTransferJob extends DeploynautJob { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	 * set by a resque worker | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	public $args = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	public function setUp() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		$this->updateStatus('Started'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		chdir(BASE_PATH); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	public function perform() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		echo "[-] DataTransferJob starting" . PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		$log = new DeploynautLogFile($this->args['logfile']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		$dataTransfer = DNDataTransfer::get()->byID($this->args['dataTransferID']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		$environment = $dataTransfer->Environment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		$backupDataTransfer = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 29 |  | View Code Duplication | 		if(!empty($this->args['backupBeforePush']) && $dataTransfer->Direction == 'push') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 			$backupDataTransfer = DNDataTransfer::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 			$backupDataTransfer->EnvironmentID = $environment->ID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 			$backupDataTransfer->Direction = 'get'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 			$backupDataTransfer->Mode = $dataTransfer->Mode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 			$backupDataTransfer->ResqueToken = $dataTransfer->ResqueToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			$backupDataTransfer->AuthorID = $dataTransfer->AuthorID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 			$backupDataTransfer->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 			$dataTransfer->BackupDataTransferID = $backupDataTransfer->ID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 			$dataTransfer->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		// This is a bit icky, but there is no easy way of capturing a failed run by using the PHP Resque | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			// Disallow concurrent jobs (don't rely on queuing implementation to restrict this) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 			// Only consider data transfers started in the last 30 minutes (older jobs probably got stuck) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			$runningTransfers = DNDataTransfer::get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 				->filter(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 					'EnvironmentID' => $environment->ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 					'Status' => array('Queued', 'Started'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 					'Created:GreaterThan' => strtotime('-30 minutes') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 				)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 				->exclude('ID', $dataTransfer->ID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			if($runningTransfers->count()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 				$runningTransfer = $runningTransfers->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 				$message = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 					'Error: another transfer is in progress (started at %s by %s)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 					$runningTransfer->dbObject('Created')->Nice(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 					$runningTransfer->Author()->Title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 				); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 				$log->write($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 				throw new \RuntimeException($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			$this->performBackup($backupDataTransfer, $log); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			$environment->Backend()->dataTransfer($dataTransfer, $log); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		} catch(Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			echo "[-] DataTransferJob failed" . PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$this->updateStatus('Finished'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		echo "[-] DataTransferJob finished" . PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 75 |  | View Code Duplication | 	protected function performBackup($backupDataTransfer, DeploynautLogFile $log) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		if (!$backupDataTransfer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$log->write('Backing up existing data'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			$dataTransfer->Environment()->Backend()->dataTransfer($backupDataTransfer, $log); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			global $databaseConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			DB::connect($databaseConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			$backupDataTransfer->Status = 'Finished'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			$backupDataTransfer->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		} catch(Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			global $databaseConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			DB::connect($databaseConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			$backupDataTransfer->Status = 'Failed'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			$backupDataTransfer->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * @param string $status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * @global array $databaseConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	protected function updateStatus($status) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		global $databaseConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		DB::connect($databaseConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		$transfer = DNDataTransfer::get()->byID($this->args['dataTransferID']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$transfer->Status = $status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		$transfer->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 * @return DNData | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  | 	 */ | 
            
                                                        
            
                                    
            
            
                | 111 |  |  | 	protected function DNData() { | 
            
                                                        
            
                                    
            
            
                | 112 |  |  | 		return DNData::inst(); | 
            
                                                        
            
                                    
            
            
                | 113 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.