Completed
Pull Request — master (#602)
by Sean
04:30 queued 01:15
created

DemoDeploymentBackend   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A planDeploy() 0 3 1
B deploy() 0 34 2
A getDeployOptions() 0 3 1
A dataTransfer() 0 3 1
A enableMaintenance() 0 3 1
A disableMaintenance() 0 3 1
A ping() 0 3 1
1
<?php
2
3
/**
4
 * This demo back-end doesn't actually do deployment.
5
 *
6
 * Whenever you deploy, it will track the deployment history in a text file assets/<environment>.deploy-history.txt
7
 *
8
 * It's useful for demonstrating how the system works, and how you can write deployment back-ends
9
 */
10
class DemoDeploymentBackend extends Object implements DeploymentBackend {
11
12
	/**
13
	 * Create a deployment strategy.
14
	 *
15
	 * @param DNEnvironment $environment
16
	 * @param array $options
17
	 *
18
	 * @return DeploymentStrategy
19
	 */
20
	public function planDeploy(DNEnvironment $environment, $options = array()) {
21
		return new DeploymentStrategy($environment, $options);
22
	}
23
24
	/**
25
	 * Deploy the given build to the given environment
26
	 *
27
	 * @param DNEnvironment $environment
28
	 * @param DeploynautLogFile $log
29
	 * @param DNProject $project
30
	 * @param array $options
31
	 */
32
	public function deploy(
33
		DNEnvironment $environment,
34
		DeploynautLogFile $log,
35
		DNProject $project,
36
		$options
37
	) {
38
		$sha = $options['sha'];
39
40
		$this->extend('deployStart', $environment, $sha, $log, $project);
41
42
		$file = sprintf('%s/%s.deploy-history.txt', DEPLOYNAUT_LOG_PATH, $environment->getFullName());
43
		$CLI_file = escapeshellarg($file);
44
		$CLI_line = escapeshellarg(date('Y-m-d H:i:s') . " => $sha");
45
46
		// Put maintenance page up
47
		$this->enableMaintenance($environment, $log, $project);
48
49
		// Do the deployment
50
		$log->write("Demo deployment: echo $CLI_line >> $CLI_file");
51
		`echo $CLI_line >> $CLI_file`;
52
		$log->write("Arbitrary pause for 10s");
53
		sleep(10);
54
		$log->write("Well, that was a waste of time");
55
56
		// Once the deployment has run it's necessary to update the maintenance page status
57
		if(!empty($options['leaveMaintenancePage'])) {
58
			$this->enableMaintenance($environment, $log, $project);
59
		} else {
60
			// Remove maintenance page if we want it to
61
			$this->disableMaintenance($environment, $log, $project);
62
		}
63
64
		$this->extend('deployEnd', $environment, $sha, $log, $project);
65
	}
66
67
	/**
68
	 * @return array
69
	 */
70
	public function getDeployOptions() {
71
		return [];
72
	}
73
74
	/**
75
	 * @inheritdoc
76
	 */
77
	public function dataTransfer(DNDataTransfer $dataTransfer, DeploynautLogFile $log) {
78
		die('Not implemented');
79
	}
80
81
	public function enableMaintenance(DNEnvironment $environment, \DeploynautLogFile $log, DNProject $project) {
82
		$log->write(sprintf('Maintenance page enabled on "%s"', $environment->getFullName()));
83
	}
84
85
	public function disableMaintenance(DNEnvironment $environment, DeploynautLogFile $log, DNProject $project) {
86
		$log->write(sprintf('Maintenance page disabled on "%s"', $environment->getFullName()));
87
	}
88
89
	public function ping(\DNEnvironment $environment, DeploynautLogFile $log, DNProject $project) {
90
		$log->write(sprintf('Ping "%s"', $environment->getFullName()));
91
	}
92
93
}
94