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

engine/classes/Elgg/Upgrades/MigrateFriendsACL.php (3 issues)

1
<?php
2
3
namespace Elgg\Upgrades;
4
5
use Elgg\Upgrade\Batch;
6
use Elgg\Upgrade\Result;
7
8
/**
9
 * Creates user friends access collection and migrates entity access_id
10
 */
11
class MigrateFriendsACL implements Batch {
12
13
	/**
14
	 * {@inheritdoc}
15
	 */
16
	public function getVersion() {
17
		return 2017121200;
18
	}
19
20
	/**
21
	 * {@inheritdoc}
22
	 */
23
	public function needsIncrementOffset() {
24
		return false;
25
	}
26
27
	/**
28
	 * {@inheritdoc}
29
	 */
30
	public function shouldBeSkipped() {
31
		return empty($this->countItems());
32
	}
33
34
	/**
35
	 * {@inheritdoc}
36
	 */
37
	public function countItems() {
38
		// users without a friends acl
39
		$db = elgg()->getDb();
40
		
41
		return elgg_get_entities([
42
			'type' => 'user',
43
			'count' => true,
44
			'wheres' => [
45
				"e.guid NOT IN (
46
					SELECT acl.owner_guid FROM {$db->prefix}access_collections acl WHERE acl.subtype = 'friends'
47
				)",
48
			],
49
		]);
50
	}
51
52
	/**
53
	 * {@inheritdoc}
54
	 */
55
	public function run(Result $result, $offset) {
56
		
57
		$db = elgg()->getDb();
58
		
59
		$users = elgg_get_entities([
60
			'type' => 'user',
61
			'limit' => 1,
62
			'offset' => $offset,
63
			'wheres' => [
64
				"e.guid NOT IN (
65
					SELECT acl.owner_guid FROM {$db->prefix}access_collections acl WHERE acl.subtype = 'friends'
66
				)",
67
			],
68
		]);
69
		
70
		if (empty($users)) {
71
			// mark as complete
72
			$result->addSuccesses(1);
73
			return $result;
74
		}
75
		
76
		$user = $users[0];
77
		
78
		// create acl
79
		$acl_id = create_access_collection('friends', $user->guid, 'friends');
80
		if (!$acl_id) {
81
			$result->addError('Failed to create an ACL for user');
82
			return $result;
83
		}
84
		
85
		$acl = get_access_collection($acl_id);
86
		
87
		$this->addFriendsToACL($user, $acl);
0 ignored issues
show
$acl of type false is incompatible with the type ElggAccessCollection expected by parameter $acl of Elgg\Upgrades\MigrateFriendsACL::addFriendsToACL(). ( Ignorable by Annotation )

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

87
		$this->addFriendsToACL($user, /** @scrutinizer ignore-type */ $acl);
Loading history...
88
		$this->updateEntities($user, $acl);
0 ignored issues
show
$acl of type false is incompatible with the type ElggAccessCollection expected by parameter $acl of Elgg\Upgrades\MigrateFriendsACL::updateEntities(). ( Ignorable by Annotation )

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

88
		$this->updateEntities($user, /** @scrutinizer ignore-type */ $acl);
Loading history...
89
		$this->updateAnnotations($user, $acl);
0 ignored issues
show
$acl of type false is incompatible with the type ElggAccessCollection expected by parameter $acl of Elgg\Upgrades\MigrateFri...CL::updateAnnotations(). ( Ignorable by Annotation )

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

89
		$this->updateAnnotations($user, /** @scrutinizer ignore-type */ $acl);
Loading history...
90
		
91
		$result->addSuccesses(1);
92
		
93
		return $result;
94
	}
95
	
96
	protected function addFriendsToACL(\ElggUser $user, \ElggAccessCollection $acl) {
97
		$friends = $user->getFriends(['batch' => true]);
98
		foreach ($friends as $friend) {
99
			$acl->addMember($friend->guid);
100
		}
101
	}
102
	
103
	protected function updateEntities(\ElggUser $user, \ElggAccessCollection $acl) {
104
		$entities = elgg_get_entities([
105
			'type' => 'object',
106
			'owner_guid' => $user->guid,
107
			'access_id' => ACCESS_FRIENDS,
108
			'limit' => false,
109
			'batch' => true,
110
		]);
111
		
112
		foreach ($entities as $entity) {
113
			$entity->access_id = $acl->id;
114
			$entity->save();
115
		}
116
	}
117
	
118
	protected function updateAnnotations(\ElggUser $user, \ElggAccessCollection $acl) {
119
		$annotations = elgg_get_annotations([
120
			'type' => 'object',
121
			'owner_guid' => $user->guid,
122
			'access_id' => ACCESS_FRIENDS,
123
			'limit' => false,
124
			'batch' => true,
125
		]);
126
		
127
		foreach ($annotations as $annotation) {
128
			$annotation->access_id = $acl->id;
129
			$annotation->save();
130
		}
131
	}
132
133
}
134