Passed
Push — master ( d2bd69...8cf071 )
by Paul
04:18
created

CreateReview   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 88.33%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
eloc 77
c 5
b 1
f 2
dl 0
loc 142
ccs 53
cts 60
cp 0.8833
rs 10
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A __construct() 0 22 1
A getAvatar() 0 6 2
A getCategory() 0 4 1
A getUser() 0 14 4
A getDate() 0 7 2
A getNumeric() 0 6 2
A getCustom() 0 18 4
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Commands;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
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 = $this->getCategory();
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 = sanitize_text_field($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(sanitize_text_field($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) Arr::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 string
76
     */
77 1
    protected function getCategory()
78
    {
79 1
        $categories = Arr::convertStringToArray($this->get('category'));
80 1
        return sanitize_key(Arr::get($categories, 0));
81
    }
82
83
    /**
84
     * @return array
85
     */
86 1
    protected function getCustom()
87
    {
88
        $unset = [
89 1
            '_action', '_ajax_request', '_counter', '_nonce', '_post_id', '_recaptcha-token',
90
            '_referer', 'assign_to', 'category', 'content', 'date', 'email', 'excluded', 'form_id',
91
            'gotcha', 'ip_address', 'name', 'rating', 'response', 'terms', 'title', 'url',
92
        ];
93 1
        $unset = apply_filters('site-reviews/create/unset-keys-from-custom', $unset);
94 1
        $custom = $this->request;
95 1
        foreach ($unset as $key) {
96 1
            unset($custom[$key]);
97
        }
98 1
        foreach ($custom as $key => $value) {
99
            if (is_string($value)) {
100
                $custom[$key] = sanitize_text_field($value);
101
            }
102
        }
103 1
        return $custom;
104
    }
105
106
    /**
107
     * @param string $key
108
     * @return string
109
     */
110 1
    protected function getDate($key)
111
    {
112 1
        $date = strtotime($this->get($key));
113 1
        if (false === $date) {
114 1
            $date = time();
115
        }
116 1
        return get_date_from_gmt(gmdate('Y-m-d H:i:s', $date));
117
    }
118
119
    /**
120
     * @param string $key
121
     * @return string
122
     */
123 1
    protected function getUser($key)
124
    {
125 1
        $value = $this->get($key);
126 1
        if (empty($value)) {
127
            $user = wp_get_current_user();
128
            $userValues = [
129
                'email' => 'user_email',
130
                'name' => 'display_name',
131
            ];
132
            if ($user->exists() && array_key_exists($key, $userValues)) {
133
                return $user->{$userValues[$key]};
134
            }
135
        }
136 1
        return $value;
137
    }
138
139
    /**
140
     * @param string $key
141
     * @return string
142
     */
143 1
    protected function getNumeric($key)
144
    {
145 1
        $value = $this->get($key);
146 1
        return is_numeric($value)
147
            ? $value
148 1
            : '';
149
    }
150
}
151