Completed
Push — master ( 4713b5...48986a )
by David
05:28 queued 15s
created

Wordlift_Jsonld_Service::get()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 41
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 0
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Define the Wordlift_Jsonld_Service class to support JSON-LD.
4
 *
5
 * @since   3.8.0
6
 * @package Wordlift
7
 */
8
9
/**
10
 * This class exports an entity using JSON-LD.
11
 *
12
 * @since 3.8.0
13
 */
14
class Wordlift_Jsonld_Service {
15
16
	/**
17
	 * A {@link Wordlift_Entity_Service} instance.
18
	 *
19
	 * @since  3.8.0
20
	 * @access private
21
	 * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
22
	 */
23
	private $entity_service;
24
25
	/**
26
	 * A {@link Wordlift_Post_Converter} instance.
27
	 *
28
	 * @since  3.8.0
29
	 * @access private
30
	 * @var \Wordlift_Post_Converter A {@link Wordlift_Post_Converter} instance.
31
	 */
32
	private $converter;
33
34
	/**
35
	 * Create a JSON-LD service.
36
	 *
37
	 * @since 3.8.0
38
	 *
39
	 * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance.
40
	 * @param \Wordlift_Post_Converter $converter      A {@link Wordlift_Uri_To_Jsonld_Converter} instance.
41
	 */
42
	public function __construct( $entity_service, $converter ) {
43
44
		$this->entity_service = $entity_service;
45
		$this->converter      = $converter;
46
47
	}
48
49
	/**
50
	 * Process calls to the AJAX 'wl_jsonld' endpoint.
51
	 *
52
	 * @since 3.8.0
53
	 */
54
	public function get() {
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
55
56
		// Clear the buffer to be sure someone doesn't mess with our response.
57
		//
58
		// See https://github.com/insideout10/wordlift-plugin/issues/406.
59
		// See https://codex.wordpress.org/AJAX_in_Plugins.
60
		ob_clean();
61
62
		// If no id has been provided return an empty array.
63
		if ( ! isset( $_REQUEST['id'] ) ) {
64
			wp_send_json( array() );
65
		}
66
67
		// Get the id.
68
		$id = $_REQUEST['id'];
69
70
		// An array of references which is captured when converting an URI to a
71
		// json which we gather to further expand our json-ld.
72
		$references = array();
73
74
		// Set a reference to the entity_to_jsonld_converter to use in the closures.
75
		$entity_to_jsonld_converter = $this->converter;
76
77
		// Convert each URI to a JSON-LD array, while gathering referenced entities.
78
		// in the references array.
79
		$jsonld = array_merge(
80
			array( $entity_to_jsonld_converter->convert( $id, $references ) ),
0 ignored issues
show
Unused Code introduced by
The call to Wordlift_Post_Converter::convert() has too many arguments starting with $references.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
81
			// Convert each URI in the references array to JSON-LD. We don't output
82
			// entities already output above (hence the array_diff).
83
			array_map( function ( $item ) use ( $entity_to_jsonld_converter, $references ) {
84
85
				// "2nd level properties" may not output here, e.g. a post
86
				// mentioning an event, located in a place: the place is referenced
87
				// via the `@id` but no other properties are loaded.
88
				return $entity_to_jsonld_converter->convert( $item, $references );
0 ignored issues
show
Unused Code introduced by
The call to Wordlift_Post_Converter::convert() has too many arguments starting with $references.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
89
			}, $references ) );
90
91
		// Finally send the JSON-LD.
92
		wp_send_json( $jsonld );
93
94
	}
95
96
}
97