Test Failed
Push — master ( 2def23...999d23 )
by Paul
04:14
created

Review   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 77
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setProperties() 0 17 3
A __construct() 0 10 1
A setTermIds() 0 6 3
A isModified() 0 5 3
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults;
7
use WP_Post;
8
9
class Review
10
{
11
	public $assigned_to;
12
	public $author;
13
	public $avatar;
14
	public $content;
15
	public $custom;
16
	public $date;
17
	public $email;
18
	public $ID;
19
	public $ip_address;
20
	public $modified;
21
	public $pinned;
22
	public $rating;
23
	public $response;
24
	public $review_id;
25
	public $review_type;
26
	public $status;
27
	public $term_ids;
28
	public $title;
29
	public $url;
30
	public $user_id;
31
32
	public function __construct( WP_Post $post )
33
	{
34
		$this->content = $post->post_content;
35
		$this->date = $post->post_date;
36
		$this->ID = intval( $post->ID );
37
		$this->status = $post->post_status;
38
		$this->title = $post->post_title;
39
		$this->user_id = intval( $post->post_author );
40
		$this->setProperties( $post );
41
		$this->setTermIds( $post );
42
	}
43
44
	/**
45
	 * @return bool
46
	 */
47
	protected function isModified( array $properties )
48
	{
49
		return $this->date != $properties['date']
50
			|| $this->content != $properties['content']
51
			|| $this->title != $properties['title'];
52
	}
53
54
	/**
55
	 * @return void
56
	 */
57
	protected function setProperties( WP_Post $post )
58
	{
59
		$defaults = [
60
			'author' => __( 'Anonymous', 'site-reviews' ),
61
			'date' => '',
62
			'review_id' => '',
63
			'review_type' => '',
64
		];
65
		$meta = array_filter(
66
			array_map( 'array_shift', (array)get_post_meta( $post->ID )),
67
			'strlen'
68
		);
69
		$properties = glsr( CreateReviewDefaults::class )->restrict( array_merge( $defaults, $meta ));
70
		$this->modified = $this->isModified( $properties );
71
		array_walk( $properties, function( $value, $key ) {
72
			if( !property_exists( $this, $key ) || isset( $this->$key ))return;
73
			$this->$key = maybe_unserialize( $value );
74
		});
75
	}
76
77
	/**
78
	 * @return void
79
	 */
80
	protected function setTermIds( WP_Post $post )
81
	{
82
		$this->term_ids = [];
83
		if( !is_array( $terms = get_the_terms( $post, Application::TAXONOMY )))return;
84
		foreach( $terms as $term ) {
85
			$this->term_ids[] = $term->term_id;
86
		}
87
	}
88
}
89