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

add_entity_relationship()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 2
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
crap 1
1
<?php
2
/**
3
 * Elgg relationships.
4
 *
5
 * @package    Elgg.Core
6
 * @subpackage DataModel.Relationship
7
 */
8
9
/**
10
 * Get a relationship by its ID
11
 *
12
 * @param int $id The relationship ID
13
 *
14
 * @return \ElggRelationship|false False if not found
15
 */
16
function get_relationship($id) {
17 7
	return _elgg_services()->relationshipsTable->get($id);
18
}
19
20
/**
21
 * Delete a relationship by its ID
22
 *
23
 * @param int $id The relationship ID
24
 *
25
 * @return bool
26
 */
27
function delete_relationship($id) {
28 3
	return _elgg_services()->relationshipsTable->delete($id);
29
30
}
31
32
/**
33
 * Create a relationship between two entities. E.g. friendship, group membership, site membership.
34
 *
35
 * This function lets you make the statement "$guid_one is a $relationship of $guid_two". In the statement,
36
 * $guid_one is the subject of the relationship, $guid_two is the target, and $relationship is the type.
37
 *
38
 * @param int    $guid_one     GUID of the subject entity of the relationship
39
 * @param string $relationship Type of the relationship
40
 * @param int    $guid_two     GUID of the target entity of the relationship
41
 *
42
 * @return bool
43
 * @throws InvalidArgumentException
44
 */
45
function add_entity_relationship($guid_one, $relationship, $guid_two) {
46 115
	return _elgg_services()->relationshipsTable->add($guid_one, $relationship, $guid_two);
47
}
48
49
/**
50
 * Check if a relationship exists between two entities. If so, the relationship object is returned.
51
 *
52
 * This function lets you ask "Is $guid_one a $relationship of $guid_two?"
53
 *
54
 * @param int    $guid_one     GUID of the subject entity of the relationship
55
 * @param string $relationship Type of the relationship
56
 * @param int    $guid_two     GUID of the target entity of the relationship
57
 *
58
 * @return \ElggRelationship|false Depending on success
59
 */
60
function check_entity_relationship($guid_one, $relationship, $guid_two) {
61 107
	return _elgg_services()->relationshipsTable->check($guid_one, $relationship, $guid_two);
62
}
63
64
/**
65
 * Delete a relationship between two entities.
66
 *
67
 * This function lets you say "$guid_one is no longer a $relationship of $guid_two."
68
 *
69
 * @param int    $guid_one     GUID of the subject entity of the relationship
70
 * @param string $relationship Type of the relationship
71
 * @param int    $guid_two     GUID of the target entity of the relationship
72
 *
73
 * @return bool
74
 */
75
function remove_entity_relationship($guid_one, $relationship, $guid_two) {
76 14
	return _elgg_services()->relationshipsTable->remove($guid_one, $relationship, $guid_two);
77
}
78
79
/**
80
 * Removes all relationships originating from a particular entity
81
 *
82
 * @param int    $guid                 GUID of the subject or target entity (see $inverse)
83
 * @param string $relationship         Type of the relationship (optional, default is all relationships)
84
 * @param bool   $inverse_relationship Is $guid the target of the deleted relationships? By default, $guid is the
85
 *                                     subject of the relationships.
86
 * @param string $type                 The type of entity related to $guid (defaults to all)
87
 *
88
 * @return true
89
 */
90
function remove_entity_relationships($guid, $relationship = "", $inverse_relationship = false, $type = '') {
91 214
	return _elgg_services()->relationshipsTable->removeAll($guid, $relationship, $inverse_relationship, $type);
92
}
93
94
/**
95
 * Get all the relationships for a given GUID.
96
 *
97
 * @param int  $guid                 GUID of the subject or target entity (see $inverse)
98
 * @param bool $inverse_relationship Is $guid the target of the relationships? By default $guid is
99
 *                                   the subject of the relationships.
100
 *
101
 * @return \ElggRelationship[]
102
 */
103
function get_entity_relationships($guid, $inverse_relationship = false) {
104
	return _elgg_services()->relationshipsTable->getAll($guid, $inverse_relationship);
105
}
106
107
/**
108
 * Gets the number of entities by a the number of entities related to them in a particular way.
109
 * This is a good way to get out the users with the most friends, or the groups with the
110
 * most members.
111
 *
112
 * @param array $options An options array compatible with elgg_get_entities_from_relationship()
113
 *
114
 * @return \ElggEntity[]|int|boolean If count, int. If not count, array. false on errors.
115
 * @since 1.8.0
116
 */
117
function elgg_get_entities_from_relationship_count(array $options = []) {
118 1
	return _elgg_services()->relationshipsTable->getEntitiesFromCount($options);
119
}
120
121
/**
122
 * Returns a list of entities by relationship count
123
 *
124
 * @see elgg_get_entities_from_relationship_count()
125
 *
126
 * @param array $options Options array
127
 *
128
 * @return string
129
 * @since 1.8.0
130
 */
131
function elgg_list_entities_from_relationship_count($options) {
132
	return elgg_list_entities($options, 'elgg_get_entities_from_relationship_count');
133
}
134
135
/**
136
 * Register relationship unit tests
137
 *
138
 * @param string $hook  'unit_test'
139
 * @param string $type  'system'
140
 * @param array  $tests current return value
141
 *
142
 * @return array
143
 *
144
 * @access private
145
 * @codeCoverageIgnore
146
 */
147
function _elgg_relationships_test($hook, $type, $tests) {
2 ignored issues
show
Unused Code introduced by
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

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

147
function _elgg_relationships_test(/** @scrutinizer ignore-unused */ $hook, $type, $tests) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

147
function _elgg_relationships_test($hook, /** @scrutinizer ignore-unused */ $type, $tests) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
	$tests[] = ElggRelationshipUnitTest::class;
0 ignored issues
show
Bug introduced by
The type ElggRelationshipUnitTest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
149
	return $tests;
150
}
151
152
153
/**
154
 * Initialize the relationship library
155
 *
156
 * @return void
157
 *
158
 * @access private
159
 */
160
function _elgg_relationship_init() {
161 31
	elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_relationships_test');
162 31
}
163
164
/**
165
 * @see \Elgg\Application::loadCore Do not do work here. Just register for events.
166
 */
167
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
1 ignored issue
show
Unused Code introduced by
The parameter $hooks is not used and could be removed. ( Ignorable by Annotation )

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

167
return function(\Elgg\EventsService $events, /** @scrutinizer ignore-unused */ \Elgg\HooksRegistrationService $hooks) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
168 18
	$events->registerHandler('init', 'system', '_elgg_relationship_init');
169
};
170