Passed
Push — master ( e8093c...ec23d4 )
by Paul
04:14
created

Review::isModified()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
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 1
	public function __construct( WP_Post $post )
33
	{
34 1
		$this->content = $post->post_content;
35 1
		$this->date = $post->post_date;
36 1
		$this->ID = intval( $post->ID );
37 1
		$this->status = $post->post_status;
38 1
		$this->title = $post->post_title;
39 1
		$this->user_id = intval( $post->post_author );
40 1
		$this->setProperties( $post );
41 1
		$this->setTermIds( $post );
42 1
	}
43
44
	/**
45
	 * @return mixed
46
	 */
47
	public function __get( $key )
48
	{
49
		if( property_exists( $this, $key )) {
50
			return $this->$key;
51
		}
52
		if( is_array( $this->custom ) && array_key_exists( key, $this->custom )) {
0 ignored issues
show
Bug introduced by
The constant GeminiLabs\SiteReviews\key was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
53
			return $this->custom[$key];
54
		}
55
		return '';
56
	}
57
58
	/**
59
	 * @return bool
60
	 */
61 1
	protected function isModified( array $properties )
62
	{
63 1
		return $this->date != $properties['date']
64 1
			|| $this->content != $properties['content']
65 1
			|| $this->title != $properties['title'];
66
	}
67
68
	/**
69
	 * @return void
70
	 */
71 1
	protected function setProperties( WP_Post $post )
72
	{
73
		$defaults = [
74 1
			'author' => __( 'Anonymous', 'site-reviews' ),
75 1
			'date' => '',
76 1
			'review_id' => '',
77 1
			'review_type' => '',
78
		];
79 1
		$meta = array_filter(
80 1
			array_map( 'array_shift', (array)get_post_meta( $post->ID )),
81 1
			'strlen'
82
		);
83 1
		$properties = glsr( CreateReviewDefaults::class )->restrict( array_merge( $defaults, $meta ));
84 1
		$this->modified = $this->isModified( $properties );
85 1
		array_walk( $properties, function( $value, $key ) {
86 1
			if( !property_exists( $this, $key ) || isset( $this->$key ))return;
87 1
			$this->$key = maybe_unserialize( $value );
88 1
		});
89 1
	}
90
91
	/**
92
	 * @return void
93
	 */
94 1
	protected function setTermIds( WP_Post $post )
95
	{
96 1
		$this->term_ids = [];
97 1
		if( !is_array( $terms = get_the_terms( $post, Application::TAXONOMY )))return;
98
		foreach( $terms as $term ) {
99
			$this->term_ids[] = $term->term_id;
100
		}
101
	}
102
}
103