Completed
Push — develop ( ce465f...dc3813 )
by David
02:29 queued 11s
created

Linked_Data_Autocomplete_Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file provides the Linked Data autocomplete service.
4
 *
5
 * @author David Riccitelli <[email protected]>
6
 * @since 3.24.2
7
 * @package Wordlift\Autocomplete
8
 */
9
10
namespace Wordlift\Autocomplete;
11
12
use Wordlift_Log_Service;
13
14
class Linked_Data_Autocomplete_Service implements Autocomplete_Service {
15
16
	/**
17
	 * The {@link Wordlift_Configuration_Service} instance.
18
	 *
19
	 * @since  3.15.0
20
	 * @access private
21
	 * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
22
	 */
23
	private $configuration_service;
24
25
	/**
26
	 * A {@link Wordlift_Log_Service} instance.
27
	 *
28
	 * @since  3.15.0
29
	 * @access private
30
	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
31
	 */
32
	private $log;
33
34
	/**
35
	 * The {@link Class_Wordlift_Autocomplete_Service} instance.
36
	 *
37
	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
38
	 *
39
	 * @since 3.15.0
40
	 *
41
	 */
42
	public function __construct( $configuration_service ) {
43
		$this->configuration_service = $configuration_service;
44
		$this->log                   = Wordlift_Log_Service::get_logger( 'Wordlift_Autocomplete_Service' );
45
	}
46
47
	/**
48
	 * Make request to external API and return the response.
49
	 *
50
	 * @param string $query The search string.
51
	 * @param string $scope The search scope: "local" will search only in the local dataset; "cloud" will search also
52
	 *                      in Wikipedia. By default is "cloud".
53
	 * @param array|string $exclude The exclude parameter string.
54
	 *
55
	 * @return array $response The API response.
56
	 * @since 3.15.0
57
	 *
58
	 */
59
	public function query( $query, $scope = 'cloud', $exclude = '' ) {
60
		$url = $this->build_request_url( $query, $exclude, $scope );
61
62
		// Return the response.
63
		$response = wp_remote_get( $url, array(
64
			'timeout' => 30
65
		) );
66
67
		// If the response is valid, then send the suggestions.
68
		if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] ) {
69
			// Echo the response.
70
			return json_decode( wp_remote_retrieve_body( $response ), true );
71
		} else {
72
			// Default error message.
73
			$error_message = 'Something went wrong.';
74
75
			// Get the real error message if there is WP_Error.
76
			if ( is_wp_error( $response ) ) {
77
				$error_message = $response->get_error_message();
78
			}
79
80
			$this->log->error( $error_message );
81
82
			return array();
83
		}
84
	}
85
86
	/**
87
	 * Build the autocomplete url.
88
	 *
89
	 * @param string $query The search string.
90
	 * @param array|string $exclude The exclude parameter.
91
	 * @param string $scope The search scope: "local" will search only in the local dataset; "cloud" will search also
92
	 *                      in Wikipedia. By default is "cloud".
93
	 *
94
	 * @return string Built url.
95
	 * @since 3.15.0
96
	 *
97
	 */
98
	private function build_request_url( $query, $exclude, $scope ) {
99
		$args = array(
100
			'key'      => $this->configuration_service->get_key(),
101
			'language' => $this->configuration_service->get_language_code(),
102
			'query'    => $query,
103
			'scope'    => $scope,
104
			'limit'    => 10,
105
		);
106
107
		// Add args to URL.
108
		$request_url = add_query_arg(
109
			urlencode_deep( $args ),
110
			$this->configuration_service->get_autocomplete_url()
111
		);
112
113
		// Add the exclude parameter.
114
		if ( ! empty( $exclude ) ) {
115
			foreach ( (array) $exclude as $item ) {
116
				$request_url .= "&exclude=" . urlencode( $item );
117
			}
118
		}
119
120
		// return the built url.
121
		return $request_url;
122
	}
123
124
}