Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

MigratePageTop   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 84
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A migrate() 0 15 2
A getVersion() 0 2 1
A shouldBeSkipped() 0 2 1
A needsIncrementOffset() 0 2 1
A countItems() 0 5 1
A run() 0 23 3
1
<?php
2
3
namespace Elgg\Pages\Upgrades;
4
5
use ElggObject;
6
use \Elgg\Upgrade\Batch;
7
use \Elgg\Upgrade\Result;
8
9
/**
10
 * Migrate 'object', 'page_top' to 'object', 'page'
11
 * also set metadata 'parent_guid' to 0
12
 *
13
 * @since 3.0
14
 */
15
class MigratePageTop implements Batch {
16
	
17
	/**
18
	 * {@inheritDoc}
19
	 */
20
	public function getVersion() {
21
		return 2017110700;
22
	}
23
	
24
	/**
25
	 * {@inheritDoc}
26
	 */
27
	public function shouldBeSkipped() {
28
		return empty($this->countItems());
29
	}
30
	
31
	/**
32
	 * {@inheritDoc}
33
	 */
34
	public function countItems() {
35
		return elgg_get_entities([
1 ignored issue
show
Bug Best Practice introduced by
The expression return elgg_get_entities...top', 'count' => true)) also could return the type ElggEntity[]|false which is incompatible with the return type mandated by Elgg\Upgrade\Batch::countItems() of integer.
Loading history...
36
			'type' => 'object',
37
			'subtype' => 'page_top',
38
			'count' => true,
39
		]);
40
	}
41
	
42
	/**
43
	 * {@inheritDoc}
44
	 */
45
	public function needsIncrementOffset() {
46
		return false;
47
	}
48
	
49
	/**
50
	 * {@inheritDoc}
51
	 */
52
	public function run(Result $result, $offset) {
53
		
54
		/* @var \ElggBatch $page_tops */
55
		$page_tops = elgg_get_entities([
56
			'type' => 'object',
57
			'subtype' => 'page_top',
58
			'offset' => $offset,
59
			'limit' => 25,
60
			'batch' => true,
61
			'batch_inc_offset' => $this->needsIncrementOffset(),
62
		]);
63
		
64
		/* @var $page_top \ElggObject */
65
		foreach ($page_tops as $page_top) {
66
			if ($this->migrate($page_top)) {
67
				$result->addSuccesses();
68
			} else {
69
				$result->addError("Error migrating page_top: {$page_top->guid}");
70
				$result->addFailures();
71
			}
72
		}
73
		
74
		return $result;
75
	}
76
	
77
	/**
78
	 * Migrate one page_top to a page
79
	 *
80
	 * @param ElggObject $page_top the top page to migrate
81
	 *
82
	 * @return bool
83
	 */
84
	protected  function migrate(ElggObject $page_top) {
85
		
86
		$dbprefix = elgg_get_config('dbprefix');
87
		$query = "UPDATE {$dbprefix}entities
88
			SET subtype = 'page'
89
			WHERE guid = {$page_top->guid}
90
		";
91
		
92
		if (!update_data($query)) {
93
			return false;
94
		}
95
		
96
		$page_top->parent_guid = 0;
97
		
98
		return true;
99
	}
100
}
101