Test Failed
Push — master ( e52611...5d5118 )
by Paul
04:30
created

Review::setTermIds()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 12
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 $date;
16
	public $email;
17
	public $ID;
18
	public $ip_address;
19
	public $modified;
20
	public $pinned;
21
	public $rating;
22
	public $response;
23
	public $review_id;
24
	public $review_type;
25
	public $status;
26
	public $term_ids;
27
	public $title;
28
	public $url;
29
	public $user_id;
30
31
	public function __construct( WP_Post $post )
32
	{
33
		$this->content = $post->post_content;
34
		$this->date = $post->post_date;
35
		$this->ID = intval( $post->ID );
36
		$this->status = $post->post_status;
37
		$this->title = $post->post_title;
38
		$this->user_id = intval( $post->post_author );
39
		$this->setProperties( $post );
40
		$this->setTermIds( $post );
41
	}
42
43
	/**
44
	 * @return bool
45
	 */
46
	protected function isModified( array $properties )
47
	{
48
		return $this->date != $properties['date']
49
			|| $this->content != $properties['content']
50
			|| $this->title != $properties['title'];
51
	}
52
53
	/**
54
	 * @return void
55
	 */
56
	protected function setProperties( WP_Post $post )
57
	{
58
		$defaults = [
59
			'author' => __( 'Anonymous', 'site-reviews' ),
60
			'date' => '',
61
			'review_id' => '',
62
			'review_type' => '',
63
		];
64
		$meta = array_filter(
65
			array_map( 'array_shift', (array)get_post_meta( $post->ID )),
66
			'strlen'
67
		);
68
		$properties = glsr( CreateReviewDefaults::class )->restrict( array_merge( $defaults, $meta ));
69
		$this->modified = $this->isModified( $properties );
70
		array_walk( $properties, function( $value, $key ) {
71
			if( !property_exists( $this, $key ) || isset( $this->$key ))return;
72
			$this->$key = $value;
73
		});
74
	}
75
76
	/**
77
	 * @return void
78
	 */
79
	protected function setTermIds( WP_Post $post )
80
	{
81
		$this->term_ids = [];
82
		if( !is_array( $terms = get_the_terms( $post, Application::TAXONOMY )))return;
83
		foreach( $terms as $term ) {
84
			$this->term_ids[] = $term->term_id;
85
		}
86
	}
87
}
88