Passed
Push — master ( d47355...85ec42 )
by Paul
04:15
created

Notification::buildSlackNotification()   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 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
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\Modules\Email;
7
use GeminiLabs\SiteReviews\Modules\Slack;
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\Modules\Slack was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use GeminiLabs\SiteReviews\Review;
9
use WP_Post;
10
11
class Notification
12
{
13
	/**
14
	 * @var bool
15
	 */
16
	protected $email;
17
18
	/**
19
	 * @var Review
20
	 */
21
	protected $review;
22
23
	/**
24
	 * @var bool
25
	 */
26
	protected $slack;
27
28
	/**
29
	 * @var array
30
	 */
31
	protected $types;
32
33 1
	public function __construct()
34
	{
35 1
		$types = glsr( OptionManager::class )->get( 'settings.general.notification', [] );
36 1
		$this->email = count( array_intersect( ['admin', 'author', 'custom'], $types )) > 0;
37 1
		$this->slack = in_array( 'slack', $types );
38 1
		$this->types = $types;
39 1
	}
40
41
	/**
42
	 * @return void
43
	 */
44 1
	public function send( Review $review )
45
	{
46 1
		if( empty( $this->types ))return;
47
		$this->review = $review;
48
		$args = [
49
			'link' => $this->getLink(),
50
			'title' => $this->getTitle(),
51
		];
52
		if( $this->email ) {
53
			$this->sendToEmail( $args );
54
		}
55
		if( $this->slack ) {
56
			$this->sendToSlack( $args );
57
		}
58
	}
59
60
	/**
61
	 * @return Email
62
	 */
63
	protected function buildEmail( array $args )
64
	{
65
		return glsr( Email::class )->compose([
66
			'to' => $this->getEmailAddresses(),
67
			'subject' => $args['title'],
68
			'template' => 'email-notification',
69
			'template-tags' => [
70
				'review_author' => $this->review->author,
71
				'review_content' => $this->review->content,
72
				'review_email' => $this->review->email,
73
				'review_ip' => $this->review->ip_address,
74
				'review_link' => sprintf( '<a href="%1$s">%1$s</a>', $args['link'] ),
75
				'review_rating' => $this->review->rating,
76
				'review_title' => $this->review->title,
77
			],
78
		]);
79
	}
80
81
	/**
82
	 * @return Email
83
	 */
84
	protected function buildSlackNotification( array $args )
85
	{
86
		return glsr( Slack::class )->compose( $this->review, [
87
			'fallback' => $this->buildEmail( $args )->read( 'plaintext' ),
88
			'link' => sprintf( '<%s|%s>', $args['link'], __( 'View Review', 'site-reviews' )),
89
			'pretext' => $args['title'],
90
		]);
91
	}
92
93
	/**
94
	 * @return array
95
	 */
96
	protected function getEmailAddresses()
97
	{
98
		$emails = [];
99
		if( in_array( 'admin', $this->types )) {
100
			$emails[] = get_option( 'admin_email' );
101
		}
102
		if( in_array( 'author', $this->types )) {
103
			$assignedPost = get_post( intval( $this->review->assigned_to ));
104
			if( $assignedPost instanceof WP_Post ) {
105
				$emails[] = get_the_author_meta( 'user_email', $assignedPost->ID );
106
			}
107
		}
108
		if( in_array( 'custom', $this->types )) {
109
			$customEmails = glsr( OptionManager::class )->get( 'settings.general.notification_email' );
110
			$customEmails = str_replace( [' ', ',', ';'], ',', $customEmails );
111
			$customEmails = explode( ',', $customEmails );
112
			$emails = array_merge( $emails, $customEmails );
113
		}
114
		$emails = array_filter( array_keys( array_flip( $emails )));
115
		return apply_filters( 'site-reviews/notification/emails', $emails, $this->review );
116
	}
117
118
	/**
119
	 * @return string
120
	 */
121
	protected function getLink()
122
	{
123
		return admin_url( 'post.php?post='.$this->review->ID.'&action=edit' );
124
	}
125
126
	/**
127
	 * @return string
128
	 */
129
	protected function getTitle()
130
	{
131
		$assignedTitle = get_the_title( intval( $this->review->assigned_to ));
132
		$title = _nx(
133
			'New %s-star review',
134
			'New %s-star review of: %s',
135
			intval( empty( $assignedTitle )),
136
			'This string differs depending on whether or not the review has been assigned to a post.',
137
			'site-reviews'
138
		);
139
		$title = sprintf( '[%s] %s',
140
			wp_specialchars_decode( strval( get_option( 'blogname' )), ENT_QUOTES ),
141
			sprintf( $title, $this->review->rating, $assignedTitle )
142
		);
143
		return apply_filters( 'site-reviews/notification/title', $title, $this->review );
144
	}
145
146
	/**
147
	 * @return void
148
	 */
149
	protected function sendToEmail( array $args )
150
	{
151
		$email = $this->buildEmail( $args );
152
		if( empty( $email->to )) {
153
			glsr_log()->error( 'Email notification was not sent: missing email address' );
154
			return;
155
		}
156
		if( $email->send() === false ) {
157
			glsr_log()->error( 'Email notification was not sent: wp_mail() failed' )->debug( $email );
158
		}
159
	}
160
161
	/**
162
	 * @return void
163
	 */
164
	protected function sendToSlack( array $args )
165
	{
166
		$notification = $this->buildSlackNotification( $args );
167
		$result = $notification->send();
168
		if( is_wp_error( $result )) {
169
			$notification->review = null;
0 ignored issues
show
Bug introduced by
The property review does not seem to exist on GeminiLabs\SiteReviews\Modules\Email.
Loading history...
170
			glsr_log()->error( $result->get_error_message() )->debug( $notification );
171
		}
172
	}
173
}
174