Completed
Push — feature/ss4-upgrade ( f41a3f )
by
unknown
10:12
created

DeleteRedundantAddonsTask   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 35
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 26 4
1
<?php
2
3
namespace SilverStripe\Addons\Tasks;
4
5
use SilverStripe\Dev\BuildTask;
6
use Elastica\Exception\NotFoundException;
7
use SilverStripe\Addons\Model\Addon;
8
9
/**
10
 * Deletes packages removed from Packagist.
11
 */
12
class DeleteRedundantAddonsTask extends BuildTask 
13
{
14
15
	protected $title = 'Delete Redundant Add-ons ';
16
17
	protected $description = 'Deletes packages removed from Packagist';
18
19
	public function run($request) 
20
	{
21
		$dateOneWeekAgo  = date('Y-m-d', strtotime('-1 week'));
22
23
		$addons = Addon::get()->filter('LastUpdated:LessThan' , $dateOneWeekAgo);
24
25
		foreach ($addons as $addon) {
26
			try {
27
				$addon->Keywords()->removeAll();
28
				$addon->Screenshots()->removeAll();
29
				$addon->CompatibleVersions()->removeAll();
30
31
				foreach ($addon->Versions() as $version) {
32
					$version->Authors()->removeAll();
33
					$version->Keywords()->removeAll();
34
					$version->CompatibleVersions()->removeAll();
35
					$version->delete();
36
				}
37
38
				$addon->delete();
39
			} catch(NotFoundException $e) {
40
				// no-op
41
			}
42
			
43
		}
44
	}
45
46
}
47