Completed
Pull Request — master (#713)
by Sean
05:17
created

CleanupOrphanedRecordsTask   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 122
Duplicated Lines 77.05 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 28
lcom 0
cbo 8
dl 94
loc 122
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 94 118 28

How to fix   Duplicated Code   

Duplicated Code

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
2
3
class CleanupOrphanedRecordsTask extends BuildTask {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
5
	public function run($request) {
6
		$args = $request->getVar('args');
7
		$dryRun = $args && in_array('--dry-run', $args);
8
9 View Code Duplication
		$log = function($message) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
10
			$message = sprintf('[%s] ', date('Y-m-d H:i:s')) . $message;
11
			echo $message . PHP_EOL;
12
		};
13
14
		if (!Director::is_cli()) {
15
			$log('This task must be run under the command line');
16
			return;
17
		}
18
19
		if ($dryRun) {
20
			$log('Running in dry-run mode. No data will be deleted');
21
		}
22
23
		$count = 0;
24
25 View Code Duplication
		foreach (DNEnvironment::get() as $environment) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
26
			$project = $environment->Project();
27
			if (!$project || !$project->exists()) {
28
				$log(sprintf(
29
					'Environment (ID %s, Created: %s) is linked to a non-existent project. Deleting',
30
					$environment->ID,
31
					$environment->Created
32
				));
33
				if (!$dryRun) {
34
					$environment->delete();
35
					$environment->destroy();
36
				}
37
				$count++;
38
			}
39
		}
40
41 View Code Duplication
		foreach (DNDeployment::get() as $deployment) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
42
			$environment = $deployment->Environment();
43
			if (!$environment || !$environment->exists()) {
44
				$log(sprintf(
45
					'Deployment (ID %s, Created: %s) is linked to a non-existent environment. Deleting',
46
					$deployment->ID,
47
					$deployment->Created
48
				));
49
				if (!$dryRun) {
50
					$deployment->delete();
51
					$deployment->destroy();
52
				}
53
				$count++;
54
			}
55
		}
56
57 View Code Duplication
		foreach (DNDataTransfer::get() as $transfer) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
58
			$environment = $transfer->Environment();
59
			if (!$environment || !$environment->exists()) {
60
				$log(sprintf(
61
					'Data transfer (ID %s, Created: %s) is linked to a non-existent environment. Deleting',
62
					$transfer->ID,
63
					$transfer->Created
64
				));
65
				if (!$dryRun) {
66
					$transfer->delete();
67
					$transfer->destroy();
68
				}
69
				$count++;
70
			}
71
		}
72
73 View Code Duplication
		foreach (DNDataArchive::get() as $archive) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
74
			$environment = $archive->Environment();
75
			if (!$environment || !$environment->exists()) {
76
				$log(sprintf(
77
					'Archive (ID %s, Created: %s) is linked to a non-existent environment. Deleting',
78
					$archive->ID,
79
					$archive->Created
80
				));
81
				if (!$dryRun) {
82
					$archive->delete();
83
					$archive->destroy();
84
				}
85
				$count++;
86
			}
87
		}
88
89 View Code Duplication
		foreach (DNGitFetch::get() as $fetch) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
90
			$project = $fetch->Project();
91
			if (!$project || !$project->exists()) {
92
				$log(sprintf(
93
					'Git fetch (ID %s, Created: %s) is linked to a non-existent project. Deleting',
94
					$fetch->ID,
95
					$fetch->Created
96
				));
97
				if (!$dryRun) {
98
					$fetch->delete();
99
					$fetch->destroy();
100
				}
101
				$count++;
102
			}
103
		}
104
105 View Code Duplication
		foreach (DNPing::get() as $ping) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
106
			$environment = $ping->Environment();
107
			if (!$environment || !$environment->exists()) {
108
				$log(sprintf(
109
					'Ping (ID %s, Created: %s) is linked to a non-existent environment. Deleting',
110
					$ping->ID,
111
					$ping->Created
112
				));
113
				if (!$dryRun) {
114
					$ping->delete();
115
					$ping->destroy();
116
				}
117
				$count++;
118
			}
119
		}
120
121
		$log(sprintf('Finished. Processed %s records', $count));
122
	}
123
124
}
125