Passed
Push — master ( c471f8...c4eebf )
by Paul
09:34 queued 02:26
created

CreateReview   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Test Coverage

Coverage 87.01%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 27
eloc 91
c 1
b 0
f 0
dl 0
loc 200
ccs 67
cts 77
cp 0.8701
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A type() 0 4 2
A toArray() 0 5 1
A redirect() 0 8 2
A avatar() 0 10 3
A custom() 0 3 1
A referer() 0 7 2
A validate() 0 8 1
A sanitize() 0 14 4
A __construct() 0 8 3
A create() 0 8 2
A success() 0 7 2
A isValid() 0 3 1
A handle() 0 6 2
A response() 0 9 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Commands;
4
5
use GeminiLabs\SiteReviews\Contracts\CommandContract as Contract;
6
use GeminiLabs\SiteReviews\Database\ReviewManager;
7
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults;
8
use GeminiLabs\SiteReviews\Defaults\CustomFieldsDefaults;
9
use GeminiLabs\SiteReviews\Helper;
10
use GeminiLabs\SiteReviews\Helpers\Cast;
11
use GeminiLabs\SiteReviews\Helpers\Url;
12
use GeminiLabs\SiteReviews\Modules\Avatar;
13
use GeminiLabs\SiteReviews\Modules\Validator\DefaultValidator;
14
use GeminiLabs\SiteReviews\Modules\Validator\ValidateReview;
15
use GeminiLabs\SiteReviews\Request;
16
use GeminiLabs\SiteReviews\Review;
17
18
class CreateReview implements Contract
19
{
20
    public $assigned_posts;
21
    public $assigned_terms;
22
    public $assigned_users;
23
    public $avatar;
24
    public $blacklisted;
25
    public $content;
26
    public $custom;
27
    public $date;
28
    public $date_gmt;
29
    public $email;
30
    public $form_id;
31
    public $ip_address;
32
    public $is_approved;
33
    public $is_pinned;
34
    public $name;
35
    public $post_id;
36
    public $rating;
37
    public $referer;
38
    public $request;
39
    public $response;
40
    public $terms;
41
    public $terms_exist;
42
    public $title;
43
    public $type;
44
    public $url;
45
46
    protected $errors;
47
    protected $message;
48
    protected $recaptcha;
49
    protected $review;
50
51 15
    public function __construct(Request $request)
52
    {
53 15
        if (!defined('WP_IMPORTING') || empty($request->ip_address)) {
54 15
            $request->set('ip_address', Helper::getIpAddress()); // required for Akismet and Blacklist validation
55
        }
56 15
        glsr()->action('review/request', $request);
57 15
        $this->request = $request;
58 15
        $this->sanitize();
59 15
    }
60
61
    /**
62
     * @return static
63
     */
64 7
    public function handle()
65
    {
66 7
        if ($this->validate()) {
67 6
            $this->create();
68
        }
69 7
        return $this;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function referer()
76
    {
77
        if ($referer = $this->redirect($this->referer)) {
78
            return $referer;
79
        }
80
        glsr_log()->warning('The form referer ($_SERVER[REQUEST_URI]) was empty.')->debug($this);
81
        return Url::home();
82
    }
83
84
    /**
85
     * @return array
86
     */
87 7
    public function response()
88
    {
89
        return [
90 7
            'errors' => $this->errors,
91 7
            'html' => (string) $this->review,
92 7
            'message' => $this->message,
93 7
            'recaptcha' => $this->recaptcha,
94 7
            'redirect' => $this->redirect(),
95 7
            'review' => Cast::toArray($this->review),
96
        ];
97
    }
98
99
    /**
100
     * @return bool
101
     */
102 7
    public function success()
103
    {
104 7
        if (false === $this->errors) {
105 6
            glsr()->sessionClear();
106 6
            return true;
107
        }
108 7
        return false;
109
    }
110
111
    /**
112
     * @return array
113
     */
114 15
    public function toArray()
115
    {
116 15
        $values = get_object_vars($this);
117 15
        $values = glsr()->filterArray('create/review-values', $values, $this);
118 15
        return glsr(CreateReviewDefaults::class)->merge($values);
119
    }
120
121
    /**
122
     * @return bool
123
     */
124 7
    public function validate()
125
    {
126 7
        $validator = glsr(ValidateReview::class)->validate($this->request);
127 7
        $this->blacklisted = $validator->blacklisted;
128 7
        $this->errors = $validator->errors;
129 7
        $this->message = $validator->message;
130 7
        $this->recaptcha = $validator->recaptcha;
131 7
        return $validator->isValid();
132
    }
133
134
    /**
135
     * This only validates the provided values in the Request.
136
     * @return bool
137
     */
138 1
    public function isValid()
139
    {
140 1
        return glsr(DefaultValidator::class, ['request' => $this->request])->isValidRequest();
141
    }
142
143
    /**
144
     * @return string
145
     */
146 15
    protected function avatar()
147
    {
148 15
        if (!empty($this->avatar)) {
149
            return $this->avatar;
150
        }
151 15
        $review = new Review($this->toArray(), false); // don't init!
152 15
        if (empty($this->email)) {
153 7
            $review->set('author_id', get_current_user_id());
154
        }
155 15
        return glsr(Avatar::class)->generate($review);
156
    }
157
158
    /**
159
     * @return void
160
     */
161 6
    protected function create()
162
    {
163 6
        if ($this->review = glsr(ReviewManager::class)->create($this)) {
164 6
            $this->message = __('Your review has been submitted!', 'site-reviews');
165 6
            return;
166
        }
167
        $this->errors = [];
168
        $this->message = __('Your review could not be submitted and the error has been logged. Please notify the site administrator.', 'site-reviews');
169
    }
170
171
    /**
172
     * @return array
173
     */
174 15
    protected function custom()
175
    {
176 15
        return glsr(CustomFieldsDefaults::class)->filter($this->request->toArray());
177
    }
178
179
    /**
180
     * @return string
181
     */
182 7
    protected function redirect($fallback = '')
183
    {
184 7
        $redirect = trim(strval(get_post_meta($this->post_id, 'redirect_to', true)));
185 7
        $redirect = glsr()->filterString('review/redirect', $redirect, $this);
186 7
        if (empty($redirect)) {
187 7
            $redirect = $fallback;
188
        }
189 7
        return sanitize_text_field($redirect);
190
    }
191
192
    /**
193
     * @return void
194
     */
195 15
    protected function sanitize()
196
    {
197 15
        $values = glsr(CreateReviewDefaults::class)->restrict($this->request->toArray());
198 15
        foreach ($values as $key => $value) {
199 15
            if (property_exists($this, $key)) {
200 15
                $this->{$key} = $value;
201
            }
202
        }
203 15
        if (!empty($this->date)) {
204
            $this->date_gmt = get_gmt_from_date($this->date); // set the GMT date
205
        }
206 15
        $this->custom = $this->custom();
207 15
        $this->type = $this->type();
208 15
        $this->avatar = $this->avatar(); // do this last
209 15
    }
210
211
    /**
212
     * @return string
213
     */
214 15
    protected function type()
215
    {
216 15
        $reviewTypes = glsr()->retrieveAs('array', 'review_types');
217 15
        return array_key_exists($this->type, $reviewTypes) ? $this->type : 'local';
218
    }
219
}
220