Groups   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 69
dl 0
loc 133
ccs 0
cts 75
cp 0
rs 10
c 0
b 0
f 0
wmc 19

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 2 1
A getCountOptions() 0 3 1
A unseed() 0 21 3
C seed() 0 85 14
1
<?php
2
3
namespace Elgg\Database\Seeds;
4
5
use Elgg\Exceptions\Seeding\MaxAttemptsException;
6
7
/**
8
 * Seed users
9
 *
10
 * @internal
11
 */
12
class Groups extends Seed {
13
14
	/**
15
	 * {@inheritdoc}
16
	 */
17
	public function seed() {
18
		$this->advance($this->getCount());
19
20
		$count_members = function ($group) {
21
			return elgg_count_entities([
22
				'types' => 'user',
23
				'relationship' => 'member',
24
				'relationship_guid' => $group->getGUID(),
25
				'inverse_relationship' => true,
26
				'metadata_names' => '__faker',
27
			]);
28
		};
29
30
		$exclude = [];
31
32
		$profile_fields_config = _elgg_services()->fields->get('group', 'group');
33
		$profile_fields = [];
34
		foreach ($profile_fields_config as $field) {
35
			$profile_fields[$field['name']] = $field['#type'];
36
		}
37
		
38
		while ($this->getCount() < $this->limit) {
39
			try {
40
				$group = $this->createGroup([
41
					'access_id' => $this->getRandomGroupVisibility(),
42
					'content_access_mode' => $this->getRandomGroupContentAccessMode(),
43
					'membership' => $this->getRandomGroupMembership(),
44
				], [
45
					'profile_fields' => $profile_fields,
46
					'group_tool_options' => _elgg_services()->group_tools->all(),
47
				]);
48
			} catch (MaxAttemptsException $e) {
49
				// unable to create a group with the given options
50
				continue;
51
			}
52
53
			$this->createIcon($group);
54
55
			$exclude[] = $group->guid;
56
57
			if ($count_members($group) > 1) {
58
				// exclude owner from count
59
				continue;
60
			}
61
62
			$members_limit = $this->faker()->numberBetween(1, 5);
63
64
			$members_exclude = [];
65
66
			while ($count_members($group) - 1 < $members_limit) {
67
				$member = $this->getRandomUser($members_exclude);
68
				if (!$member) {
69
					continue;
70
				}
71
72
				$members_exclude[] = $member->guid;
73
74
				if ($group->join($member)) {
75
					$this->log("User {$member->getDisplayName()} [guid: {$member->guid}] joined group {$group->getDisplayName()} [guid: {$group->guid}]");
76
				}
77
78
				if (!$group->isPublicMembership()) {
79
					$invitee = $this->getRandomUser($members_exclude);
80
					if ($invitee) {
81
						$members_exclude[] = $invitee->guid;
82
						if (!$group->isMember($invitee)) {
83
							$group->addRelationship($invitee->guid, 'invited');
84
							$this->log("User {$invitee->getDisplayName()} [guid: {$invitee->guid}] was invited to {$group->getDisplayName()} [guid: {$group->guid}]");
85
						}
86
					}
87
88
					$requestor = $this->getRandomUser($members_exclude);
89
					if ($requestor) {
90
						$members_exclude[] = $requestor->guid;
91
						if (!$group->hasRelationship($requestor->guid, 'invited')
92
							&& !$group->isMember($requestor)
93
						) {
94
							$requestor->addRelationship($group->guid, 'membership_request');
95
							$this->log("User {$invitee->getDisplayName()} [guid: {$invitee->guid}] requested to join {$group->getDisplayName()} [guid: {$group->guid}]");
96
						}
97
					}
98
				}
99
			}
100
101
			$this->advance();
102
		}
103
	}
104
105
	/**
106
	 * {@inheritdoc}
107
	 */
108
	public function unseed() {
109
		/* @var $groups \ElggBatch */
110
		$groups = elgg_get_entities([
111
			'type' => 'group',
112
			'metadata_name' => '__faker',
113
			'limit' => false,
114
			'batch' => true,
115
			'batch_inc_offset' => false,
116
		]);
117
118
		/* @var $group \ElggGroup */
119
		foreach ($groups as $group) {
120
			if ($group->delete(true, true)) {
121
				$this->log("Deleted group {$group->guid}");
122
			} else {
123
				$this->log("Failed to delete group {$group->guid}");
124
				$groups->reportFailure();
125
				continue;
126
			}
127
128
			$this->advance();
129
		}
130
	}
131
132
	/**
133
	 * {@inheritDoc}
134
	 */
135
	public static function getType(): string {
136
		return 'group';
137
	}
138
	
139
	/**
140
	 * {@inheritDoc}
141
	 */
142
	protected function getCountOptions(): array {
143
		return [
144
			'type' => 'group',
145
		];
146
	}
147
}
148