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

PublicLikesAnnotations::countItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 16
ccs 0
cts 13
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Elgg\Likes\Upgrades;
3
4
use Elgg\Upgrade\Batch;
5
use Elgg\Upgrade\Result;
6
7
class PublicLikesAnnotations implements Batch {
8
	
9
	/**
10
	 * {@inheritdoc}
11
	 */
12
	public function getVersion() {
13
		return 2017120700;
14
	}
15
16
	/**
17
	 * {@inheritdoc}
18
	 */
19
	public function needsIncrementOffset() {
20
		return false;
21
	}
22
	
23
	/**
24
	 * {@inheritdoc}
25
	 */
26
	public function shouldBeSkipped() {
27
		return empty($this->countItems());
28
	}
29
	
30
	/**
31
	 * {@inheritdoc}
32
	 */
33
	public function countItems() {
34
		$dbprefix = elgg_get_config('dbprefix');
35
		$public = ACCESS_PUBLIC;
36
		
37
		$query = "SELECT COUNT(*) as total
38
			FROM {$dbprefix}annotations
39
			WHERE name = 'likes'
40
			AND access_id != {$public}
41
		";
42
		
43
		$row = get_data_row($query);
44
		if (empty($row)) {
45
			return 0;
46
		}
47
		
48
		return (int) $row->total;
49
	}
50
	
51
	/**
52
	 * {@inheritDoc}
53
	 */
54
	public function run(Result $result, $offset) {
55
		$dbprefix = elgg_get_config('dbprefix');
56
		$public = ACCESS_PUBLIC;
57
		
58
		$query = "UPDATE {$dbprefix}annotations
59
			SET access_id = {$public}
60
			WHERE name = 'likes'
61
			AND access_id != {$public}
62
		";
63
		
64
		$count = update_data($query, [], true);
65
		
66
		$result->addSuccesses($count);
0 ignored issues
show
Bug introduced by Jeroen Dalsem
$count of type boolean is incompatible with the type integer expected by parameter $num of Elgg\Upgrade\Result::addSuccesses(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
		$result->addSuccesses(/** @scrutinizer ignore-type */ $count);
Loading history...
67
		
68
		return $result;
69
	}
70
}