Passed
Push — hotfix/fix-counts ( 4b43d1...cc9e05 )
by Paul
03:52
created

CreateReview::getReferer()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 8
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 12
ccs 0
cts 9
cp 0
crap 12
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Handlers;
4
5
use GeminiLabs\SiteReviews\Commands\CreateReview as Command;
6
use GeminiLabs\SiteReviews\Database\ReviewManager;
7
use GeminiLabs\SiteReviews\Modules\Notification;
8
use GeminiLabs\SiteReviews\Modules\Session;
9
10
class CreateReview
11
{
12
    /**
13
     * @return void|string
14
     */
15 1
    public function handle(Command $command)
16
    {
17 1
        $review = glsr(ReviewManager::class)->create($command);
18 1
        if (!$review) {
19
            glsr(Session::class)->set($command->form_id.'errors', []);
20
            glsr(Session::class)->set($command->form_id.'message', __('Your review could not be submitted and the error has been logged. Please notify the site admin.', 'site-reviews'));
21
            return;
22
        }
23 1
        glsr(Session::class)->set($command->form_id.'message', __('Your review has been submitted!', 'site-reviews'));
24 1
        glsr(Notification::class)->send($review);
25 1
        if ($command->ajax_request) {
26 1
            return;
27
        }
28
        wp_safe_redirect($this->getReferer($command));
29
        exit;
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    protected function getReferer(Command $command)
36
    {
37
        $referer = trim(strval(get_post_meta($command->post_id, 'redirect_to', true)));
38
        $referer = apply_filters('site-reviews/review/redirect', $referer, $command);
39
        if (empty($referer)) {
40
            $referer = $command->referer;
41
        }
42
        if (empty($referer)) {
43
            glsr_log()->warning('The form referer ($_SERVER[REQUEST_URI]) was empty.')->debug($command);
44
            $referer = home_url();
45
        }
46
        return $referer;
47
    }
48
}
49