Passed
Push — master ( 6f6d9e...e32a3c )
by Paul
04:05
created

Review::isModified()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults;
6
use WP_Post;
7
8
class Review
9
{
10
	public $assigned_to;
11
	public $author;
12
	public $avatar;
13
	public $content;
14
	public $date;
15
	public $email;
16
	public $ID;
17
	public $ip_address;
18
	public $modified;
19
	public $pinned;
20
	public $rating;
21
	public $response;
22
	public $review_id;
23
	public $review_type;
24
	public $status;
25
	public $title;
26
	public $url;
27
	public $user_id;
28
29
	public function __construct( WP_Post $post )
30
	{
31
		$this->content = $post->post_content;
32
		$this->date = $post->post_date;
33
		$this->ID = $post->ID;
34
		$this->status = $post->post_status;
35
		$this->title = $post->post_title;
36
		$this->user_id = $post->post_author;
37
		$this->setProperties( $post );
38
	}
39
40
	/**
41
	 * @return bool
42
	 */
43
	protected function isModified( array $properties )
44
	{
45
		return $this->date != $properties['date']
46
			|| $this->content != $properties['content']
47
			|| $this->title != $properties['title'];
48
	}
49
50
	/**
51
	 * @return void
52
	 */
53
	protected function setProperties( WP_Post $post )
54
	{
55
		$defaults = [
56
			'author' => __( 'Anonymous', 'site-reviews' ),
57
			'date' => '',
58
			'review_id' => '',
59
			'review_type' => '',
60
		];
61
		$meta = array_filter(
62
			array_map( 'array_shift', (array)get_post_meta( $post->ID )),
63
			'strlen'
64
		);
65
		$properties = glsr( CreateReviewDefaults::class )->restrict( array_merge( $defaults, $meta ));
66
		$this->modified = $this->isModified( $properties );
67
		array_walk( $properties, function( $value, $key ) {
68
			if( !property_exists( $this, $key ) || isset( $this->$key ))return;
69
			$this->$key = $value;
70
		});
71
	}
72
}
73