Test Failed
Push — hotfix/fix-counts ( 1fe4ce...872cd6 )
by Paul
03:14
created

CreateReview   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 90.74%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
eloc 72
c 5
b 1
f 2
dl 0
loc 128
ccs 49
cts 54
cp 0.9074
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 14 4
A getNumeric() 0 6 2
A get() 0 3 1
A getDate() 0 7 2
A __construct() 0 22 1
A getCustom() 0 13 2
A getAvatar() 0 6 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Commands;
4
5
use GeminiLabs\SiteReviews\Helper;
6
7
class CreateReview
8
{
9
	public $ajax_request;
10
	public $assigned_to;
11
	public $author;
12
	public $avatar;
13
	public $blacklisted;
14
	public $category;
15
	public $content;
16
	public $custom;
17
	public $date;
18
	public $email;
19
	public $form_id;
20
	public $ip_address;
21
	public $post_id;
22
	public $rating;
23
	public $referer;
24
	public $request;
25
	public $response;
26
	public $terms;
27
	public $title;
28
	public $url;
29
30 1
	public function __construct( $input )
31
	{
32 1
		$this->request = $input;
33 1
		$this->ajax_request = isset( $input['_ajax_request'] );
34 1
		$this->assigned_to = $this->getNumeric( 'assign_to' );
35 1
		$this->author = sanitize_text_field( $this->getUser( 'name' ));
36 1
		$this->avatar = $this->getAvatar();
37 1
		$this->blacklisted = isset( $input['blacklisted'] );
38 1
		$this->category = sanitize_key( $this->get( 'category' ));
39 1
		$this->content = sanitize_textarea_field( $this->get( 'content' ));
40 1
		$this->custom = $this->getCustom();
41 1
		$this->date = $this->getDate( 'date' );
42 1
		$this->email = sanitize_email( $this->getUser( 'email' ));
43 1
		$this->form_id = sanitize_key( $this->get( 'form_id' ));
44 1
		$this->ip_address = $this->get( 'ip_address' );
45 1
		$this->post_id = intval( $this->get( '_post_id' ));
46 1
		$this->rating = intval( $this->get( 'rating' ));
47 1
		$this->referer = $this->get( '_referer' );
48 1
		$this->response = sanitize_textarea_field( $this->get( 'response' ));
49 1
		$this->terms = !empty( $input['terms'] );
50 1
		$this->title = sanitize_text_field( $this->get( 'title' ));
51 1
		$this->url = esc_url_raw( $this->get( 'url' ));
52 1
	}
53
54
	/**
55
	 * @param string $key
56
	 * @return string
57
	 */
58 1
	protected function get( $key )
59
	{
60 1
		return (string)glsr_get( $this->request, $key );
61
	}
62
63
	/**
64
	 * @return string
65
	 */
66 1
	protected function getAvatar()
67
	{
68 1
		$avatar = $this->get( 'avatar' );
69 1
		return !filter_var( $avatar, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED )
70 1
			? (string)get_avatar_url( $this->get( 'email' ))
71 1
			: $avatar;
72
	}
73
74
	/**
75
	 * @return array
76
	 */
77 1
	protected function getCustom()
78
	{
79
		$unset = [
80 1
			'_action', '_ajax_request', '_counter', '_nonce', '_post_id', '_recaptcha-token',
81
			'_referer', 'assign_to', 'category', 'content', 'date', 'email', 'excluded', 'form_id',
82
			'gotcha', 'ip_address', 'name', 'rating', 'response', 'terms', 'title', 'url',
83
		];
84 1
		$unset = apply_filters( 'site-reviews/create/unset-keys-from-custom', $unset );
85 1
		$custom = $this->request;
86 1
		foreach( $unset as $value ) {
87 1
			unset( $custom[$value] );
88
		}
89 1
		return $custom;
90
	}
91
92
	/**
93
	 * @param string $key
94
	 * @return string
95
	 */
96 1
	protected function getDate( $key )
97
	{
98 1
		$date = strtotime( $this->get( $key ));
99 1
		if( $date === false ) {
100 1
			$date = time();
101
		}
102 1
		return get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $date ));
103
	}
104
105
	/**
106
	 * @param string $key
107
	 * @return string
108
	 */
109 1
	protected function getUser( $key )
110
	{
111 1
		$value = $this->get( $key );
112 1
		if( empty( $value )) {
113
			$user = wp_get_current_user();
114
			$userValues = [
115
				'email' => 'user_email',
116
				'name' => 'display_name',
117
			];
118
			if( $user->exists() && array_key_exists( $key, $userValues )) {
119
				return $user->{$userValues[$key]};
120
			}
121
		}
122 1
		return $value;
123
	}
124
125
	/**
126
	 * @param string $key
127
	 * @return string
128
	 */
129 1
	protected function getNumeric( $key )
130
	{
131 1
		$value = $this->get( $key );
132 1
		return is_numeric( $value )
133
			? $value
134 1
			: '';
135
	}
136
}
137