DNCreateEnvironment::ResqueStatus()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 0
1
<?php
2
3
/**
4
 * DNCreateEnvironment
5
 *
6
 * @property string Data
7
 * @property string ResqueToken
8
 * @property string Status
9
 * @property bool IsInitialEnvironment
10
 *
11
 * @method DNProject Project()
12
 * @property int $ProjectID
13
 * @method Member Creator()
14
 * @property int $CreatorID
15
 */
16
class DNCreateEnvironment extends DataObject {
0 ignored issues
show
Coding Style introduced by
The property $has_one is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
17
18
	/**
19
	 * @var array
20
	 */
21
	private static $db = array(
22
		'Data' => 'Text',
23
		'ResqueToken' => 'Varchar(255)',
24
		"Status" => "Enum('Queued, Started, Finished, Failed, n/a', 'n/a')",
25
		'IsInitialEnvironment' => 'Boolean',
26
	);
27
28
	/**
29
	 * @var array
30
	 */
31
	private static $has_one = array(
32
		'Project' => 'DNProject',
33
		'Creator' => 'Member'
34
	);
35
36
	/**
37
	 *
38
	 * @param int $int
39
	 * @return string
40
	 */
41 View Code Duplication
	public static function map_resque_status($int) {
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...
42
		$remap = array(
43
			Resque_Job_Status::STATUS_WAITING => "Queued",
44
			Resque_Job_Status::STATUS_RUNNING => "Running",
45
			Resque_Job_Status::STATUS_FAILED => "Failed",
46
			Resque_Job_Status::STATUS_COMPLETE => "Complete",
47
			false => "Invalid",
48
		);
49
		return $remap[$int];
50
	}
51
52
	/**
53
	 * @return string
54
	 */
55
	public function Name() {
56
		$data = unserialize($this->Data);
57
		return !empty($data['Name']) ? Convert::raw2xml($data['Name']) : '';
58
	}
59
60
	/**
61
	 * @return string
62
	 */
63
	public function Link() {
64
		return Controller::join_links($this->Project()->Link(), 'createenv', $this->ID);
65
	}
66
67
	/**
68
	 * @return string
69
	 */
70
	public function LogLink() {
71
		return $this->Link() . '/log';
72
	}
73
74
	/**
75
	 * @return boolean
76
	 */
77
	public function canView($member = null) {
78
		return $this->Project()->canView($member);
79
	}
80
81
	/**
82
	 * Return a path to the log file.
83
	 * @return string
84
	 */
85
	protected function logfile() {
86
		return sprintf(
87
			'%s.createenv.%s.log',
88
			$this->Project()->Name,
89
			$this->ID
90
		);
91
	}
92
93
	/**
94
	 * @return \DeploynautLogFile
95
	 */
96
	public function log() {
97
		return Injector::inst()->createWithArgs('DeploynautLogFile', array($this->logfile()));
98
	}
99
100
	public function LogContent() {
101
		return $this->log()->content();
102
	}
103
104
	/**
105
	 * Returns the status of the resque job
106
	 *
107
	 * @return string
108
	 */
109 View Code Duplication
	public function ResqueStatus() {
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...
110
		$status = new Resque_Job_Status($this->ResqueToken);
0 ignored issues
show
Security Bug introduced by
It seems like $this->ResqueToken can also be of type false; however, Resque_Job_Status::__construct() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
111
		$statusCode = $status->get();
112
		// The Resque job can no longer be found, fallback to the DNDeployment.Status
113
		if($statusCode === false) {
114
			// Translate from the DNDeployment.Status to the Resque job status for UI purposes
115
			switch($this->Status) {
116
				case 'Finished':
117
					return 'Complete';
118
				case 'Started':
119
					return 'Running';
120
				default:
121
					return $this->Status;
122
			}
123
		}
124
		return self::map_resque_status($statusCode);
125
	}
126
127
	/**
128
	 * Start a resque job for this creation.
129
	 *
130
	 * @return string Resque token
131
	 */
132
	protected function enqueueCreation() {
133
		$project = $this->Project();
134
		$log = $this->log();
135
136
		$args = array(
137
			'createID' => $this->ID,
138
			'logfile' => $this->logfile(),
139
			'projectName' => $project->Name
140
		);
141
142
		if(!$this->CreatorID) {
143
			$this->CreatorID = Member::currentUserID();
144
		}
145
146
		if($this->CreatorID) {
147
			$creator = $this->Creator();
148
			$message = sprintf(
149
				'Environment creation for project %s initiated by %s (%s), with IP address %s',
150
				$project->Name,
151
				$creator->getName(),
152
				$creator->Email,
153
				Controller::curr()->getRequest()->getIP()
154
			);
155
			$log->write($message);
156
		}
157
158
		return Resque::enqueue('create', 'CreateEnvJob', $args, true);
159
	}
160
161
	public function start() {
162
		$log = $this->log();
163
		$token = $this->enqueueCreation();
164
		$this->ResqueToken = $token;
165
		$this->Status = 'Queued';
166
		$this->write();
167
168
		$message = sprintf('Environment creation queued as job %s', $token);
169
		$log->write($message);
170
	}
171
172
	public function createEnvironment() {
173
		$backend = $this->getBackend();
174
		if($backend) {
175
			return $backend->createEnvironment($this);
176
		}
177
		throw new Exception("Unable to find backend.");
178
	}
179
180
	/**
181
	 * Fetches the EnvironmentCreateBackend based on the EnvironmentType saved to this job.
182
	 *
183
	 * @return EnvironmentCreateBackend|null
184
	 * @throws Exception
185
	 */
186
	public function getBackend() {
187
		$data = unserialize($this->Data);
188
		if(isset($data['EnvironmentType']) && class_exists($data['EnvironmentType'])) {
189
			$env = Injector::inst()->get($data['EnvironmentType']);
190
			if($env instanceof EnvironmentCreateBackend) {
191
				return $env;
192
			} else {
193
				throw new Exception("Invalid backend: " . $data['EnvironmentType']);
194
			}
195
		}
196
		return null;
197
	}
198
}
199
200