Completed
Pull Request — master (#594)
by Mateusz
03:09
created

DeployJob   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 18.06 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 9
Bugs 1 Features 2
Metric Value
wmc 7
c 9
b 1
f 2
lcom 1
cbo 6
dl 13
loc 72
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
B perform() 13 42 3
A onFailure() 0 3 1
A updateStatus() 0 6 1
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
 * Runs a deployment via the most appropriate backend
5
 */
6
class DeployJob extends DeploynautJob {
7
8
	public function setUp() {
9
		parent::setUp();
10
		$this->updateStatus(DNDeployment::TR_DEPLOY);
11
	}
12
13
	public function perform() {
14
		echo "[-] DeployJob starting" . PHP_EOL;
15
		$log = new DeploynautLogFile($this->args['logfile']);
16
17
		$deployment = DNDeployment::get()->byID($this->args['deploymentID']);
18
		$environment = $deployment->Environment();
19
		$project = $environment->Project();
20
		// This is a bit icky, but there is no easy way of capturing a failed deploy by using the PHP Resque
21
		try {
22
			// Disallow concurrent deployments (don't rely on queuing implementation to restrict this)
23
			// Only consider deployments started in the last 30 minutes (older jobs probably got stuck)
24
			$runningDeployments = $environment->runningDeployments()->exclude('ID', $this->args['deploymentID']);
25 View Code Duplication
			if($runningDeployments->count()) {
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...
26
				$runningDeployment = $runningDeployments->first();
27
				$log->write(sprintf(
28
					'[-] Error: another deployment is in progress (started at %s by %s)',
29
					$runningDeployment->dbObject('Created')->Nice(),
30
					$runningDeployment->Deployer()->Title
31
				));
32
				throw new RuntimeException(sprintf(
33
					'Another deployment is in progress (started at %s by %s)',
34
					$runningDeployment->dbObject('Created')->Nice(),
35
					$runningDeployment->Deployer()->Title
36
				));
37
			}
38
39
			$environment->Backend($this)->deploy(
40
				$environment,
41
				$log,
42
				$project,
43
				// Pass all args to give the backend full visibility. These args also contain
44
				// all options from the DeploymentStrategy merged in, including sha.
45
				$this->args
46
			);
47
		} catch(Exception $e) {
48
			// DeploynautJob will automatically trigger onFailure.
49
			echo "[-] DeployJob failed" . PHP_EOL;
50
			throw $e;
51
		}
52
		$this->updateStatus(DNDeployment::TR_COMPLETE);
53
		echo "[-] DeployJob finished" . PHP_EOL;
54
	}
55
56
	public function onFailure(Exception $exception) {
57
		$this->updateStatus(DNDeployment::TR_FAIL);
58
	}
59
60
	/**
61
	 * @param string $status Transition
62
	 * @global array $databaseConfig
63
	 */
64
	protected function updateStatus($status) {
65
		global $databaseConfig;
66
		DB::connect($databaseConfig);
67
		$dnDeployment = DNDeployment::get()->byID($this->args['deploymentID']);
68
		$dnDeployment->getMachine()->apply($status);
69
	}
70
71
	/**
72
	 * @return DNData
73
	 */
74
	protected function DNData() {
75
		return DNData::inst();
76
	}
77
}
78