Completed
Pull Request — master (#713)
by Sean
03:55
created

CleanupOrphanedRecordsTask::run()   F

Complexity

Conditions 16
Paths 243

Size

Total Lines 81
Code Lines 57

Duplication

Lines 56
Ratio 69.14 %

Importance

Changes 0
Metric Value
dl 56
loc 81
rs 3.9126
c 0
b 0
f 0
cc 16
eloc 57
nc 243
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 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...
7
			$message = sprintf('[%s] ', date('Y-m-d H:i:s')) . $message;
8
			echo $message . PHP_EOL;
9
		};
10
11
		$count = 0;
12
13
		foreach (DNDeployment::get() as $deployment) {
14
			$environment = $deployment->Environment();
15
			if (!$environment || !$environment->exists()) {
16
				$log(sprintf(
17
					'Deployment (ID %s, Created: %s, State: %s) is linked to a non-existent environment. Deleting',
18
					$deployment->ID,
19
					$deployment->Created,
20
					$deployment->State
21
				));
22
				$deployment->delete();
23
				$deployment->destroy();
24
				$count++;
25
			}
26
		}
27
28 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...
29
			$environment = $transfer->Environment();
30
			if (!$environment || !$environment->exists()) {
31
				$log(sprintf(
32
					'Data transfer (ID %s, Created: %s) is linked to a non-existent environment. Deleting',
33
					$transfer->ID,
34
					$transfer->Created
35
				));
36
				$transfer->delete();
37
				$transfer->destroy();
38
				$count++;
39
			}
40
		}
41
42 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...
43
			$environment = $archive->Environment();
44
			if (!$environment || !$environment->exists()) {
45
				$log(sprintf(
46
					'Archive (ID %s, Created: %s) is linked to a non-existent environment. Deleting',
47
					$archive->ID,
48
					$archive->Created
49
				));
50
				$archive->delete();
51
				$archive->destroy();
52
				$count++;
53
			}
54
		}
55
56 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...
57
			$project = $fetch->Project();
58
			if (!$project || !$project->exists()) {
59
				$log(sprintf(
60
					'Git fetch (ID %s, Created: %s) is linked to a non-existent project. Deleting',
61
					$fetch->ID,
62
					$fetch->Created
63
				));
64
				$fetch->delete();
65
				$fetch->destroy();
66
				$count++;
67
			}
68
		}
69
70 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...
71
			$environment = $ping->Environment();
72
			if (!$environment || !$environment->exists()) {
73
				$log(sprintf(
74
					'Ping (ID %s, Created: %s) is linked to a non-existent environment. Deleting',
75
					$ping->ID,
76
					$ping->Created
77
				));
78
				$ping->delete();
79
				$ping->destroy();
80
				$count++;
81
			}
82
		}
83
84
		$log(sprintf('Finished. Processed %s records', $count));
85
	}
86
87
}
88