Passed
Push — master ( 1dac3a...f8914b )
by Paul
04:03
created

Slack::buildLinkField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Database\OptionManager;
6
use GeminiLabs\SiteReviews\Defaults\SlackDefaults;
7
use GeminiLabs\SiteReviews\Modules\Rating;
8
use GeminiLabs\SiteReviews\Review;
9
use WP_Error;
10
11
class Slack
12
{
13
	/**
14
	 * @var string
15
	 */
16
	public $endpoint;
17
18
	/**
19
	 * @var array
20
	 */
21
	public $notification;
22
23
	/**
24
	 * @var Review
25
	 */
26
	public $review;
27
28
	public function __construct()
29
	{
30
		$this->endpoint = glsr( OptionManager::class )->get( 'settings.general.slack_webhook' );
31
	}
32
33
	/**
34
	 * @return Slack
35
	 */
36
	public function compose( Review $review, array $notification )
37
	{
38
		if( empty( $this->endpoint ))return;
39
		$args = shortcode_atts( glsr( SlackDefaults::class )->defaults(), $notification );
40
		$this->review = $review;
41
		$notification = [
42
			'icon_url' => $args['icon_url'],
43
			'username' => $args['username'],
44
			'attachments' => [[
45
				'pretext' => $args['pretext'],
46
				'color' => $args['color'],
47
				'fallback' => $args['fallback'],
48
				'fields' => $this->buildFields( $args['link'] ),
49
			]],
50
		];
51
		$this->notification = apply_filters( 'site-reviews/slack/compose', $notification, $this );
52
		return $this;
53
	}
54
55
	/**
56
	 * @return WP_Error|array
57
	 */
58
	public function send()
59
	{
60
		if( empty( $this->endpoint )) {
61
			return new WP_Error( 'slack', 'Slack notification was not sent: missing endpoint' );
62
		}
63
		return wp_remote_post( $this->endpoint, [
64
			'blocking' => false,
65
			'body' => json_encode( $this->notification ),
66
			'headers' => ['Content-Type' => 'application/json'],
67
			'httpversion' => '1.0',
68
			'method' => 'POST',
69
			'redirection' => 5,
70
			'sslverify' => false,
71
			'timeout' => 45,
72
		]);
73
	}
74
75
	/**
76
	 * @return array
77
	 */
78
	protected function buildAuthorField()
79
	{
80
		$email = !empty( $this->review->email )
81
			? '<'.$this->review->email.'>'
82
			: '';
83
		$author = trim( rtrim( $this->review->author ).' '.$email );
84
		return ['value' => implode( ' - ', array_filter( [$author, $this->review->ip_address] ))];
85
	}
86
87
	/**
88
	 * @return array
89
	 */
90
	protected function buildContentField()
91
	{
92
		return !empty( $this->review->content )
93
			? ['value' => $this->review->content]
94
			: [];
95
	}
96
97
	/**
98
	 * @return array
99
	 */
100
	protected function buildFields( $link )
101
	{
102
		$fields = [
103
			$this->buildStarsField(),
104
			$this->buildTitleField(),
105
			$this->buildContentField(),
106
			$this->buildAuthorField(),
107
		];
108
		if( !empty( $link )) {
109
			$fields[] = ['value' => $link];
110
		}
111
		return array_filter( $fields );
112
	}
113
114
	/**
115
	 * @return array
116
	 */
117
	protected function buildStarsField()
118
	{
119
		$solidStars = str_repeat( '★', $this->review->rating );
120
		$emptyStars = str_repeat( '☆', max( 0, Rating::MAX_RATING - $this->review->rating ));
121
		$stars = $solidStars.$emptyStars;
122
		$stars = apply_filters( 'site-reviews/slack/stars', $stars, $this->review->rating, Rating::MAX_RATING );
123
		return ['title' => $stars];
124
	}
125
126
	/**
127
	 * @return array
128
	 */
129
	protected function buildTitleField()
130
	{
131
		return !empty( $this->review->title )
132
			? ['title' => $this->review->title]
133
			: [];
134
	}
135
}
136