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:
Complex classes like DeployDispatcher often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DeployDispatcher, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class DeployDispatcher extends Dispatcher { |
||
8 | |||
9 | const ACTION_DEPLOY = 'deploys'; |
||
10 | |||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | public static $allowed_actions = [ |
||
15 | 'history', |
||
16 | 'upcoming', |
||
17 | 'currentbuild', |
||
18 | 'show', |
||
19 | 'log', |
||
20 | 'start', |
||
21 | 'save' |
||
22 | |||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * @var \DNProject |
||
27 | */ |
||
28 | protected $project = null; |
||
29 | |||
30 | /** |
||
31 | * @var \DNEnvironment |
||
32 | */ |
||
33 | protected $environment = null; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private static $action_types = [ |
||
39 | self::ACTION_DEPLOY |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * This is a per request cache of $this->project()->listMembers() |
||
44 | * |
||
45 | * @var null|array |
||
46 | */ |
||
47 | private static $_cache_project_members = null; |
||
48 | |||
49 | /** |
||
50 | * This is a per request cache of $this->environment->CurrentBuild(); |
||
51 | * |
||
52 | * @var null|DNDeployment |
||
53 | */ |
||
54 | private static $_cache_current_build = null; |
||
55 | |||
56 | View Code Duplication | public function init() { |
|
57 | parent::init(); |
||
58 | |||
59 | $this->project = $this->getCurrentProject(); |
||
60 | |||
61 | if (!$this->project) { |
||
62 | return $this->project404Response(); |
||
63 | } |
||
64 | |||
65 | // Performs canView permission check by limiting visible projects |
||
66 | $this->environment = $this->getCurrentEnvironment($this->project); |
||
67 | if (!$this->environment) { |
||
68 | return $this->environment404Response(); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param \SS_HTTPRequest $request |
||
74 | * @return \HTMLText|\SS_HTTPResponse |
||
75 | */ |
||
76 | public function index(\SS_HTTPRequest $request) { |
||
77 | return $this->redirect(\Controller::join_links($this->Link(), 'history'), 302); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param \SS_HTTPRequest $request |
||
82 | * @return \SS_HTTPResponse |
||
83 | */ |
||
84 | View Code Duplication | public function history(SS_HTTPRequest $request) { |
|
85 | $data = []; |
||
86 | |||
87 | $list = $this->environment->DeployHistory('DeployStarted'); |
||
88 | |||
89 | foreach ($list as $deployment) { |
||
90 | $data[] = $this->getDeploymentData($deployment); |
||
91 | } |
||
92 | |||
93 | return $this->getAPIResponse([ |
||
94 | 'list' => $data, |
||
95 | ], 200); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param \SS_HTTPRequest $request |
||
100 | * @return \SS_HTTPResponse |
||
101 | */ |
||
102 | View Code Duplication | public function upcoming(SS_HTTPRequest $request) { |
|
103 | $data = []; |
||
104 | $list = $this->environment->UpcomingDeployments(); |
||
105 | foreach ($list as $deployment) { |
||
106 | $data[] = $this->getDeploymentData($deployment); |
||
107 | } |
||
108 | return $this->getAPIResponse([ |
||
109 | 'list' => $data, |
||
110 | ], 200); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param \SS_HTTPRequest $request |
||
115 | * @return \SS_HTTPResponse |
||
116 | */ |
||
117 | public function currentbuild(SS_HTTPRequest $request) { |
||
118 | $currentBuild = $this->environment->CurrentBuild(); |
||
119 | if (!$currentBuild) { |
||
120 | return $this->getAPIResponse(['deployment' => []], 200); |
||
121 | } |
||
122 | return $this->getAPIResponse(['deployment' => $this->getDeploymentData($currentBuild)], 200); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param \SS_HTTPRequest $request |
||
127 | * @return \SS_HTTPResponse |
||
128 | */ |
||
129 | public function show(SS_HTTPRequest $request) { |
||
130 | $deployment = DNDeployment::get()->byId($request->param('ID')); |
||
131 | $errorResponse = $this->validateDeployment($deployment); |
||
132 | if ($errorResponse instanceof \SS_HTTPResponse) { |
||
133 | return $errorResponse; |
||
134 | } |
||
135 | return $this->getAPIResponse(['deployment' => $this->getDeploymentData($deployment)], 200); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param \SS_HTTPRequest $request |
||
140 | * @return \SS_HTTPResponse |
||
141 | */ |
||
142 | public function log(SS_HTTPRequest $request) { |
||
143 | $deployment = DNDeployment::get()->byId($request->param('ID')); |
||
144 | $errorResponse = $this->validateDeployment($deployment); |
||
145 | if ($errorResponse instanceof \SS_HTTPResponse) { |
||
146 | return $errorResponse; |
||
147 | } |
||
148 | $log = $deployment->log(); |
||
149 | $content = $log->exists() ? $log->content() : 'Waiting for action to start'; |
||
150 | $lines = explode(PHP_EOL, $content); |
||
151 | |||
152 | return $this->getAPIResponse([ |
||
153 | 'message' => $lines, |
||
154 | 'status' => $deployment->Status, |
||
155 | 'deployment' => $this->getDeploymentData($deployment), |
||
156 | ], 200); |
||
157 | } |
||
158 | |||
159 | public function save(\SS_HTTPRequest $request) { |
||
160 | |||
161 | if ($request->httpMethod() !== 'POST') { |
||
162 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
||
163 | } |
||
164 | |||
165 | $this->checkSecurityToken(); |
||
166 | if (!$this->environment->canDeploy(Member::currentUser())) { |
||
167 | return $this->getAPIResponse(['message' => 'You are not authorised to deploy this environment'], 403); |
||
168 | } |
||
169 | |||
170 | // @todo the strategy should have been saved when there has been a request for an |
||
171 | // approval or a bypass. This saved state needs to be checked if it's invalidated |
||
172 | // if another deploy happens before this one |
||
173 | $options = [ |
||
174 | 'sha' => $request->requestVar('ref'), |
||
175 | 'ref_type' => $request->requestVar('ref_type'), |
||
176 | 'branch' => $request->requestVar('ref_name'), |
||
177 | 'summary' => $request->requestVar('summary') |
||
178 | ]; |
||
179 | $strategy = $this->environment->Backend()->planDeploy($this->environment, $options); |
||
180 | |||
181 | $strategy->fromArray($request->requestVars()); |
||
182 | $deployment = $strategy->createDeployment(); |
||
183 | $deployment->getMachine()->apply(DNDeployment::TR_SUBMIT); |
||
184 | return $this->getAPIResponse([ |
||
185 | 'message' => 'deployment has been created', |
||
186 | 'id' => $deployment->ID, |
||
187 | 'deployment' => $this->getDeploymentData($deployment), |
||
188 | ], 201); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param \SS_HTTPRequest $request |
||
193 | * @return \SS_HTTPResponse |
||
194 | */ |
||
195 | public function start(SS_HTTPRequest $request) { |
||
196 | if ($request->httpMethod() !== 'POST') { |
||
197 | return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
||
198 | } |
||
199 | |||
200 | $this->checkSecurityToken(); |
||
201 | |||
202 | $deployment = DNDeployment::get()->byId($request->param('ID')); |
||
203 | |||
204 | if (!$deployment || !$deployment->exists()) { |
||
205 | return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); |
||
206 | } |
||
207 | if (!$this->environment->canDeploy(Member::currentUser())) { |
||
208 | return $this->getAPIResponse(['message' => 'You are not authorised to deploy this environment'], 403); |
||
209 | } |
||
210 | |||
211 | // until we have a system that can invalidate currently scheduled deployments due |
||
212 | // to emergency deploys etc, replan the deployment to check if it's still valid. |
||
213 | |||
214 | $options = $deployment->getDeploymentStrategy()->getOptions(); |
||
215 | |||
216 | $strategy = $this->environment->Backend()->planDeploy($this->environment, $options); |
||
217 | $deployment->Strategy = $strategy->toJSON(); |
||
218 | $deployment->write(); |
||
219 | |||
220 | $deployment->getMachine()->apply(DNDeployment::TR_QUEUE); |
||
221 | |||
222 | $location = \Controller::join_links(Director::absoluteBaseURL(), $this->Link('log'), $deployment->ID); |
||
223 | |||
224 | $response = $this->getAPIResponse([ |
||
225 | 'message' => 'deployment has been queued', |
||
226 | 'id' => $deployment->ID, |
||
227 | 'location' => $location, |
||
228 | 'deployment' => $this->getDeploymentData($deployment), |
||
229 | ], 201); |
||
230 | $response->addHeader('Location', $location); |
||
231 | return $response; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @param string $action |
||
236 | * @return string |
||
237 | */ |
||
238 | public function Link($action = '') { |
||
241 | |||
242 | /** |
||
243 | * @param string $name |
||
244 | * @return array |
||
245 | */ |
||
246 | public function getModel($name = '') { |
||
249 | |||
250 | /** |
||
251 | * Return data about a single deployment for use in API response. |
||
252 | * @param DNDeployment $deployment |
||
253 | * @return array |
||
254 | */ |
||
255 | protected function getDeploymentData(DNDeployment $deployment) { |
||
256 | |||
257 | if (self::$_cache_current_build === null) { |
||
258 | self::$_cache_current_build = $this->environment->CurrentBuild(); |
||
305 | |||
306 | /** |
||
307 | * Return data about a particular {@link Member} of the stack for use in API response. |
||
308 | * Note that role can be null in the response. This is the case of an admin, or an operations |
||
309 | * user who can create the deployment but is not part of the stack roles. |
||
310 | * |
||
311 | * @param Member $member |
||
312 | * @return array |
||
313 | */ |
||
314 | protected function getStackMemberData(Member $member) { |
||
336 | |||
337 | /** |
||
338 | * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return |
||
339 | * an APIResponse with the error, otherwise null. |
||
340 | * |
||
341 | * @param \DNDeployment $deployment |
||
342 | * |
||
343 | * @return null|SS_HTTPResponse |
||
344 | */ |
||
345 | protected function validateDeployment(\DNDeployment $deployment) { |
||
354 | |||
355 | } |
||
356 |
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
.