Completed
Push — develop ( fc3971...71605d )
by David
03:09 queued 11s
created

Entity_Helper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A map_many_to_local() 0 24 3
1
<?php
2
/**
3
 * Provides helper functions for entity. In particular a function to map external URIs to local URIs.
4
 *
5
 * @author David Riccitelli <[email protected]>
6
 * @since 3.25.1
7
 * @package Wordlift\Entity
8
 */
9
10
namespace Wordlift\Entity;
11
12
class Entity_Helper {
13
14
	private $entity_uri_service;
15
16
	private $entity_service;
17
18
	/**
19
	 * Entity_Helper constructor.
20
	 *
21
	 * @param \Wordlift_Entity_Uri_Service $entity_uri_service
22
	 * @param \Wordlift_Entity_Service $entity_service
23
	 */
24
	public function __construct( $entity_uri_service, $entity_service ) {
25
26
		$this->entity_uri_service = $entity_uri_service;
27
		$this->entity_service     = $entity_service;
28
29
	}
30
31
	/**
32
	 * Maps the provided URIs to local URIs.
33
	 *
34
	 * The input array is filtered out of the local URIs. Then each URI is checked with itemid and sameAs in the local
35
	 * database. If found a mapping is added from the external URI to the local URI.
36
	 *
37
	 * An array of mappings is returned, where the key is the external URI and the value is the local URI.
38
	 *
39
	 * @param array $uris An array of URIs.
40
	 *
41
	 * @return array The mappings array.
42
	 */
43
	public function map_many_to_local( $uris ) {
44
45
		// Filter only the external URIs.
46
		$entity_uri_service = $this->entity_uri_service;
47
		$external_uris      = array_filter( $uris, function ( $item ) use ( $entity_uri_service ) {
48
			return ! $entity_uri_service->is_internal( $item );
49
		} );
50
51
		// Preload the URIs.
52
		$entity_uri_service->preload_uris( $external_uris );
53
54
		$mappings = array();
55
		foreach ( $external_uris as $external_uri ) {
56
			$entity = $entity_uri_service->get_entity( $external_uri );
57
			if ( null !== $entity ) {
58
59
				// Get the internal URI.
60
				$internal_uri              = $this->entity_service->get_uri( $entity->ID );
61
				$mappings[ $external_uri ] = $internal_uri;
62
			}
63
		}
64
65
		return $mappings;
66
	}
67
68
}
69