Completed
Push — master ( d75d5d...45122e )
by David
04:06
created

Wordlift_User_Service   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 22
lcom 1
cbo 2
dl 0
loc 244
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A get_instance() 0 4 1
A get_uri() 0 17 3
B wp_insert_post() 0 26 5
A _get_uri() 0 10 2
A _build_uri() 0 14 3
A _set_uri() 0 4 1
B get_delete_query() 0 26 2
B get_insert_query() 0 24 4
1
<?php
2
3
/**
4
 * Manage user-related functions. This class receives notifications when a post is created/updated and pushes the author's
5
 * data to the triple store. It does NOT receive notification when a user is create/updated because we don't want to send
6
 * to the triple stores users that eventually do not write posts (therefore if user data change, the triple store is updated
7
 * only when the user creates/updates a new post).
8
 *
9
 * @since 3.1.7
10
 */
11
class Wordlift_User_Service {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
12
13
	/**
14
	 * The meta key where the user's URI is stored.
15
	 *
16
	 * @since 3.1.7
17
	 */
18
	const URI_META_KEY = '_wl_uri';
19
20
	/**
21
	 * The Log service.
22
	 *
23
	 * @since 3.1.7
24
	 * @access private
25
	 * @var \Wordlift_Log_Service $log_service The Log service.
26
	 */
27
	private $log_service;
28
29
	/**
30
	 * The singleton instance of the User service.
31
	 *
32
	 * @since 3.1.7
33
	 * @access private
34
	 * @var \Wordlift_User_Service $user_service The singleton instance of the User service.
35
	 */
36
	private static $instance;
37
38
	/**
39
	 * Create an instance of the User service.
40
	 *
41
	 * @since 3.1.7
42
	 */
43
	public function __construct() {
44
45
		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' );
46
47
		self::$instance = $this;
48
49
	}
50
51
	/**
52
	 * Get the singleton instance of the User service.
53
	 *
54
	 * @since 3.1.7
55
	 * @return \Wordlift_User_Service The singleton instance of the User service.
56
	 */
57
	public static function get_instance() {
58
59
		return self::$instance;
60
	}
61
62
	/**
63
	 * Get the URI for a user.
64
	 *
65
	 * @since 3.1.7
66
	 *
67
	 * @param int $user_id The user id
68
	 *
69
	 * @return false|string The user's URI or false in case of failure.
70
	 */
71
	public function get_uri( $user_id ) {
72
73
		// Try to get the URI stored in the user's meta and return it if available.
74
		if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) {
75
			return $user_uri;
76
		}
77
78
		// Try to build an URI, return false in case of failure.
79
		if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) {
80
			return false;
81
		}
82
83
		// Store the URI for future requests (we need a "permanent" URI).
84
		$this->_set_uri( $user_id, $user_uri );
85
86
		return $user_uri;
87
	}
88
89
	/**
90
	 * Receives wp_insert_post events.
91
	 *
92
	 * @since 3.1.7
93
	 *
94
	 * @param int $post_id Post ID.
95
	 * @param WP_Post $post Post object.
96
	 * @param bool $update Whether this is an existing post being updated or not.
97
	 */
98
	public function wp_insert_post( $post_id, $post, $update ) {
0 ignored issues
show
Unused Code introduced by
The parameter $update is not used and could be removed.

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

Loading history...
99
100
		// If the post is not published, return.
101
		if ( 'publish' !== get_post_status( $post_id ) ) {
102
			return;
103
		}
104
105
		// We expect a numeric author id.
106
		if ( ! is_numeric( $post->post_author ) ) {
107
			return;
108
		}
109
110
		// Get the delete query,or return in case of failure.
111
		if ( false === ( $delete = $this->get_delete_query( $post->post_author ) ) ) {
112
			return;
113
		}
114
115
		// Get the insert query,or return in case of failure.
116
		if ( false === ( $insert = $this->get_insert_query( $post->post_author ) ) ) {
117
			return;
118
		}
119
120
		// Send the query to the triple store.
121
		rl_execute_sparql_update_query( $delete . $insert );
122
123
	}
