Passed
Push — feature/rebusify ( 816514...289185 )
by Paul
07:17
created

Rebusify::getBodyForReview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 12
ccs 0
cts 12
cp 0
crap 2
rs 9.9666
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Review;
6
7
class Rebusify
8
{
9
    const API_URL = 'https://www.rebusify.com/api/rbs/';
10
11
    /**
12
     * @return void|array
13
     */
14
    public function sendReview(Review $review)
15
    {
16
        return $this->send('index.php', [
17
            'body' => $this->getBodyForReview($review),
18
            'timeout' => 120,
19
        ]);
20
    }
21
22
    /**
23
     * @return void|array
24
     */
25
    public function sendReviewResponse(Review $review)
26
    {
27
        return $this->send('fetch_customer_reply.php', [
28
            'body' => $this->getBodyForResponse($review),
29
        ]);
30
    }
31
32
    /**
33
     * @return array
34
     */
35
    protected function getBodyForResponse(Review $review)
36
    {
37
        $rebusifyResponse = [
38
            'reply' => $review->response, // what is the 300 character limit for?
39
            'review_id' => '', // @todo
40
            'review_transaction_id' => '', // @todo
41
            'type' => 'M',
42
        ];
43
        return apply_filters('site-reviews/rebusify/response', $rebusifyResponse, $review);
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    protected function getBodyForReview(Review $review)
50
    {
51
        $rebusifyReview = [
52
            'domain' => get_site_url(),
53
            'firstname' => $review->name, // what is the 25 character limit for?
1 ignored issue
show
Bug Best Practice introduced by
The property name does not exist on GeminiLabs\SiteReviews\Review. Since you implemented __get, consider adding a @property annotation.
Loading history...
54
            'rate' => $review->rating,
55
            'review_transaction_id' => '', // @todo
56
            'reviews' => $review->content, // what is the 280 character limit for?
57
            'title' => $review->title, // what is the 35 character limit for?
58
            'transaction' => '', // @todo
59
        ];
60
        return apply_filters('site-reviews/rebusify/review', $rebusifyReview, $review);
61
    }
62
63
    /**
64
     * @return void|array
65
     */
66
    protected function send($endpoint, $args)
67
    {
68
        $args = wp_parse_args($args, [
69
            'blocking' => false,
70
            'body' => [],
71
            'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
72
            'redirection' => 5,
73
            'sslverify' => false,
74
            'timeout' => 5,
75
        ]);
76
        $response = wp_remote_post(trailingslashit(static::API_URL).$endpoint, $args);
77
        if (is_wp_error($response)) {
78
            glsr_log()->error('REBUSIFY: '.$response->get_error_message());
79
            return;
80
        }
81
        if (200 === wp_remote_retrieve_response_code($response)) {
82
            $responsedata = wp_remote_retrieve_body($response);
83
            return json_decode($responsedata, true);
84
        }
85
    }
86
}
87