Code Duplication    Length = 28-30 lines in 2 locations

code/control/ApprovalsDispatcher.php 2 locations

@@ 88-117 (lines=30) @@
85
	 * @param \SS_HTTPRequest $request
86
	 * @return \SS_HTTPResponse
87
	 */
88
	public function submit(SS_HTTPRequest $request) {
89
		if ($request->httpMethod() !== 'POST') {
90
			return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);
91
		}
92
93
		$deployment = DNDeployment::get()->byId($request->postVar('id'));
94
		$errorResponse = $this->validateDeployment($deployment);
95
		if ($errorResponse instanceof \SS_HTTPResponse) {
96
			return $errorResponse;
97
		}
98
99
		// todo set approver id from request, and if the approver has changed
100
		// we have to re-transition to TR_SUBMIT so the new approver gets
101
		// emailed from the state transition event handler
102
103
		try {
104
			$deployment->getMachine()->apply(DNDeployment::TR_SUBMIT);
105
		} catch (\Exception $e) {
106
			return $this->getAPIResponse([
107
				'status' => 'FAILED',
108
				'message' => $e->getMessage()
109
			], 400);
110
		}
111
112
		return $this->getAPIResponse([
113
			'status' => 'OK',
114
			'id' => $deployment->ID,
115
			'new_status' => DNDeployment::STATE_SUBMITTED
116
		], 200);
117
	}
118
119
	/**
120
	 * @param \SS_HTTPRequest $request
@@ 123-150 (lines=28) @@
120
	 * @param \SS_HTTPRequest $request
121
	 * @return \SS_HTTPResponse
122
	 */
123
	public function cancel(SS_HTTPRequest $request) {
124
		if ($request->httpMethod() !== 'POST') {
125
			return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);
126
		}
127
128
		$deployment = DNDeployment::get()->byId($request->postVar('id'));
129
		$errorResponse = $this->validateDeployment($deployment);
130
		if ($errorResponse instanceof \SS_HTTPResponse) {
131
			return $errorResponse;
132
		}
133
134
		// @todo permission checking for cancelling an approval request
135
136
		try {
137
			$deployment->getMachine()->apply(DNDeployment::TR_NEW);
138
		} catch (\Exception $e) {
139
			return $this->getAPIResponse([
140
				'status' => 'FAILED',
141
				'message' => $e->getMessage()
142
			], 400);
143
		}
144
145
		return $this->getAPIResponse([
146
			'status' => 'OK',
147
			'id' => $deployment->ID,
148
			'new_status' => DNDeployment::STATE_NEW
149
		], 200);
150
	}
151
152
	/**
153
	 * @param \SS_HTTPRequest $request