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 DNRoot 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 DNRoot, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class DNRoot extends Controller implements PermissionProvider, TemplateGlobalProvider { |
||
11 | |||
12 | /** |
||
13 | * @const string - action type for actions that perform deployments |
||
14 | */ |
||
15 | const ACTION_DEPLOY = 'deploy'; |
||
16 | |||
17 | /** |
||
18 | * @const string - action type for actions that manipulate snapshots |
||
19 | */ |
||
20 | const ACTION_SNAPSHOT = 'snapshot'; |
||
21 | |||
22 | const ACTION_ENVIRONMENTS = 'createenv'; |
||
23 | |||
24 | const PROJECT_OVERVIEW = 'overview'; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $actionType = self::ACTION_DEPLOY; |
||
30 | |||
31 | /** |
||
32 | * Allow advanced options on deployments |
||
33 | */ |
||
34 | const DEPLOYNAUT_ADVANCED_DEPLOY_OPTIONS = 'DEPLOYNAUT_ADVANCED_DEPLOY_OPTIONS'; |
||
35 | |||
36 | const ALLOW_PROD_DEPLOYMENT = 'ALLOW_PROD_DEPLOYMENT'; |
||
37 | const ALLOW_NON_PROD_DEPLOYMENT = 'ALLOW_NON_PROD_DEPLOYMENT'; |
||
38 | const ALLOW_PROD_SNAPSHOT = 'ALLOW_PROD_SNAPSHOT'; |
||
39 | const ALLOW_NON_PROD_SNAPSHOT = 'ALLOW_NON_PROD_SNAPSHOT'; |
||
40 | const ALLOW_CREATE_ENVIRONMENT = 'ALLOW_CREATE_ENVIRONMENT'; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private static $allowed_actions = array( |
||
46 | 'projects', |
||
47 | 'nav', |
||
48 | 'update', |
||
49 | 'project', |
||
50 | 'toggleprojectstar', |
||
51 | 'branch', |
||
52 | 'environment', |
||
53 | 'metrics', |
||
54 | 'createenvlog', |
||
55 | 'createenv', |
||
56 | 'getDeployForm', |
||
57 | 'doDeploy', |
||
58 | 'deploy', |
||
59 | 'deploylog', |
||
60 | 'abortDeploy', |
||
61 | 'getDataTransferForm', |
||
62 | 'transfer', |
||
63 | 'transferlog', |
||
64 | 'snapshots', |
||
65 | 'createsnapshot', |
||
66 | 'snapshotslog', |
||
67 | 'uploadsnapshot', |
||
68 | 'getCreateEnvironmentForm', |
||
69 | 'getUploadSnapshotForm', |
||
70 | 'getPostSnapshotForm', |
||
71 | 'getDataTransferRestoreForm', |
||
72 | 'getDeleteForm', |
||
73 | 'getMoveForm', |
||
74 | 'restoresnapshot', |
||
75 | 'deletesnapshot', |
||
76 | 'movesnapshot', |
||
77 | 'postsnapshotsuccess', |
||
78 | 'gitRevisions', |
||
79 | 'deploySummary', |
||
80 | 'startDeploy' |
||
81 | ); |
||
82 | |||
83 | /** |
||
84 | * URL handlers pretending that we have a deep URL structure. |
||
85 | */ |
||
86 | private static $url_handlers = array( |
||
87 | 'project/$Project/environment/$Environment/DeployForm' => 'getDeployForm', |
||
88 | 'project/$Project/createsnapshot/DataTransferForm' => 'getDataTransferForm', |
||
89 | 'project/$Project/DataTransferForm' => 'getDataTransferForm', |
||
90 | 'project/$Project/DataTransferRestoreForm' => 'getDataTransferRestoreForm', |
||
91 | 'project/$Project/DeleteForm' => 'getDeleteForm', |
||
92 | 'project/$Project/MoveForm' => 'getMoveForm', |
||
93 | 'project/$Project/UploadSnapshotForm' => 'getUploadSnapshotForm', |
||
94 | 'project/$Project/PostSnapshotForm' => 'getPostSnapshotForm', |
||
95 | 'project/$Project/environment/$Environment/metrics' => 'metrics', |
||
96 | 'project/$Project/environment/$Environment/deploy_summary' => 'deploySummary', |
||
97 | 'project/$Project/environment/$Environment/git_revisions' => 'gitRevisions', |
||
98 | 'project/$Project/environment/$Environment/start-deploy' => 'startDeploy', |
||
99 | 'project/$Project/environment/$Environment/deploy/$Identifier/log' => 'deploylog', |
||
100 | 'project/$Project/environment/$Environment/deploy/$Identifier/abort-deploy' => 'abortDeploy', |
||
101 | 'project/$Project/environment/$Environment/deploy/$Identifier' => 'deploy', |
||
102 | 'project/$Project/transfer/$Identifier/log' => 'transferlog', |
||
103 | 'project/$Project/transfer/$Identifier' => 'transfer', |
||
104 | 'project/$Project/environment/$Environment' => 'environment', |
||
105 | 'project/$Project/createenv/$Identifier/log' => 'createenvlog', |
||
106 | 'project/$Project/createenv/$Identifier' => 'createenv', |
||
107 | 'project/$Project/CreateEnvironmentForm' => 'getCreateEnvironmentForm', |
||
108 | 'project/$Project/branch' => 'branch', |
||
109 | 'project/$Project/build/$Build' => 'build', |
||
110 | 'project/$Project/restoresnapshot/$DataArchiveID' => 'restoresnapshot', |
||
111 | 'project/$Project/deletesnapshot/$DataArchiveID' => 'deletesnapshot', |
||
112 | 'project/$Project/movesnapshot/$DataArchiveID' => 'movesnapshot', |
||
113 | 'project/$Project/update' => 'update', |
||
114 | 'project/$Project/snapshots' => 'snapshots', |
||
115 | 'project/$Project/createsnapshot' => 'createsnapshot', |
||
116 | 'project/$Project/uploadsnapshot' => 'uploadsnapshot', |
||
117 | 'project/$Project/snapshotslog' => 'snapshotslog', |
||
118 | 'project/$Project/postsnapshotsuccess/$DataArchiveID' => 'postsnapshotsuccess', |
||
119 | 'project/$Project/star' => 'toggleprojectstar', |
||
120 | 'project/$Project' => 'project', |
||
121 | 'nav/$Project' => 'nav', |
||
122 | 'projects' => 'projects', |
||
123 | ); |
||
124 | |||
125 | /** |
||
126 | * @var array |
||
127 | */ |
||
128 | protected static $_project_cache = array(); |
||
129 | |||
130 | /** |
||
131 | * @var array |
||
132 | */ |
||
133 | private static $support_links = array(); |
||
134 | |||
135 | /** |
||
136 | * @var array |
||
137 | */ |
||
138 | private static $platform_specific_strings = array(); |
||
139 | |||
140 | /** |
||
141 | * @var array |
||
142 | */ |
||
143 | private static $action_types = array( |
||
144 | self::ACTION_DEPLOY, |
||
145 | self::ACTION_SNAPSHOT, |
||
146 | self::PROJECT_OVERVIEW |
||
147 | ); |
||
148 | |||
149 | /** |
||
150 | * @var DNData |
||
151 | */ |
||
152 | protected $data; |
||
153 | |||
154 | /** |
||
155 | * Include requirements that deploynaut needs, such as javascript. |
||
156 | */ |
||
157 | public static function include_requirements() { |
||
158 | |||
159 | // JS should always go to the bottom, otherwise there's the risk that Requirements |
||
160 | // puts them halfway through the page to the nearest <script> tag. We don't want that. |
||
161 | Requirements::set_force_js_to_bottom(true); |
||
162 | |||
163 | // todo these should be bundled into the same JS as the others in "static" below. |
||
164 | // We've deliberately not used combined_files as it can mess with some of the JS used |
||
165 | // here and cause sporadic errors. |
||
166 | Requirements::javascript('deploynaut/javascript/jquery.js'); |
||
167 | Requirements::javascript('deploynaut/javascript/bootstrap.js'); |
||
168 | Requirements::javascript('deploynaut/javascript/q.js'); |
||
169 | Requirements::javascript('deploynaut/javascript/tablefilter.js'); |
||
170 | Requirements::javascript('deploynaut/javascript/deploynaut.js'); |
||
171 | |||
172 | Requirements::javascript('deploynaut/javascript/bootstrap.file-input.js'); |
||
173 | Requirements::javascript('deploynaut/thirdparty/select2/dist/js/select2.min.js'); |
||
174 | Requirements::javascript('deploynaut/thirdparty/bootstrap-switch/dist/js/bootstrap-switch.min.js'); |
||
175 | Requirements::javascript('deploynaut/javascript/material.js'); |
||
176 | |||
177 | // Load the buildable dependencies only if not loaded centrally. |
||
178 | if (!is_dir(BASE_PATH . DIRECTORY_SEPARATOR . 'static')) { |
||
179 | if (\Director::isDev()) { |
||
180 | \Requirements::javascript('deploynaut/static/bundle-debug.js'); |
||
181 | } else { |
||
182 | \Requirements::javascript('deploynaut/static/bundle.js'); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | Requirements::css('deploynaut/static/style.css'); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Check for feature flags: |
||
191 | * - FLAG_SNAPSHOTS_ENABLED: set to true to enable globally |
||
192 | * - FLAG_SNAPSHOTS_ENABLED_FOR_MEMBERS: set to semicolon-separated list of email addresses of allowed users. |
||
193 | * |
||
194 | * @return boolean |
||
195 | */ |
||
196 | public static function FlagSnapshotsEnabled() { |
||
209 | |||
210 | /** |
||
211 | * @return ArrayList |
||
212 | */ |
||
213 | public static function get_support_links() { |
||
219 | |||
220 | /** |
||
221 | * @return array |
||
222 | */ |
||
223 | public static function get_template_global_variables() { |
||
231 | |||
232 | /** |
||
233 | */ |
||
234 | public function init() { |
||
246 | |||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | public function Link() { |
||
253 | |||
254 | /** |
||
255 | * Actions |
||
256 | * |
||
257 | * @param SS_HTTPRequest $request |
||
258 | * @return \SS_HTTPResponse |
||
259 | */ |
||
260 | public function index(SS_HTTPRequest $request) { |
||
263 | |||
264 | /** |
||
265 | * Action |
||
266 | * |
||
267 | * @param SS_HTTPRequest $request |
||
268 | * @return string - HTML |
||
269 | */ |
||
270 | public function projects(SS_HTTPRequest $request) { |
||
276 | |||
277 | /** |
||
278 | * @param SS_HTTPRequest $request |
||
279 | * @return HTMLText |
||
280 | */ |
||
281 | public function nav(SS_HTTPRequest $request) { |
||
284 | |||
285 | /** |
||
286 | * Return a link to the navigation template used for AJAX requests. |
||
287 | * @return string |
||
288 | */ |
||
289 | public function NavLink() { |
||
294 | |||
295 | /** |
||
296 | * Action |
||
297 | * |
||
298 | * @param SS_HTTPRequest $request |
||
299 | * @return SS_HTTPResponse - HTML |
||
300 | */ |
||
301 | public function snapshots(SS_HTTPRequest $request) { |
||
305 | |||
306 | /** |
||
307 | * Action |
||
308 | * |
||
309 | * @param SS_HTTPRequest $request |
||
310 | * @return string - HTML |
||
311 | */ |
||
312 | View Code Duplication | public function createsnapshot(SS_HTTPRequest $request) { |
|
331 | |||
332 | /** |
||
333 | * Action |
||
334 | * |
||
335 | * @param SS_HTTPRequest $request |
||
336 | * @return string - HTML |
||
337 | */ |
||
338 | View Code Duplication | public function uploadsnapshot(SS_HTTPRequest $request) { |
|
357 | |||
358 | /** |
||
359 | * Return the upload limit for snapshot uploads |
||
360 | * @return string |
||
361 | */ |
||
362 | public function UploadLimit() { |
||
368 | |||
369 | /** |
||
370 | * Construct the upload form. |
||
371 | * |
||
372 | * @param SS_HTTPRequest $request |
||
373 | * @return Form |
||
374 | */ |
||
375 | public function getUploadSnapshotForm(SS_HTTPRequest $request) { |
||
425 | |||
426 | /** |
||
427 | * @param array $data |
||
428 | * @param Form $form |
||
429 | * |
||
430 | * @return bool|HTMLText|SS_HTTPResponse |
||
431 | */ |
||
432 | public function doUploadSnapshot($data, Form $form) { |
||
433 | $this->setCurrentActionType(self::ACTION_SNAPSHOT); |
||
434 | |||
435 | // Performs canView permission check by limiting visible projects |
||
436 | $project = $this->getCurrentProject(); |
||
437 | if(!$project) { |
||
438 | return $this->project404Response(); |
||
439 | } |
||
440 | |||
441 | $validEnvs = $project->DNEnvironmentList() |
||
442 | ->filterByCallback(function($item) { |
||
443 | return $item->canUploadArchive(); |
||
444 | }); |
||
445 | |||
446 | // Validate $data['EnvironmentID'] by checking against $validEnvs. |
||
447 | $environment = $validEnvs->find('ID', $data['EnvironmentID']); |
||
448 | if(!$environment) { |
||
449 | throw new LogicException('Invalid environment'); |
||
450 | } |
||
451 | |||
452 | $this->validateSnapshotMode($data['Mode']); |
||
453 | |||
454 | $dataArchive = DNDataArchive::create(array( |
||
455 | 'AuthorID' => Member::currentUserID(), |
||
456 | 'EnvironmentID' => $data['EnvironmentID'], |
||
457 | 'IsManualUpload' => true, |
||
458 | )); |
||
459 | // needs an ID and transfer to determine upload path |
||
460 | $dataArchive->write(); |
||
461 | $dataTransfer = DNDataTransfer::create(array( |
||
462 | 'AuthorID' => Member::currentUserID(), |
||
463 | 'Mode' => $data['Mode'], |
||
464 | 'Origin' => 'ManualUpload', |
||
465 | 'EnvironmentID' => $data['EnvironmentID'] |
||
466 | )); |
||
467 | $dataTransfer->write(); |
||
468 | $dataArchive->DataTransfers()->add($dataTransfer); |
||
469 | $form->saveInto($dataArchive); |
||
470 | $dataArchive->write(); |
||
471 | $workingDir = TEMP_FOLDER . DIRECTORY_SEPARATOR . 'deploynaut-transfer-' . $dataTransfer->ID; |
||
472 | |||
473 | $cleanupFn = function() use($workingDir, $dataTransfer, $dataArchive) { |
||
474 | $process = new Process(sprintf('rm -rf %s', escapeshellarg($workingDir))); |
||
475 | $process->run(); |
||
476 | $dataTransfer->delete(); |
||
477 | $dataArchive->delete(); |
||
478 | }; |
||
479 | |||
480 | // extract the sspak contents so we can inspect them |
||
481 | try { |
||
482 | $dataArchive->extractArchive($workingDir); |
||
483 | } catch(Exception $e) { |
||
484 | $cleanupFn(); |
||
485 | $form->sessionMessage( |
||
486 | 'There was a problem trying to open your snapshot for processing. Please try uploading again', |
||
487 | 'bad' |
||
488 | ); |
||
489 | return $this->redirectBack(); |
||
490 | } |
||
491 | |||
492 | // validate that the sspak contents match the declared contents |
||
493 | $result = $dataArchive->validateArchiveContents(); |
||
494 | if(!$result->valid()) { |
||
495 | $cleanupFn(); |
||
496 | $form->sessionMessage($result->message(), 'bad'); |
||
497 | return $this->redirectBack(); |
||
498 | } |
||
499 | |||
500 | // fix file permissions of extracted sspak files then re-build the sspak |
||
501 | try { |
||
502 | $dataArchive->fixArchivePermissions($workingDir); |
||
503 | $dataArchive->setArchiveFromFiles($workingDir); |
||
504 | } catch(Exception $e) { |
||
505 | $cleanupFn(); |
||
506 | $form->sessionMessage( |
||
507 | 'There was a problem processing your snapshot. Please try uploading again', |
||
508 | 'bad' |
||
509 | ); |
||
510 | return $this->redirectBack(); |
||
511 | } |
||
512 | |||
513 | // cleanup any extracted sspak contents lying around |
||
514 | $process = new Process(sprintf('rm -rf %s', escapeshellarg($workingDir))); |
||
515 | $process->run(); |
||
516 | |||
517 | return $this->customise(array( |
||
518 | 'Project' => $project, |
||
519 | 'CurrentProject' => $project, |
||
520 | 'SnapshotsSection' => 1, |
||
521 | 'DataArchive' => $dataArchive, |
||
522 | 'DataTransferRestoreForm' => $this->getDataTransferRestoreForm($this->request, $dataArchive), |
||
523 | 'BackURL' => $project->Link('snapshots') |
||
524 | ))->renderWith(array('DNRoot_uploadsnapshot', 'DNRoot')); |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * @param SS_HTTPRequest $request |
||
529 | * @return Form |
||
530 | */ |
||
531 | public function getPostSnapshotForm(SS_HTTPRequest $request) { |
||
575 | |||
576 | /** |
||
577 | * @param array $data |
||
578 | * @param Form $form |
||
579 | * |
||
580 | * @return SS_HTTPResponse |
||
581 | */ |
||
582 | public function doPostSnapshot($data, $form) { |
||
612 | |||
613 | /** |
||
614 | * Action |
||
615 | * |
||
616 | * @param SS_HTTPRequest $request |
||
617 | * @return SS_HTTPResponse - HTML |
||
618 | */ |
||
619 | public function snapshotslog(SS_HTTPRequest $request) { |
||
623 | |||
624 | /** |
||
625 | * @param SS_HTTPRequest $request |
||
626 | * @return SS_HTTPResponse|string |
||
627 | * @throws SS_HTTPResponse_Exception |
||
628 | */ |
||
629 | public function postsnapshotsuccess(SS_HTTPRequest $request) { |
||
658 | |||
659 | /** |
||
660 | * @param SS_HTTPRequest $request |
||
661 | * @return \SS_HTTPResponse |
||
662 | */ |
||
663 | public function project(SS_HTTPRequest $request) { |
||
667 | |||
668 | /** |
||
669 | * This action will star / unstar a project for the current member |
||
670 | * |
||
671 | * @param SS_HTTPRequest $request |
||
672 | * |
||
673 | * @return SS_HTTPResponse |
||
674 | */ |
||
675 | public function toggleprojectstar(SS_HTTPRequest $request) { |
||
696 | |||
697 | /** |
||
698 | * @param SS_HTTPRequest $request |
||
699 | * @return \SS_HTTPResponse |
||
700 | */ |
||
701 | public function branch(SS_HTTPRequest $request) { |
||
717 | |||
718 | /** |
||
719 | * @param SS_HTTPRequest $request |
||
720 | * @return \SS_HTTPResponse |
||
721 | */ |
||
722 | public function environment(SS_HTTPRequest $request) { |
||
741 | |||
742 | /** |
||
743 | * Shows the creation log. |
||
744 | * |
||
745 | * @param SS_HTTPRequest $request |
||
746 | * @return string |
||
747 | */ |
||
748 | public function createenv(SS_HTTPRequest $request) { |
||
775 | |||
776 | |||
777 | public function createenvlog(SS_HTTPRequest $request) { |
||
803 | |||
804 | /** |
||
805 | * @param SS_HTTPRequest $request |
||
806 | * @return Form |
||
807 | */ |
||
808 | public function getCreateEnvironmentForm(SS_HTTPRequest $request) { |
||
850 | |||
851 | /** |
||
852 | * @param array $data |
||
853 | * @param Form $form |
||
854 | * |
||
855 | * @return bool|HTMLText|SS_HTTPResponse |
||
856 | */ |
||
857 | public function doCreateEnvironment($data, Form $form) { |
||
881 | |||
882 | /** |
||
883 | * |
||
884 | * @param SS_HTTPRequest $request |
||
885 | * @return \SS_HTTPResponse |
||
886 | */ |
||
887 | public function metrics(SS_HTTPRequest $request) { |
||
902 | |||
903 | /** |
||
904 | * Get the DNData object. |
||
905 | * |
||
906 | * @return DNData |
||
907 | */ |
||
908 | public function DNData() { |
||
911 | |||
912 | /** |
||
913 | * Provide a list of all projects. |
||
914 | * |
||
915 | * @return SS_List |
||
916 | */ |
||
917 | public function DNProjectList() { |
||
934 | |||
935 | /** |
||
936 | * @return ArrayList |
||
937 | */ |
||
938 | public function getPlatformSpecificStrings() { |
||
944 | |||
945 | /** |
||
946 | * Provide a list of all starred projects for the currently logged in member |
||
947 | * |
||
948 | * @return SS_List |
||
949 | */ |
||
950 | public function getStarredProjects() { |
||
966 | |||
967 | /** |
||
968 | * Returns top level navigation of projects. |
||
969 | * |
||
970 | * @param int $limit |
||
971 | * |
||
972 | * @return ArrayList |
||
973 | */ |
||
974 | public function Navigation($limit = 5) { |
||
1032 | |||
1033 | /** |
||
1034 | * Construct the deployment form |
||
1035 | * |
||
1036 | * @return Form |
||
1037 | */ |
||
1038 | public function getDeployForm($request = null) { |
||
1078 | |||
1079 | /** |
||
1080 | * @param SS_HTTPRequest $request |
||
1081 | * |
||
1082 | * @return SS_HTTPResponse|string |
||
1083 | */ |
||
1084 | public function gitRevisions(SS_HTTPRequest $request) { |
||
1229 | |||
1230 | protected function applyRedeploy(SS_HTTPRequest $request, &$data) { |
||
1256 | |||
1257 | /** |
||
1258 | * Check and regenerate a global CSRF token |
||
1259 | * |
||
1260 | * @param SS_HTTPRequest $request |
||
1261 | * @param bool $resetToken |
||
1262 | * |
||
1263 | * @return bool |
||
1264 | */ |
||
1265 | protected function checkCsrfToken(SS_HTTPRequest $request, $resetToken = true) { |
||
1285 | |||
1286 | /** |
||
1287 | * @param SS_HTTPRequest $request |
||
1288 | * |
||
1289 | * @return string |
||
1290 | */ |
||
1291 | public function deploySummary(SS_HTTPRequest $request) { |
||
1336 | |||
1337 | /** |
||
1338 | * Deployment form submission handler. |
||
1339 | * |
||
1340 | * Initiate a DNDeployment record and redirect to it for status polling |
||
1341 | * |
||
1342 | * @param SS_HTTPRequest $request |
||
1343 | * |
||
1344 | * @return SS_HTTPResponse |
||
1345 | * @throws ValidationException |
||
1346 | * @throws null |
||
1347 | */ |
||
1348 | public function startDeploy(SS_HTTPRequest $request) { |
||
1384 | |||
1385 | /** |
||
1386 | * Action - Do the actual deploy |
||
1387 | * |
||
1388 | * @param SS_HTTPRequest $request |
||
1389 | * |
||
1390 | * @return SS_HTTPResponse|string |
||
1391 | * @throws SS_HTTPResponse_Exception |
||
1392 | */ |
||
1393 | public function deploy(SS_HTTPRequest $request) { |
||
1418 | |||
1419 | |||
1420 | /** |
||
1421 | * Action - Get the latest deploy log |
||
1422 | * |
||
1423 | * @param SS_HTTPRequest $request |
||
1424 | * |
||
1425 | * @return string |
||
1426 | * @throws SS_HTTPResponse_Exception |
||
1427 | */ |
||
1428 | public function deploylog(SS_HTTPRequest $request) { |
||
1458 | |||
1459 | public function abortDeploy(SS_HTTPRequest $request) { |
||
1493 | |||
1494 | /** |
||
1495 | * @param SS_HTTPRequest|null $request |
||
1496 | * |
||
1497 | * @return Form |
||
1498 | */ |
||
1499 | public function getDataTransferForm(SS_HTTPRequest $request = null) { |
||
1527 | |||
1528 | /** |
||
1529 | * @param array $data |
||
1530 | * @param Form $form |
||
1531 | * |
||
1532 | * @return SS_HTTPResponse |
||
1533 | * @throws SS_HTTPResponse_Exception |
||
1534 | */ |
||
1535 | public function doDataTransfer($data, Form $form) { |
||
1599 | |||
1600 | /** |
||
1601 | * View into the log for a {@link DNDataTransfer}. |
||
1602 | * |
||
1603 | * @param SS_HTTPRequest $request |
||
1604 | * |
||
1605 | * @return SS_HTTPResponse|string |
||
1606 | * @throws SS_HTTPResponse_Exception |
||
1607 | */ |
||
1608 | public function transfer(SS_HTTPRequest $request) { |
||
1633 | |||
1634 | /** |
||
1635 | * Action - Get the latest deploy log |
||
1636 | * |
||
1637 | * @param SS_HTTPRequest $request |
||
1638 | * |
||
1639 | * @return string |
||
1640 | * @throws SS_HTTPResponse_Exception |
||
1641 | */ |
||
1642 | public function transferlog(SS_HTTPRequest $request) { |
||
1671 | |||
1672 | /** |
||
1673 | * Note: Submits to the same action as {@link getDataTransferForm()}, |
||
1674 | * but with a Direction=push and an archive reference. |
||
1675 | * |
||
1676 | * @param SS_HTTPRequest $request |
||
1677 | * @param DNDataArchive|null $dataArchive Only set when method is called manually in {@link restore()}, |
||
1678 | * otherwise the state is inferred from the request data. |
||
1679 | * @return Form |
||
1680 | */ |
||
1681 | public function getDataTransferRestoreForm(SS_HTTPRequest $request, DNDataArchive $dataArchive = null) { |
||
1729 | |||
1730 | /** |
||
1731 | * View a form to restore a specific {@link DataArchive}. |
||
1732 | * Permission checks are handled in {@link DataArchives()}. |
||
1733 | * Submissions are handled through {@link doDataTransfer()}, same as backup operations. |
||
1734 | * |
||
1735 | * @param SS_HTTPRequest $request |
||
1736 | * |
||
1737 | * @return HTMLText |
||
1738 | * @throws SS_HTTPResponse_Exception |
||
1739 | */ |
||
1740 | View Code Duplication | public function restoresnapshot(SS_HTTPRequest $request) { |
|
1761 | |||
1762 | /** |
||
1763 | * View a form to delete a specific {@link DataArchive}. |
||
1764 | * Permission checks are handled in {@link DataArchives()}. |
||
1765 | * Submissions are handled through {@link doDelete()}. |
||
1766 | * |
||
1767 | * @param SS_HTTPRequest $request |
||
1768 | * |
||
1769 | * @return HTMLText |
||
1770 | * @throws SS_HTTPResponse_Exception |
||
1771 | */ |
||
1772 | View Code Duplication | public function deletesnapshot(SS_HTTPRequest $request) { |
|
1791 | |||
1792 | /** |
||
1793 | * @param SS_HTTPRequest $request |
||
1794 | * @param DNDataArchive|null $dataArchive Only set when method is called manually, otherwise the state is inferred |
||
1795 | * from the request data. |
||
1796 | * @return Form |
||
1797 | */ |
||
1798 | public function getDeleteForm(SS_HTTPRequest $request, DNDataArchive $dataArchive = null) { |
||
1827 | |||
1828 | /** |
||
1829 | * @param array $data |
||
1830 | * @param Form $form |
||
1831 | * |
||
1832 | * @return bool|SS_HTTPResponse |
||
1833 | * @throws SS_HTTPResponse_Exception |
||
1834 | */ |
||
1835 | public function doDelete($data, Form $form) { |
||
1865 | |||
1866 | /** |
||
1867 | * View a form to move a specific {@link DataArchive}. |
||
1868 | * |
||
1869 | * @param SS_HTTPRequest $request |
||
1870 | * |
||
1871 | * @return HTMLText |
||
1872 | * @throws SS_HTTPResponse_Exception |
||
1873 | */ |
||
1874 | View Code Duplication | public function movesnapshot(SS_HTTPRequest $request) { |
|
1894 | |||
1895 | /** |
||
1896 | * Build snapshot move form. |
||
1897 | * |
||
1898 | * @param SS_HTTPRequest $request |
||
1899 | * @param DNDataArchive|null $dataArchive |
||
1900 | * |
||
1901 | * @return Form|SS_HTTPResponse |
||
1902 | */ |
||
1903 | public function getMoveForm(SS_HTTPRequest $request, DNDataArchive $dataArchive = null) { |
||
1933 | |||
1934 | /** |
||
1935 | * @param array $data |
||
1936 | * @param Form $form |
||
1937 | * |
||
1938 | * @return bool|SS_HTTPResponse |
||
1939 | * @throws SS_HTTPResponse_Exception |
||
1940 | * @throws ValidationException |
||
1941 | * @throws null |
||
1942 | */ |
||
1943 | public function doMove($data, Form $form) { |
||
1975 | |||
1976 | /** |
||
1977 | * Returns an error message if redis is unavailable |
||
1978 | * |
||
1979 | * @return string |
||
1980 | */ |
||
1981 | public static function RedisUnavailable() { |
||
1989 | |||
1990 | /** |
||
1991 | * Returns the number of connected Redis workers |
||
1992 | * |
||
1993 | * @return int |
||
1994 | */ |
||
1995 | public static function RedisWorkersCount() { |
||
1998 | |||
1999 | /** |
||
2000 | * @return array |
||
2001 | */ |
||
2002 | public function providePermissions() { |
||
2032 | |||
2033 | /** |
||
2034 | * @return DNProject|null |
||
2035 | */ |
||
2036 | public function getCurrentProject() { |
||
2046 | |||
2047 | /** |
||
2048 | * @param DNProject|null $project |
||
2049 | * @return DNEnvironment|null |
||
2050 | */ |
||
2051 | public function getCurrentEnvironment(DNProject $project = null) { |
||
2064 | |||
2065 | /** |
||
2066 | * This will return a const that indicates the class of action currently being performed |
||
2067 | * |
||
2068 | * Until DNRoot is de-godded, it does a bunch of different actions all in the same class. |
||
2069 | * So we just have each action handler calll setCurrentActionType to define what sort of |
||
2070 | * action it is. |
||
2071 | * |
||
2072 | * @return string - one of the consts from self::$action_types |
||
2073 | */ |
||
2074 | public function getCurrentActionType() { |
||
2077 | |||
2078 | /** |
||
2079 | * Sets the current action type |
||
2080 | * |
||
2081 | * @param string $actionType string - one of the consts from self::$action_types |
||
2082 | */ |
||
2083 | public function setCurrentActionType($actionType) { |
||
2086 | |||
2087 | /** |
||
2088 | * Helper method to allow templates to know whether they should show the 'Archive List' include or not. |
||
2089 | * The actual permissions are set on a per-environment level, so we need to find out if this $member can upload to |
||
2090 | * or download from *any* {@link DNEnvironment} that (s)he has access to. |
||
2091 | * |
||
2092 | * TODO To be replaced with a method that just returns the list of archives this {@link Member} has access to. |
||
2093 | * |
||
2094 | * @param Member|null $member The {@link Member} to check (or null to check the currently logged in Member) |
||
2095 | * @return boolean|null true if $member has access to upload or download to at least one {@link DNEnvironment}. |
||
2096 | */ |
||
2097 | public function CanViewArchives(Member $member = null) { |
||
2127 | |||
2128 | /** |
||
2129 | * Returns a list of attempted environment creations. |
||
2130 | * |
||
2131 | * @return PaginatedList |
||
2132 | */ |
||
2133 | public function CreateEnvironmentList() { |
||
2144 | |||
2145 | /** |
||
2146 | * Returns a list of all archive files that can be accessed by the currently logged-in {@link Member} |
||
2147 | * |
||
2148 | * @return PaginatedList |
||
2149 | */ |
||
2150 | public function CompleteDataArchives() { |
||
2164 | |||
2165 | /** |
||
2166 | * @return PaginatedList The list of "pending" data archives which are waiting for a file |
||
2167 | * to be delivered offline by post, and manually uploaded into the system. |
||
2168 | */ |
||
2169 | public function PendingDataArchives() { |
||
2181 | |||
2182 | /** |
||
2183 | * @return PaginatedList |
||
2184 | */ |
||
2185 | public function DataTransferLogs() { |
||
2186 | $project = $this->getCurrentProject(); |
||
2187 | |||
2188 | $transfers = DNDataTransfer::get()->filterByCallback(function($record) use($project) { |
||
2189 | return |
||
2190 | $record->Environment()->Project()->ID == $project->ID && // Ensure only the current Project is shown |
||
2191 | ( |
||
2192 | $record->Environment()->canRestore() || // Ensure member can perform an action on the transfers env |
||
2193 | $record->Environment()->canBackup() || |
||
2194 | $record->Environment()->canUploadArchive() || |
||
2195 | $record->Environment()->canDownloadArchive() |
||
2196 | ); |
||
2197 | }); |
||
2198 | |||
2199 | return new PaginatedList($transfers->sort("Created", "DESC"), $this->request); |
||
2200 | } |
||
2201 | |||
2202 | /** |
||
2203 | * @return null|PaginatedList |
||
2204 | */ |
||
2205 | public function DeployHistory() { |
||
2216 | |||
2217 | /** |
||
2218 | * @return SS_HTTPResponse |
||
2219 | */ |
||
2220 | protected function project404Response() { |
||
2226 | |||
2227 | /** |
||
2228 | * @return SS_HTTPResponse |
||
2229 | */ |
||
2230 | protected function environment404Response() { |
||
2234 | |||
2235 | /** |
||
2236 | * @param string $status |
||
2237 | * @param string $content |
||
2238 | * |
||
2239 | * @return string |
||
2240 | */ |
||
2241 | public function sendResponse($status, $content) { |
||
2258 | |||
2259 | /** |
||
2260 | * Validate the snapshot mode |
||
2261 | * |
||
2262 | * @param string $mode |
||
2263 | */ |
||
2264 | protected function validateSnapshotMode($mode) { |
||
2269 | |||
2270 | /** |
||
2271 | * @param string $sectionName |
||
2272 | * @param string $title |
||
2273 | * |
||
2274 | * @return SS_HTTPResponse |
||
2275 | */ |
||
2276 | protected function getCustomisedViewSection($sectionName, $title = '', $data = array()) { |
||
2290 | |||
2291 | /** |
||
2292 | * Get items for the ambient menu that should be accessible from all pages. |
||
2293 | * |
||
2294 | * @return ArrayList |
||
2295 | */ |
||
2296 | public function AmbientMenu() { |
||
2313 | |||
2314 | /** |
||
2315 | * Checks whether the user can create a project. |
||
2316 | * |
||
2317 | * @return bool |
||
2318 | */ |
||
2319 | public function canCreateProjects($member = null) { |
||
2325 | |||
2326 | } |
||
2327 | |||
2328 |
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.