Completed
Push — 2.3 ( 07b9ab...95473c )
by Jeroen
24:31 queued 11:55
created

Groups::seed()   D

Complexity

Conditions 15
Paths 88

Size

Total Lines 90
Code Lines 52

Duplication

Lines 10
Ratio 11.11 %

Importance

Changes 0
Metric Value
cc 15
eloc 52
nc 88
nop 0
dl 10
loc 90
rs 4.9121
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Elgg\Database\Seeds;
4
5
/**
6
 * Seed users
7
 *
8
 * @access private
9
 */
10
class Groups extends Seed {
11
12
	/**
13
	 * @var \ElggUser[]
14
	 */
15
	private $users;
0 ignored issues
show
Unused Code introduced by
The property $users is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
17
	private $visibility = [
18
		ACCESS_PUBLIC,
19
		ACCESS_LOGGED_IN,
20
		ACCESS_PRIVATE,
21
	];
22
23
	private $content_access_modes = [
24
		\ElggGroup::CONTENT_ACCESS_MODE_MEMBERS_ONLY,
25
		\ElggGroup::CONTENT_ACCESS_MODE_UNRESTRICTED,
26
	];
27
28
	private $membership = [
29
		ACCESS_PUBLIC,
30
		ACCESS_PRIVATE,
31
	];
32
33
	/**
34
	 * {@inheritdoc}
35
	 */
36
	public function seed() {
37
38
		$count_groups = function () {
39
			return elgg_get_entities_from_metadata([
40
				'types' => 'group',
41
				'metadata_names' => '__faker',
42
				'count' => true,
43
			]);
44
		};
45
46 View Code Duplication
		$count_members = function ($group) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
			return elgg_get_entities_from_relationship([
48
				'types' => 'user',
49
				'relationship' => 'member',
50
				'relationship_guid' => $group->getGUID(),
51
				'inverse_relationship' => true,
52
				'metadata_names' => '__faker',
53
				'count' => true,
54
			]);
55
		};
56
57
		$exclude = [];
58
59
		while ($count_groups() < $this->limit) {
60
			$group = $this->getRandomGroup($exclude);
61
			if (!$group) {
62
				$group = $this->createGroup([
63
					'access_id' => $this->getRandomVisibility(),
64
				], [
65
					'content_access_mode' => $this->getRandomContentAccessMode(),
66
					'membership' => $this->getRandomMembership(),
67
				]);
68
				if (!$group) {
69
					continue;
70
				}
71
			}
72
73
			$exclude[] = $group->guid;
74
75
			if ($count_members($group) > 1) {
76
				// exclude owner from count
77
				continue;
78
			}
79
80
			$members_limit = $this->faker->numberBetween(5, 10);
81
82
			$members_exclude = [];
83
84
			while ($count_members($group) - 1 < $members_limit) {
85
86
				$member = $this->getRandomUser($members_exclude);
87
				if (!$member) {
88
					$member = $this->createUser();
89
					if (!$member) {
90
						continue;
91
					}
92
				}
93
94
				$members_exclude[] = $member->guid;
95
96
				if ($group->join($member)) {
97
					$this->log("User $member->name [guid: $member->guid] joined group $group->name [guid: $group->guid]");
98
				}
99
100
				if (!$group->isPublicMembership()) {
101
					$invitee = $this->getRandomUser($members_exclude);
102
					if ($invitee) {
103
						$members_exclude[] = $invitee->guid;
104
						if (!check_entity_relationship($invitee->guid, 'member', $group->guid)) {
105
							add_entity_relationship($group->guid, 'invited', $invitee->guid);
106
							$this->log("User $invitee->name [guid: $invitee->guid] was invited to $group->name [guid: $group->guid]");
107
						}
108
					}
109
110
					$requestor = $this->getRandomUser($members_exclude);
111
					if ($requestor) {
112
						$members_exclude[] = $requestor->guid;
113
						if (!check_entity_relationship($group->guid, 'invited', $requestor->guid)
114
							&& !check_entity_relationship($requestor->guid, 'member', $group->guid)
115
						) {
116
							add_entity_relationship($requestor->guid, 'membership_request', $group->guid);
117
							$this->log("User $invitee->name [guid: $invitee->guid] requested to join $group->name [guid: $group->guid]");
118
						}
119
					}
120
				}
121
			}
122
123
		}
124
125
	}
126
127
	/**
128
	 * {@inheritdoc}
129
	 */
130 View Code Duplication
	public function unseed() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
132
		$groups = elgg_get_entities_from_metadata([
133
			'types' => 'group',
134
			'metadata_names' => '__faker',
135
			'limit' => 0,
136
			'batch' => true,
137
		]);
138
139
		/* @var $groups \ElggBatch */
140
141
		$groups->setIncrementOffset(false);
142
143
		foreach ($groups as $group) {
144
			if ($group->delete()) {
145
				$this->log("Deleted group $group->guid");
146
			} else {
147
				$this->log("Failed to delete group $group->guid");
148
			}
149
		}
150
	}
151
152
	/**
153
	 * Returns random visibility value
154
	 * @return int
155
	 */
156
	public function getRandomVisibility() {
157
		$key = array_rand($this->visibility, 1);
158
159
		return $this->visibility[$key];
160
	}
161
162
	/**
163
	 * Returns random content access mode value
164
	 * @return string
165
	 */
166
	public function getRandomContentAccessMode() {
167
		$key = array_rand($this->content_access_modes, 1);
168
169
		return $this->content_access_modes[$key];
170
	}
171
172
	/**
173
	 * Returns random membership mode
174
	 * @return mixed
175
	 */
176
	public function getRandomMembership() {
177
		$key = array_rand($this->membership, 1);
178
179
		return $this->membership[$key];
180
	}
181
}