Passed
Push — master ( 85ec42...1dac3a )
by Paul
06:37
created

Slack::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 0
dl 0
loc 14
ccs 0
cts 14
cp 0
crap 6
rs 9.9
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
		$this->review = $review;
40
		$args = shortcode_atts( glsr( SlackDefaults::class )->defaults(), $notification );
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(),
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( $endpoint, [
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $endpoint seems to be never defined.
Loading history...
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 buildLinkField()
101
	{
102
		return !empty( $this->link )
103
			? ['value' => $this->link]
0 ignored issues
show
Bug Best Practice introduced by
The property link does not exist on GeminiLabs\SiteReviews\Modules\Slack. Did you maybe forget to declare it?
Loading history...
104
			: [];
105
	}
106
107
	/**
108
	 * @return array
109
	 */
110
	protected function buildFields()
111
	{
112
		$fields = [
113
			$this->buildStarsField(),
114
			$this->buildTitleField(),
115
			$this->buildContentField(),
116
			$this->buildAuthorField(),
117
			$this->buildLinkField(),
118
		];
119
		return array_filter( $fields );
120
	}
121
122
	/**
123
	 * @return array
124
	 */
125
	protected function buildStarsField()
126
	{
127
		$solidStars = str_repeat( '★', $this->review->rating );
128
		$emptyStars = str_repeat( '☆', max( 0, Rating::MAX_RATING - $this->review->rating ));
129
		$stars = $solidStars.$emptyStars;
130
		$stars = apply_filters( 'site-reviews/slack/stars', $stars, $this->review->rating, Rating::MAX_RATING );
131
		return ['title' => $stars];
132
	}
133
134
	/**
135
	 * @return array
136
	 */
137
	protected function buildTitleField()
138
	{
139
		return !empty( $this->review->title )
140
			? ['title' => $this->review->title]
141
			: [];
142
	}
143
}
144