Completed
Pull Request — master (#602)
by Sean
14:12 queued 10:30
created

DataTransferJob   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 29.09 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 13
c 4
b 1
f 0
lcom 1
cbo 8
dl 32
loc 110
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
B perform() 22 63 7
A updateStatus() 0 7 1
A updateBackupStatus() 10 10 3
A DNData() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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
54
			if($runningTransfers->count()) {
55
				$runningTransfer = $runningTransfers->first();
56
				$message = sprintf(
57
					'Error: another transfer is in progress (started at %s by %s)',
58
					$runningTransfer->dbObject('Created')->Nice(),
59
					$runningTransfer->Author()->Title
60
				);
61
				$log->write($message);
62
				throw new \RuntimeException($message);
63
			}
64
65 View Code Duplication
			if($backupDataTransfer) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
66
				$log->write('Backing up existing data');
67
				try {
68
					$environment->Backend()->dataTransfer($backupDataTransfer, $log);
69
					$this->updateBackupStatus('Finished');
70
				} catch(Exception $e) {
71
					$this->updateBackupStatus('Failed');
72
					throw $e;
73
				}
74
			}
75
76
			$environment->Backend()->dataTransfer($dataTransfer, $log);
77
		} catch(Exception $e) {
78
			echo "[-] DataTransferJob failed" . PHP_EOL;
79
			throw $e;
80
		}
81
82
		$this->updateStatus('Finished');
83
		echo "[-] DataTransferJob finished" . PHP_EOL;
84
	}
85
86
	/**
87
	 * @param string $status
88
	 * @global array $databaseConfig
89
	 */
90
	protected function updateStatus($status) {
91
		global $databaseConfig;
92
		DB::connect($databaseConfig);
93
		$transfer = DNDataTransfer::get()->byID($this->args['dataTransferID']);
94
		$transfer->Status = $status;
95
		$transfer->write();
96
	}
97
98
	/**
99
	 * @param string $status Status of DNDataTransfer
100
	 * @global array $databaseConfig
101
	 */
102 View Code Duplication
	protected function updateBackupStatus($status) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
103
		global $databaseConfig;
104
		DB::connect($databaseConfig);
105
		$deployment = DNDeployment::get()->byID($this->args['deploymentID']);
106
		$backup = $deployment->BackupDataTransfer();
107
		if($backup && $backup->exists()) {
108
			$backup->Status = $status;
109
			$backup->write();
110
		}
111
	}
112
113
	/**
114
	 * @return DNData
115
	 */
116
	protected function DNData() {
117
		return DNData::inst();
118
	}
119
}
120