Test Failed
Push — master ( 690440...5089f0 )
by Steve
09:47
created

Groups::seed()   D

Complexity

Conditions 15
Paths 88

Size

Total Lines 88
Code Lines 52

Duplication

Lines 10
Ratio 11.36 %

Importance

Changes 0
Metric Value
cc 15
eloc 52
nc 88
nop 0
dl 10
loc 88
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
				$member = $this->getRandomUser($members_exclude);
86
				if (!$member) {
87
					$member = $this->createUser();
88
					if (!$member) {
89
						continue;
90
					}
91
				}
92
93
				$members_exclude[] = $member->guid;
94
95
				if ($group->join($member)) {
96
					$this->log("User $member->name [guid: $member->guid] joined group $group->name [guid: $group->guid]");
97
				}
98
99
				if (!$group->isPublicMembership()) {
100
					$invitee = $this->getRandomUser($members_exclude);
101
					if ($invitee) {
102
						$members_exclude[] = $invitee->guid;
103
						if (!check_entity_relationship($invitee->guid, 'member', $group->guid)) {
104
							add_entity_relationship($group->guid, 'invited', $invitee->guid);
105
							$this->log("User $invitee->name [guid: $invitee->guid] was invited to $group->name [guid: $group->guid]");
106
						}
107
					}
108
109
					$requestor = $this->getRandomUser($members_exclude);
110
					if ($requestor) {
111
						$members_exclude[] = $requestor->guid;
112
						if (!check_entity_relationship($group->guid, 'invited', $requestor->guid)
113
							&& !check_entity_relationship($requestor->guid, 'member', $group->guid)
114
						) {
115
							add_entity_relationship($requestor->guid, 'membership_request', $group->guid);
116
							$this->log("User $invitee->name [guid: $invitee->guid] requested to join $group->name [guid: $group->guid]");
117
						}
118
					}
119
				}
120
			}
121
		}
122
123
	}
124
125
	/**
126
	 * {@inheritdoc}
127
	 */
128 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...
129
130
		$groups = elgg_get_entities_from_metadata([
131
			'types' => 'group',
132
			'metadata_names' => '__faker',
133
			'limit' => 0,
134
			'batch' => true,
135
		]);
136
137
		/* @var $groups \ElggBatch */
138
139
		$groups->setIncrementOffset(false);
140
141
		foreach ($groups as $group) {
142
			if ($group->delete()) {
143
				$this->log("Deleted group $group->guid");
144
			} else {
145
				$this->log("Failed to delete group $group->guid");
146
			}
147
		}
148
	}
149
150
	/**
151
	 * Returns random visibility value
152
	 * @return int
153
	 */
154
	public function getRandomVisibility() {
155
		$key = array_rand($this->visibility, 1);
156
157
		return $this->visibility[$key];
158
	}
159
160
	/**
161
	 * Returns random content access mode value
162
	 * @return string
163
	 */
164
	public function getRandomContentAccessMode() {
165
		$key = array_rand($this->content_access_modes, 1);
166
167
		return $this->content_access_modes[$key];
168
	}
169
170
	/**
171
	 * Returns random membership mode
172
	 * @return mixed
173
	 */
174
	public function getRandomMembership() {
175
		$key = array_rand($this->membership, 1);
176
177
		return $this->membership[$key];
178
	}
179
}
180