Passed
Push — master ( 688e42...8bf068 )
by Paul
08:13
created

CreateReview::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

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