Passed
Push — master ( 8a0962...507d84 )
by Paul
03:52
created

Review   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 76
ccs 32
cts 34
cp 0.9412
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 $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 1
	public function __construct( WP_Post $post )
32
	{
33 1
		$this->content = $post->post_content;
34 1
		$this->date = $post->post_date;
35 1
		$this->ID = intval( $post->ID );
36 1
		$this->status = $post->post_status;
37 1
		$this->title = $post->post_title;
38 1
		$this->user_id = intval( $post->post_author );
39 1
		$this->setProperties( $post );
40 1
		$this->setTermIds( $post );
41 1
	}
42
43
	/**
44
	 * @return bool
45
	 */
46 1
	protected function isModified( array $properties )
47
	{
48 1
		return $this->date != $properties['date']
49 1
			|| $this->content != $properties['content']
50 1
			|| $this->title != $properties['title'];
51
	}
52
53
	/**
54
	 * @return void
55
	 */
56 1
	protected function setProperties( WP_Post $post )
57
	{
58
		$defaults = [
59 1
			'author' => __( 'Anonymous', 'site-reviews' ),
60 1
			'date' => '',
61 1
			'review_id' => '',
62 1
			'review_type' => '',
63
		];
64 1
		$meta = array_filter(
65 1
			array_map( 'array_shift', (array)get_post_meta( $post->ID )),
66 1
			'strlen'
67
		);
68 1
		$properties = glsr( CreateReviewDefaults::class )->restrict( array_merge( $defaults, $meta ));
69 1
		$this->modified = $this->isModified( $properties );
70 1
		array_walk( $properties, function( $value, $key ) {
71 1
			if( !property_exists( $this, $key ) || isset( $this->$key ))return;
72 1
			$this->$key = $value;
73 1
		});
74 1
	}
75
76
	/**
77
	 * @return void
78
	 */
79 1
	protected function setTermIds( WP_Post $post )
80
	{
81 1
		$this->term_ids = [];
82 1
		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