| 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 |  |  | 		// This is a bit icky, but there is no easy way of capturing a failed run by using the PHP Resque | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 			// Disallow concurrent jobs (don't rely on queuing implementation to restrict this) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 			// Only consider data transfers started in the last 30 minutes (older jobs probably got stuck) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 			$runningTransfers = DNDataTransfer::get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 				->filter(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 					'EnvironmentID' => $environment->ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 					'Status' => array('Queued', 'Started'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 					'Created:GreaterThan' => strtotime('-30 minutes') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 				)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 				->exclude('ID', $dataTransfer->ID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 41 |  | View Code Duplication | 			if($runningTransfers->count()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 				$runningTransfer = $runningTransfers->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 				$log->write(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 					'[-] Error: another transfer is in progress (started at %s by %s)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 					$runningTransfer->dbObject('Created')->Nice(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 					$runningTransfer->Author()->Title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 				)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 				throw new RuntimeException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 					'Another transfer is in progress (started at %s by %s)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 					$runningTransfer->dbObject('Created')->Nice(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 					$runningTransfer->Author()->Title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 				)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			// before we push data to an environment, we'll make a backup first | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			if($backupDataTransfer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 				$log->write('Backing up existing data'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 				$environment->Backend()->dataTransfer( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 					$backupDataTransfer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 					$log | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 				); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			$environment->Backend()->dataTransfer( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 				$dataTransfer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 				$log | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		} catch(RuntimeException $exc) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			$log->write($exc->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			echo "[-] DataTransferJob failed" . PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			throw $exc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		$this->updateStatus('Finished'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		echo "[-] DataTransferJob finished" . PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 * @param string $status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 * @global array $databaseConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	protected function updateStatus($status) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		global $databaseConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		DB::connect($databaseConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		$env = DNDataTransfer::get()->byID($this->args['dataTransferID']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$env->Status = $status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		$env->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		$backup = $env->BackupDataTransfer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		if($backup && $backup->exists()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			$backup->Status = $status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			$backup->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * @return DNData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	protected function DNData() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		return DNData::inst(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  | 
            
                        
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.