Completed
Push — develop ( a1bd27...53d72c )
by David
03:08
created

Jsonld_User_Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Jsonld;
4
5
use Wordlift\Object_Type_Enum;
6
7
class Jsonld_User_Service {
8
9
	/**
10
	 * @var \Wordlift_User_Service $user_service
11
	 */
12
	private $user_service;
13
14
	/**
15
	 * @var Jsonld_Service
16
	 */
17
	private $jsonld_service;
18
19
	/**
20
	 * Jsonld_User_Service constructor.
21
	 *
22
	 * @param \Wordlift_User_Service $user_service
23
	 */
24
	function __construct( $user_service ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
		$this->user_service = $user_service;
26
	}
27
28
	/**
29
	 * @param Jsonld_Service $jsonld_service
30
	 */
31
	function set_jsonld_service( $jsonld_service ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
32
		$this->jsonld_service = $jsonld_service;
33
	}
34
35
	function get( $user_id ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
		$userdata = get_userdata( $user_id );
37
38
		// Bail out if user not found.
39
		if ( ! ( $userdata instanceof \WP_User ) ) {
0 ignored issues
show
Bug introduced by
The class WP_User does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
40
			return array();
41
		}
42
43
		// Return the post JSON-LD if a post has been bound to this user.
44
		$post_id = $this->user_service->get_entity( $user_id );
45
		if ( ! empty( $post_id ) && isset( $this->jsonld_service ) ) {
46
			return $this->jsonld_service->get( Object_Type_Enum::POST, $post_id );
47
		}
48
49
		// Bail out if the user doesn't have a valid URI.
50
		$uri = $this->user_service->get_uri( $user_id );
51
		if ( empty( $uri ) ) {
52
			return array();
53
		}
54
55
		// Finally return the user's JSON-LD.
56
		$data = array(
57
			'@id'   => $uri,
58
			'@type' => 'Person',
59
		);
60
61
		if ( ! empty( $userdata->display_name ) ) {
62
			$data['name'] = $userdata->display_name;
63
		}
64
65
		if ( ! empty( $userdata->first_name ) ) {
66
			$data['givenName'] = $userdata->first_name;
67
		}
68
69
		if ( ! empty( $userdata->last_name ) ) {
70
			$data['familyName'] = $userdata->last_name;
71
		}
72
73
		if ( ! empty( $userdata->user_url ) ) {
74
			$data['url'] = $userdata->user_url;
75
		}
76
77
		return array( $data );
78
	}
79
80
}
81