CleanupOrphanedRecordsTask::run()   F
last analyzed

Complexity

Conditions 28
Paths 16386

Size

Total Lines 119
Code Lines 80

Duplication

Lines 95
Ratio 79.83 %

Importance

Changes 0
Metric Value
dl 95
loc 119
rs 2
c 0
b 0
f 0
cc 28
eloc 80
nc 16386
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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, Name: %s, Created: %s) is linked to a non-existent project. Deleting',
30
					$environment->ID,
31
					$environment->Name,
32
					$environment->Created
33
				));
34
				if (!$dryRun) {
35
					$environment->delete();
36
					$environment->destroy();
37
				}
38
				$count++;
39
			}
40
		}
41
42 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...
43
			$environment = $deployment->Environment();
44
			if (!$environment || !$environment->exists()) {
45
				$log(sprintf(
46
					'Deployment (ID %s, Created: %s) is linked to a non-existent environment. Deleting',
47
					$deployment->ID,
48
					$deployment->Created
49
				));
50
				if (!$dryRun) {
51
					$deployment->delete();
52
					$deployment->destroy();
53
				}
54
				$count++;
55
			}
56
		}
57
58 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...
59
			$environment = $transfer->Environment();
60
			if (!$environment || !$environment->exists()) {
61
				$log(sprintf(
62
					'Data transfer (ID %s, Created: %s) is linked to a non-existent environment. Deleting',
63
					$transfer->ID,
64
					$transfer->Created
65
				));
66
				if (!$dryRun) {
67
					$transfer->delete();
68
					$transfer->destroy();
69
				}
70
				$count++;
71
			}
72
		}
73
74 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...
75
			$environment = $archive->Environment();
76
			if (!$environment || !$environment->exists()) {
77
				$log(sprintf(
78
					'Archive (ID %s, Created: %s) is linked to a non-existent environment. Deleting',
79
					$archive->ID,
80
					$archive->Created
81
				));
82
				if (!$dryRun) {
83
					$archive->delete();
84
					$archive->destroy();
85
				}
86
				$count++;
87
			}
88
		}
89
90 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...
91
			$project = $fetch->Project();
92
			if (!$project || !$project->exists()) {
93
				$log(sprintf(
94
					'Git fetch (ID %s, Created: %s) is linked to a non-existent project. Deleting',
95
					$fetch->ID,
96
					$fetch->Created
97
				));
98
				if (!$dryRun) {
99
					$fetch->delete();
100
					$fetch->destroy();
101
				}
102
				$count++;
103
			}
104
		}
105
106 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...
107
			$environment = $ping->Environment();
108
			if (!$environment || !$environment->exists()) {
109
				$log(sprintf(
110
					'Ping (ID %s, Created: %s) is linked to a non-existent environment. Deleting',
111
					$ping->ID,
112
					$ping->Created
113
				));
114
				if (!$dryRun) {
115
					$ping->delete();
116
					$ping->destroy();
117
				}
118
				$count++;
119
			}
120
		}
121
122
		$log(sprintf('Finished. Processed %s records', $count));
123
	}
124
125
}
126