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 |
||
7 | class DeployDispatcher extends Dispatcher { |
||
8 | |||
9 | const ACTION_DEPLOY = 'deploys'; |
||
10 | |||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private static $allowed_actions = [ |
||
15 | 'history', |
||
16 | 'upcoming', |
||
17 | 'currentbuild', |
||
18 | 'show', |
||
19 | 'log', |
||
20 | 'create', |
||
21 | 'createdeployment' |
||
22 | ]; |
||
23 | |||
24 | private static $dependencies = [ |
||
25 | 'formatter' => '%$DeploynautAPIFormatter' |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * @var \DNProject |
||
30 | */ |
||
31 | protected $project = null; |
||
32 | |||
33 | /** |
||
34 | * @var \DNEnvironment |
||
35 | */ |
||
36 | protected $environment = null; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | private static $action_types = [ |
||
42 | self::ACTION_DEPLOY |
||
43 | ]; |
||
44 | |||
45 | View Code Duplication | public function init() { |
|
46 | parent::init(); |
||
47 | |||
48 | $this->project = $this->getCurrentProject(); |
||
49 | |||
50 | if (!$this->project) { |
||
51 | return $this->project404Response(); |
||
52 | } |
||
53 | |||
54 | // Performs canView permission check by limiting visible projects |
||
55 | $this->environment = $this->getCurrentEnvironment($this->project); |
||
56 | if (!$this->environment) { |
||
57 | return $this->environment404Response(); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param \SS_HTTPRequest $request |
||
63 | * @return \HTMLText|\SS_HTTPResponse |
||
64 | */ |
||
65 | public function index(\SS_HTTPRequest $request) { |
||
66 | return $this->redirect(\Controller::join_links($this->Link(), 'history'), 302); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param \SS_HTTPRequest $request |
||
71 | * @return \SS_HTTPResponse |
||
72 | */ |
||
73 | View Code Duplication | public function history(SS_HTTPRequest $request) { |
|
74 | $data = []; |
||
75 | |||
76 | $list = $this->environment->DeployHistory('DeployStarted'); |
||
77 | |||
78 | foreach ($list as $deployment) { |
||
79 | $data[] = $this->formatter->getDeploymentData($deployment); |
||
80 | } |
||
81 | |||
82 | return $this->getAPIResponse([ |
||
83 | 'list' => $data, |
||
84 | ], 200); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param \SS_HTTPRequest $request |
||
89 | * @return \SS_HTTPResponse |
||
90 | */ |
||
91 | View Code Duplication | public function upcoming(SS_HTTPRequest $request) { |
|
92 | $data = []; |
||
93 | $list = $this->environment->UpcomingDeployments(); |
||
94 | foreach ($list as $deployment) { |
||
95 | $data[] = $this->formatter->getDeploymentData($deployment); |
||
96 | } |
||
97 | return $this->getAPIResponse([ |
||
98 | 'list' => $data, |
||
99 | ], 200); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param \SS_HTTPRequest $request |
||
104 | * @return \SS_HTTPResponse |
||
105 | */ |
||
106 | public function currentbuild(SS_HTTPRequest $request) { |
||
107 | $currentBuild = $this->environment->CurrentBuild(); |
||
108 | if (!$currentBuild) { |
||
109 | return $this->getAPIResponse(['deployment' => []], 200); |
||
110 | } |
||
111 | return $this->getAPIResponse(['deployment' => $this->formatter->getDeploymentData($currentBuild)], 200); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param \SS_HTTPRequest $request |
||
116 | * @return \SS_HTTPResponse |
||
117 | */ |
||
118 | public function show(SS_HTTPRequest $request) { |
||
119 | $deployment = DNDeployment::get()->byId($request->param('ID')); |
||
120 | $errorResponse = $this->validateDeployment($deployment); |
||
121 | if ($errorResponse instanceof \SS_HTTPResponse) { |
||
122 | return $errorResponse; |
||
123 | } |
||
124 | return $this->getAPIResponse(['deployment' => $this->formatter->getDeploymentData($deployment)], 200); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param \SS_HTTPRequest $request |
||
129 | * @return \SS_HTTPResponse |
||
130 | */ |
||
131 | public function log(SS_HTTPRequest $request) { |
||
132 | $deployment = DNDeployment::get()->byId($request->param('ID')); |
||
133 | $errorResponse = $this->validateDeployment($deployment); |
||
134 | if ($errorResponse instanceof \SS_HTTPResponse) { |
||
135 | return $errorResponse; |
||
136 | } |
||
137 | $log = $deployment->log(); |
||
138 | $content = $log->exists() ? $log->content() : 'Waiting for action to start'; |
||
139 | $lines = explode(PHP_EOL, $content); |
||
140 | |||
141 | return $this->getAPIResponse([ |
||
142 | 'message' => $lines, |
||
143 | 'status' => $deployment->Status, |
||
144 | 'deployment' => $this->formatter->getDeploymentData($deployment), |
||
145 | ], 200); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Create deployment. Can't use {@link create()} as it's taken by Object. |
||
150 | * |
||
151 | * @param \SS_HTTPRequest $request |
||
152 | * @return \SS_HTTPResponse |
||
153 | */ |
||
154 | public function createdeployment(SS_HTTPRequest $request) { |
||
155 | if ($request->httpMethod() !== 'POST') { |
||
156 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
||
157 | } |
||
158 | |||
159 | $this->checkSecurityToken(); |
||
160 | |||
161 | // @todo the strategy should have been saved when there has been a request for an |
||
162 | // approval or a bypass. This saved state needs to be checked if it's invalidated |
||
163 | // if another deploy happens before this one |
||
164 | $isBranchDeploy = (int) $request->postVar('ref_type') === GitDispatcher::REF_TYPE_BRANCH; |
||
165 | |||
166 | $options = [ |
||
167 | 'sha' => $request->postVar('ref'), |
||
168 | 'ref_type' => $request->postVar('ref_type'), |
||
169 | 'branch' => $isBranchDeploy ? $request->postVar('ref_name') : null, |
||
170 | 'summary' => $request->postVar('summary') |
||
171 | ]; |
||
172 | $strategy = $this->environment->Backend()->planDeploy($this->environment, $options); |
||
173 | |||
174 | $strategy->fromArray($request->postVars()); |
||
175 | |||
176 | $approver = Member::get()->byId($request->postVar('approver_id')); |
||
177 | View Code Duplication | if ($approver && $approver->exists()) { |
|
178 | if (!$this->project->allowed(ApprovalsDispatcher::ALLOW_APPROVAL, $approver)) { |
||
179 | return $this->getAPIResponse(['message' => 'The given approver does not have permissions to approve'], 403); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | $deployment = $strategy->createDeployment(); |
||
184 | if ($approver && $approver->exists()) { |
||
185 | $deployment->ApproverID = $approver->ID; |
||
186 | $deployment->write(); |
||
187 | } |
||
188 | |||
189 | return $this->getAPIResponse([ |
||
190 | 'message' => 'Deployment has been created', |
||
191 | 'deployment' => $this->formatter->getDeploymentData($deployment), |
||
192 | ], 201); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @param \SS_HTTPRequest $request |
||
197 | * @return \SS_HTTPResponse |
||
198 | */ |
||
199 | public function start(SS_HTTPRequest $request) { |
||
200 | if ($request->httpMethod() !== 'POST') { |
||
201 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
||
202 | } |
||
203 | |||
204 | $this->checkSecurityToken(); |
||
205 | |||
206 | $deployment = DNDeployment::get()->byId($request->postVar('id')); |
||
207 | if (!$deployment || !$deployment->exists()) { |
||
208 | return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); |
||
209 | } |
||
210 | |||
211 | // The deployment cannot be started until it has been approved, or bypassed straight to approved state |
||
212 | if ($deployment->State != DNDeployment::STATE_APPROVED) { |
||
213 | return $this->getAPIResponse(['message' => 'This deployment has not been approved. Cannot deploy'], 403); |
||
214 | } |
||
215 | |||
216 | // until we have a system that can invalidate currently scheduled deployments due |
||
217 | // to emergency deploys etc, replan the deployment to check if it's still valid. |
||
218 | $options = $deployment->getDeploymentStrategy()->getOptions(); |
||
219 | $strategy = $this->environment->Backend()->planDeploy($this->environment, $options); |
||
220 | $deployment->Strategy = $strategy->toJSON(); |
||
221 | $deployment->write(); |
||
222 | |||
223 | $deployment->getMachine()->apply(DNDeployment::TR_QUEUE); |
||
224 | |||
225 | $location = \Controller::join_links(Director::absoluteBaseURL(), $this->Link('log'), $deployment->ID); |
||
226 | |||
227 | $response = $this->getAPIResponse([ |
||
228 | 'message' => 'Deployment has been queued', |
||
229 | 'location' => $location, |
||
230 | 'deployment' => $this->formatter->getDeploymentData($deployment), |
||
231 | ], 201); |
||
232 | |||
233 | $response->addHeader('Location', $location); |
||
234 | |||
235 | return $response; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param string $action |
||
240 | * @return string |
||
241 | */ |
||
242 | public function Link($action = '') { |
||
245 | |||
246 | /** |
||
247 | * @param string $name |
||
248 | * @return array |
||
249 | */ |
||
250 | public function getModel($name = '') { |
||
253 | |||
254 | /** |
||
255 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
256 | * an APIResponse with the error, otherwise null. |
||
257 | * |
||
258 | * @param \DNDeployment $deployment |
||
259 | * |
||
260 | * @return null|SS_HTTPResponse |
||
261 | */ |
||
262 | View Code Duplication | protected function validateDeployment(\DNDeployment $deployment) { |
|
274 | |||
275 | } |
||
276 |
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
.