Completed
Push — master ( b02327...423f34 )
by Naveen
01:11
created
src/wordlift/vocabulary/class-analysis-service.php 1 patch
Indentation   +165 added lines, -165 removed lines patch added patch discarded remove patch
@@ -12,170 +12,170 @@
 block discarded – undo
12 12
  */
13 13
 class Analysis_Service {
14 14
 
15
-	/**
16
-	 * @var Default_Api_Service
17
-	 */
18
-	private $api_service;
19
-	/**
20
-	 * @var Cache
21
-	 */
22
-	private $cache_service;
23
-	/**
24
-	 * @var \Wordlift_Log_Service
25
-	 */
26
-	private $log;
27
-
28
-	/**
29
-	 * Tag_Rest_Endpoint constructor.
30
-	 *
31
-	 * @param Default_Api_Service $api_service
32
-	 * @param Cache               $cache_service
33
-	 */
34
-	public function __construct( $api_service, $cache_service ) {
35
-
36
-		$this->api_service = $api_service;
37
-
38
-		$this->cache_service = $cache_service;
39
-
40
-		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
41
-
42
-	}
43
-
44
-	/**
45
-	 * Check if entities are in cache, if not return the results from
46
-	 * cache service.
47
-	 *
48
-	 * @param $tag \WP_Term
49
-	 */
50
-	public function get_entities( $tag ) {
51
-
52
-		$cache_key    = $tag->term_id;
53
-		$cache_result = $this->cache_service->get( $cache_key );
54
-		if ( false !== $cache_result ) {
55
-			return $cache_result;
56
-		}
57
-
58
-		// send the request.
59
-		$tag_name = $tag->name;
60
-
61
-		$entities = $this->get_entities_by_search_query( $tag_name );
62
-
63
-		if ( ! $entities ) {
64
-			return false;
65
-		}
66
-
67
-		$this->cache_service->put( $cache_key, $entities );
68
-
69
-		return $entities;
70
-
71
-	}
72
-
73
-	/**
74
-	 * @param $entity_url string
75
-	 * Formats the entity url from https://foo.com/some/path to
76
-	 * https/foo.com/some/path
77
-	 *
78
-	 * @return bool|string
79
-	 */
80
-	public static function format_entity_url( $entity_url ) {
81
-		$result = wp_parse_url( $entity_url );
82
-		if ( ! $result ) {
83
-			return false;
84
-		}
85
-		if ( ! array_key_exists( 'scheme', $result )
86
-			 || ! array_key_exists( 'host', $result )
87
-			 || ! array_key_exists( 'path', $result ) ) {
88
-			return false;
89
-		}
90
-
91
-		return $result['scheme'] . '/' . $result['host'] . $result['path'];
92
-	}
93
-
94
-	private function get_meta( $entity_url ) {
95
-
96
-		$cache_results = $this->cache_service->get( $entity_url );
97
-
98
-		if ( false !== $cache_results ) {
99
-			return $cache_results;
100
-		}
101
-
102
-		$formatted_url = self::format_entity_url( $entity_url );
103
-
104
-		if ( ! $formatted_url ) {
105
-			return array();
106
-		}
107
-
108
-		$meta_url = 'https://api.wordlift.io/id/' . $formatted_url;
109
-
110
-		$response = wp_remote_get( $meta_url );
111
-
112
-		if ( is_wp_error( $response )
113
-			 || ! isset( $response['response']['code'] )
114
-			 || 2 !== (int) $response['response']['code'] / 100 ) {
115
-			return false;
116
-		}
117
-
118
-		if ( ! is_wp_error( $response ) ) {
119
-			$meta = json_decode( wp_remote_retrieve_body( $response ), true );
120
-			$this->cache_service->put( $entity_url, $meta );
121
-
122
-			return $meta;
123
-		}
124
-
125
-		return array();
126
-
127
-	}
128
-
129
-	private function get_meta_for_entities( $entities ) {
130
-
131
-		$filtered_entities = array();
132
-		foreach ( $entities as $entity ) {
133
-			$entity['meta'] = array();
134
-			$meta           = $this->get_meta( $entity['entityId'] );
135
-			if ( $meta ) {
136
-				$meta                = Default_Entity_List::compact_jsonld( $meta );
137
-				$entity['meta']      = $meta;
138
-				$filtered_entities[] = $entity;
139
-			}
140
-		}
141
-		return $filtered_entities;
142
-
143
-	}
144
-
145
-	/**
146
-	 * @param $tag_name
147
-	 *
148
-	 * @return array
149
-	 */
150
-	public function get_entities_by_search_query( $tag_name ) {
151
-		$response = $this->api_service->request(
152
-			'POST',
153
-			'/analysis/single',
154
-			array( 'Content-Type' => 'application/json' ),
155
-			wp_json_encode(
156
-				array(
157
-					'content'         => $tag_name,
158
-					'contentType'     => 'text/plain',
159
-					'version'         => '1.0.0',
160
-					'contentLanguage' => 'en',
161
-					'scope'           => 'network',
162
-				)
163
-			)
164
-		);
165
-
166
-		if ( ! $response->is_success() ) {
167
-			return false;
168
-		}
169
-
170
-		$response = json_decode( $response->get_body(), true );
171
-
172
-		if ( ! array_key_exists( 'entities', $response ) ) {
173
-			return false;
174
-		}
175
-
176
-		$entities = $this->get_meta_for_entities( $response['entities'] );
177
-
178
-		return $entities;
179
-	}
15
+    /**
16
+     * @var Default_Api_Service
17
+     */
18
+    private $api_service;
19
+    /**
20
+     * @var Cache
21
+     */
22
+    private $cache_service;
23
+    /**
24
+     * @var \Wordlift_Log_Service
25
+     */
26
+    private $log;
27
+
28
+    /**
29
+     * Tag_Rest_Endpoint constructor.
30
+     *
31
+     * @param Default_Api_Service $api_service
32
+     * @param Cache               $cache_service
33
+     */
34
+    public function __construct( $api_service, $cache_service ) {
35
+
36
+        $this->api_service = $api_service;
37
+
38
+        $this->cache_service = $cache_service;
39
+
40
+        $this->log = \Wordlift_Log_Service::get_logger( get_class() );
41
+
42
+    }
43
+
44
+    /**
45
+     * Check if entities are in cache, if not return the results from
46
+     * cache service.
47
+     *
48
+     * @param $tag \WP_Term
49
+     */
50
+    public function get_entities( $tag ) {
51
+
52
+        $cache_key    = $tag->term_id;
53
+        $cache_result = $this->cache_service->get( $cache_key );
54
+        if ( false !== $cache_result ) {
55
+            return $cache_result;
56
+        }
57
+
58
+        // send the request.
59
+        $tag_name = $tag->name;
60
+
61
+        $entities = $this->get_entities_by_search_query( $tag_name );
62
+
63
+        if ( ! $entities ) {
64
+            return false;
65
+        }
66
+
67
+        $this->cache_service->put( $cache_key, $entities );
68
+
69
+        return $entities;
70
+
71
+    }
72
+
73
+    /**
74
+     * @param $entity_url string
75
+     * Formats the entity url from https://foo.com/some/path to
76
+     * https/foo.com/some/path
77
+     *
78
+     * @return bool|string
79
+     */
80
+    public static function format_entity_url( $entity_url ) {
81
+        $result = wp_parse_url( $entity_url );
82
+        if ( ! $result ) {
83
+            return false;
84
+        }
85
+        if ( ! array_key_exists( 'scheme', $result )
86
+             || ! array_key_exists( 'host', $result )
87
+             || ! array_key_exists( 'path', $result ) ) {
88
+            return false;
89
+        }
90
+
91
+        return $result['scheme'] . '/' . $result['host'] . $result['path'];
92
+    }
93
+
94
+    private function get_meta( $entity_url ) {
95
+
96
+        $cache_results = $this->cache_service->get( $entity_url );
97
+
98
+        if ( false !== $cache_results ) {
99
+            return $cache_results;
100
+        }
101
+
102
+        $formatted_url = self::format_entity_url( $entity_url );
103
+
104
+        if ( ! $formatted_url ) {
105
+            return array();
106
+        }
107
+
108
+        $meta_url = 'https://api.wordlift.io/id/' . $formatted_url;
109
+
110
+        $response = wp_remote_get( $meta_url );
111
+
112
+        if ( is_wp_error( $response )
113
+             || ! isset( $response['response']['code'] )
114
+             || 2 !== (int) $response['response']['code'] / 100 ) {
115
+            return false;
116
+        }
117
+
118
+        if ( ! is_wp_error( $response ) ) {
119
+            $meta = json_decode( wp_remote_retrieve_body( $response ), true );
120
+            $this->cache_service->put( $entity_url, $meta );
121
+
122
+            return $meta;
123
+        }
124
+
125
+        return array();
126
+
127
+    }
128
+
129
+    private function get_meta_for_entities( $entities ) {
130
+
131
+        $filtered_entities = array();
132
+        foreach ( $entities as $entity ) {
133
+            $entity['meta'] = array();
134
+            $meta           = $this->get_meta( $entity['entityId'] );
135
+            if ( $meta ) {
136
+                $meta                = Default_Entity_List::compact_jsonld( $meta );
137
+                $entity['meta']      = $meta;
138
+                $filtered_entities[] = $entity;
139
+            }
140
+        }
141
+        return $filtered_entities;
142
+
143
+    }
144
+
145
+    /**
146
+     * @param $tag_name
147
+     *
148
+     * @return array
149
+     */
150
+    public function get_entities_by_search_query( $tag_name ) {
151
+        $response = $this->api_service->request(
152
+            'POST',
153
+            '/analysis/single',
154
+            array( 'Content-Type' => 'application/json' ),
155
+            wp_json_encode(
156
+                array(
157
+                    'content'         => $tag_name,
158
+                    'contentType'     => 'text/plain',
159
+                    'version'         => '1.0.0',
160
+                    'contentLanguage' => 'en',
161
+                    'scope'           => 'network',
162
+                )
163
+            )
164
+        );
165
+
166
+        if ( ! $response->is_success() ) {
167
+            return false;
168
+        }
169
+
170
+        $response = json_decode( $response->get_body(), true );
171
+
172
+        if ( ! array_key_exists( 'entities', $response ) ) {
173
+            return false;
174
+        }
175
+
176
+        $entities = $this->get_meta_for_entities( $response['entities'] );
177
+
178
+        return $entities;
179
+    }
180 180
 
181 181
 }
Please login to merge, or discard this patch.