124
125
	/**
126
	 * Get the user's URI stored in the user's meta.
127
	 *
128
	 * @since 3.1.7
129
	 *
130
	 * @param int $user_id The user id.
131
	 *
132
	 * @return false|string The user's URI or false if not found.
133
	 */
134
	private function _get_uri( $user_id ) {
1 ignored issue
show
Coding Style introduced by
Method name "_get_uri" should not be prefixed with an underscore to indicate visibility
Loading history...
135
136
		$user_uri = get_user_meta( $user_id, self::URI_META_KEY, true );
137
138
		if ( empty( $user_uri ) ) {
139
			return false;
140
		}
141
142
		return $user_uri;
143
	}
144
145
	/**
146
	 * Build an URI for a user.
147
	 *
148
	 * @since 3.1.7
149
	 *
150
	 * @param int $user_id The user's id.
151
	 *
152
	 * @return false|string The user's URI or false in case of failure.
153
	 */
154
	private function _build_uri( $user_id ) {
1 ignored issue
show
Coding Style introduced by
Method name "_build_uri" should not be prefixed with an underscore to indicate visibility
Loading history...
155
156
		// Get the user, return false in case of failure.
157
		if ( false === ( $user = get_userdata( $user_id ) ) ) {
158
			return false;
159
		};
160
161
		// If the nicename is not set, return a failure.
162
		if ( empty( $user->user_nicename ) ) {
163
			return false;
164
		}
165
166
		return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename";
167
	}
168
169
	/**
170
	 * Store the URI in user's meta.
171
	 *
172
	 * @since 3.1.7
173
	 *
174
	 * @param int $user_id The user's id.
175
	 * @param string $user_uri The user's uri.
176
	 *
177
	 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
178
	 */
179
	private function _set_uri( $user_id, $user_uri ) {
1 ignored issue
show
Coding Style introduced by
Method name "_set_uri" should not be prefixed with an underscore to indicate visibility
Loading history...
180
181
		return update_user_meta( $user_id, self::URI_META_KEY, $user_uri );
182
	}
183
184
	/**
185
	 * Get the delete query.
186
	 *
187
	 * @since 3.1.7
188
	 *
189
	 * @param int $user_id The user id.
190
	 *
191
	 * @return false|string The delete query or false in case of failure.
192
	 */
193
	private function get_delete_query( $user_id ) {
194
195
		// Get the URI, return if there's none.
196
		if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) {
197
			return false;
198
		}
199
200
		// Build the delete query.
201
		$query = Wordlift_Query_Builder::new_instance()->delete()
202
		                               ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' )
203
		                               ->build()
204
		         . Wordlift_Query_Builder::new_instance()->delete()
205
		                                 ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o' )
206
		                                 ->build()
207
		         . Wordlift_Query_Builder::new_instance()->delete()
208
		                                 ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o' )
209
		                                 ->build()
210
		         . Wordlift_Query_Builder::new_instance()->delete()
211
		                                 ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o' )
212
		                                 ->build()
213
		         . Wordlift_Query_Builder::new_instance()->delete()
214
		                                 ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' )
215
		                                 ->build();
216
217
		return $query;
218
	}
219
220
	/**
221
	 * Get the insert query.
222
	 *
223
	 * @since 3.1.7
224
	 *
225
	 * @param int $user_id The user id.
226
	 *
227
	 * @return false|string The insert query or false in case of failure.
228
	 */
229
	private function get_insert_query( $user_id ) {
230
231
		// Get the URI, return if there's none.
232
		if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) {
233
			return false;
234
		}
235
236
		// Try to get the user data, in case of failure return false.
237
		if ( false === ( $user = get_userdata( $user_id ) ) ) {
238
			return false;
239
		};
240
241
		// Build the insert query.
242
		$query = Wordlift_Query_Builder::new_instance()
243
		                               ->insert()
244
		                               ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI )
245
		                               ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name )
246
		                               ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname )
247
		                               ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname )
248
		                               ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty( $user->user_url ) ? $user->user_url : get_author_posts_url( $user_id ) ) )
249
		                               ->build();
250
251
		return $query;
252
	}
253
254
}
1 ignored issue
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...
255