Passed
Push — main ( e8f4c1...3a01be )
by Paul
11:45 queued 11s
created

Discord::rating()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Contracts\WebhookContract;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Defaults\DiscordDefaults;
8
use GeminiLabs\SiteReviews\Review;
9
10
class Discord implements WebhookContract
11
{
12
    /**
13
     * @var array
14
     */
15
    public $notification;
16
17
    /**
18
     * @var Review
19
     */
20
    public $review;
21
22
    /**
23
     * @var string
24
     */
25
    public $webhook;
26
27
    public function __construct()
28
    {
29
        $this->webhook = glsr_get_option('general.notification_discord');
30
    }
31
32
    /**
33
     * @return WebhookContract
34
     */
35
    public function compose(Review $review, array $args)
36
    {
37
        if (empty($this->webhook)) {
38
            return $this;
39
        }
40
        $this->review = $review;
41
        $args = glsr(DiscordDefaults::class)->restrict($args);
42
        $notification = [
43
            'avatar_url' => esc_url($args['avatar_url']),
44
            'content' => esc_html($args['content']),
45
            'embeds' => [[
46
                'color' => $args['color'],
47
                'description' => $this->description(), // rating and content
48
                'fields' => $this->fields(),
49
                'title' => $this->title(),
50
                'url' =>  esc_url($args['edit_url']),
51
            ]],
52
            'username' => esc_html($args['username']),
53
        ];
54
        $this->notification = glsr()->filterArray('discord/compose', $notification, $this);
55
        return $this;
56
    }
57
58
    /**
59
     * @return \WP_Error|array
60
     */
61
    public function send()
62
    {
63
        if (empty($this->webhook)) {
64
            return new \WP_Error('discord', 'Discord notification was not sent: missing webhook');
65
        }
66
        return wp_remote_post($this->webhook, [
67
            'blocking' => false,
68
            'body' => wp_json_encode($this->notification),
69
            'headers' => [
70
                'Content-Type' => 'application/json',
71
            ],
72
        ]);
73
    }
74
75
    protected function description(): string
76
    {
77
        return $this->rating().PHP_EOL.PHP_EOL.$this->review->content;
78
    }
79
80
    protected function fields(): array
81
    {
82
        $fields = [
83
            'name' => [
84
                'name' => 'Name',
85
                'value' => $this->review->name,
86
                'inline' => true,
87
            ],
88
            'email' => [
89
                'name' => 'Email',
90
                'value' => $this->review->email,
91
                'inline' => true,
92
            ],
93
            'ip_address' => [
94
                'name' => 'IP Address',
95
                'value' => $this->review->ip_address,
96
                'inline' => true,
97
            ],
98
        ];
99
        $fields = glsr()->filterArray('discord/fields', $fields, $this->review);
100
        return array_values($fields);
101
    }
102
103
    protected function rating(): string
104
    {
105
        $solidStars = str_repeat('★', $this->review->rating);
106
        $emptyStars = str_repeat('☆', max(0, glsr()->constant('MAX_RATING', Rating::class) - $this->review->rating));
107
        $stars = $solidStars.$emptyStars;
108
        return glsr()->filterString('discord/stars', $stars, $this->review->rating, glsr()->constant('MAX_RATING', Rating::class));
109
    }
110
111
    protected function title(): string
112
    {
113
        $title = trim($this->review->title);
114
        return empty($title)
115
            ? '(no title)'
116
            : $title;
117
    }
118
}
119