Completed
Push — master ( b6760c...d0c54a )
by J.D.
03:04
created

WordPoints_Entity_Relationship_Dynamic   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 40
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 3
A get_title() 0 12 2
1
<?php
2
3
/**
4
 * Dynamic entity relationship class.
5
 *
6
 * @package wordpoints-hooks-api
7
 * @since 1.0.0
8
 */
9
10
/**
11
 * Represents the relationship between dynamic entities.
12
 *
13
 * @since 1.0.0
14
 */
15
abstract class WordPoints_Entity_Relationship_Dynamic extends WordPoints_Entity_Relationship {
16
17
	/**
18
	 * @since 1.0.0
19
	 */
20
	public function __construct( $slug ) {
21
22
		parent::__construct( $slug );
23
24
		$parts = explode( '\\', $this->slug, 2 );
25
26
		if ( isset( $parts[1] ) ) {
27
28
			$parsed = $this->parse_slug( $this->related_entity_slug );
29
30
			$this->primary_entity_slug = "{$this->primary_entity_slug}\\{$parts[1]}";
31
			$this->related_entity_slug = "{$parsed['slug']}\\{$parts[1]}";
32
33
			if ( $parsed['is_array'] ) {
34
				$this->related_entity_slug .= '{}';
35
			}
36
		}
37
	}
38
39
	/**
40
	 * @since 1.0.0
41
	 */
42
	public function get_title() {
43
44
		$parsed = $this->parse_slug( $this->related_entity_slug );
45
46
		$entity = wordpoints_entities()->get( $parsed['slug'] );
47
48
		if ( $entity instanceof WordPoints_Entity ) {
49
			return $entity->get_title();
50
		} else {
51
			return $this->related_entity_slug;
52
		}
53
	}
54
}
55
56
// EOF
57