Completed
Push — develop ( e7ebe2...e67c0e )
by Naveen
29s queued 12s
created

Analysis_Service::format_entity_url()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 1
dl 0
loc 13
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Vocabulary;
4
5
use Wordlift\Api\Default_Api_Service;
6
use Wordlift\Vocabulary\Cache\Cache;
7
use Wordlift\Vocabulary\Data\Entity_List\Default_Entity_List;
8
9
10
/**
11
 * @since 1.0.0
12
 * @author Naveen Muthusamy <[email protected]>
13
 */
14
class Analysis_Service {
15
16
	/**
17
	 * @var Default_Api_Service
18
	 */
19
	private $api_service;
20
	/**
21
	 * @var Cache
22
	 */
23
	private $cache_service;
24
	/**
25
	 * @var \Wordlift_Log_Service
26
	 */
27
	private $log;
28
29
30
	/**
31
	 * Tag_Rest_Endpoint constructor.
32
	 *
33
	 * @param Default_Api_Service $api_service
34
	 * @param Cache $cache_service
35
	 */
36
	public function __construct( $api_service, $cache_service ) {
37
38
		$this->api_service = $api_service;
39
40
		$this->cache_service = $cache_service;
41
42
		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
43
44
	}
45
46
47
	/**
48
	 * Check if entities are in cache, if not return the results from
49
	 * cache service.
50
	 *
51
	 * @param $tag \WP_Term
52
	 */
53
	public function get_entities( $tag ) {
54
55
		$cache_key    = $tag->term_id;
56
		$cache_result = $this->cache_service->get( $cache_key );
57
		if ( $cache_result !== false ) {
58
			return $cache_result;
59
		}
60
61
		// send the request.
62
		$response = $this->api_service->request(
63
			'POST',
64
			"/analysis/single",
65
			array( 'Content-Type' => 'application/json' ),
66
			wp_json_encode( array(
67
				"content"         => $tag->name,
68
				"contentType"     => "text/plain",
69
				"version"         => "1.0.0",
70
				"contentLanguage" => "en",
71
				"scope"           => "all",
72
			) )
73
		);
74
75
76
		if ( ! $response->is_success() ) {
77
			return false;
78
		}
79
80
		$response = json_decode( $response->get_body(), true );
81
82
		if ( ! array_key_exists( 'entities', $response ) ) {
83
			return false;
84
		}
85
86
87
		$entities = $this->get_meta_for_entities( $response['entities'] );
88
89
		$this->cache_service->put( $cache_key, $entities );
90
91
		return $entities;
92
93
	}
94
95
96
	/**
97
	 * @param $entity_url string
98
	 * Formats the entity url from https://foo.com/some/path to
99
	 * https/foo.com/some/path
100
	 *
101
	 * @return bool|string
102
	 */
103
	public static function format_entity_url( $entity_url ) {
104
		$result = parse_url( $entity_url );
105
		if ( ! $result ) {
106
			return false;
107
		}
108
		if ( ! array_key_exists( 'scheme', $result )
109
		     || ! array_key_exists( 'host', $result )
110
		     || ! array_key_exists( 'path', $result ) ) {
111
			return false;
112
		}
113
114
		return $result['scheme'] . "/" . $result['host'] . $result['path'];
115
	}
116
117
	private function get_meta( $entity_url ) {
118
119
120
		$cache_results = $this->cache_service->get( $entity_url );
121
122
		if ( $cache_results !== false ) {
123
			return $cache_results;
124
		}
125
126
		$formatted_url = self::format_entity_url( $entity_url );
127
128
		if ( ! $formatted_url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $formatted_url of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
129
			return array();
130
		}
131
132
		$meta_url = 'https://api.wordlift.io/id/' . $formatted_url;
133
134
		$response = wp_remote_get( $meta_url );
135
136
		$this->log->debug( "Requesting entity data for url :" . $meta_url );
137
		$this->log->debug( "Got entity meta data as : " );
138
		$this->log->debug( var_export( $response, true ) );
139
		if ( ! is_wp_error( $response ) ) {
140
			$meta = json_decode( wp_remote_retrieve_body( $response ), true );
141
			$this->log->debug( "Saved entity data to meta :" );
142
			$this->log->debug( var_export( $meta, true ) );
143
			$this->cache_service->put( $entity_url, $meta );
144
145
			return $meta;
146
		}
147
148
149
		return array();
150
151
	}
152
153
	private function get_meta_for_entities( $entities ) {
154
155
		$filtered_entities = array();
156
		foreach ( $entities as $entity ) {
157
			$entity['meta'] = array();
158
			$meta = $this->get_meta( $entity['entityId'] );
159
			$meta = Default_Entity_List::compact_jsonld($meta);
160
			if ( $meta ) {
161
				$entity['meta'] = $meta;
162
			}
163
			$filtered_entities[] = $entity;
164
		}
165
		$this->log->debug("Returning filtered entities as");
166
		$this->log->debug(var_export($filtered_entities, true));
167
168
		return $filtered_entities;
169
170
	}
171
172
173
}
174