CommentNotifiable::notificationTemplate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace SilverStripe\CommentNotifications\Extensions;
4
5
use SilverStripe\Comments\Model\Comment;
6
use SilverStripe\ORM\DataExtension;
7
use SilverStripe\Control\Email\Email;
8
use SilverStripe\Security\Member;
9
use Traversable;
10
11
class CommentNotifiable extends DataExtension
12
{
13
14
    /**
15
     * Default subject line if the owner doesn't override it
16
     *
17
     * @config
18
     * @var string
19
     */
20
    private static $default_notification_subject = 'A new comment has been posted';
21
22
    /**
23
     * Default sender
24
     *
25
     * @config
26
     * @var string
27
     */
28
    private static $default_notification_sender = 'noreply@{host}';
29
30
    /**
31
     * Default template to use for comment notifications
32
     *
33
     * @config
34
     * @var string
35
     */
36
    private static $default_notification_template = 'SilverStripe\\CommentNotifications\\CommentEmail';
37
38
    /**
39
     * Return the list of members or emails to send comment notifications to
40
     *
41
     * @param Comment $comment
42
     * @return array|Traversable
43
     */
44
    public function notificationRecipients($comment)
45
    {
46
        $list = [];
47
48
        if ($adminEmail = Email::config()->admin_email) {
49
            $list[] = $adminEmail;
50
        }
51
52
        $this->owner->invokeWithExtensions('updateNotificationRecipients', $list, $comment);
53
54
        return $list;
55
    }
56
57
    /**
58
     * Gets the email subject line for comment notifications
59
     *
60
     * @param Comment $comment Comment
61
     * @param Member|string $recipient
62
     * @return string
63
     */
64
    public function notificationSubject($comment, $recipient)
65
    {
66
        $subject = $this->owner->config()->default_notification_subject;
67
68
        $this->owner->invokeWithExtensions('updateNotificationSubject', $subject, $comment, $recipient);
69
70
        return $subject;
71
    }
72
73
    /**
74
     * Get the sender email address to use for email notifications
75
     *
76
     * @param Comment $comment
77
     * @param Member|string $recipient
78
     * @return string
79
     */
80
    public function notificationSender($comment, $recipient)
81
    {
82
        $sender = $this->owner->config()->default_notification_sender;
83
84
        // Do hostname substitution
85
        $host = isset($_SERVER['HTTP_HOST'])
86
            ? preg_replace('/^www\./i', '', $_SERVER['HTTP_HOST'])
87
            : 'localhost';
88
        $sender = preg_replace('/{host}/', $host, $sender);
89
90
        $this->owner->invokeWithExtensions('updateNotificationSender', $sender, $comment, $recipient);
91
92
        return $sender;
93
    }
94
95
    /**
96
     * Determine the template to use for this email
97
     *
98
     * @param Comment $comment
99
     * @param Member|string $recipient
100
     * @return string Template name (excluding .ss extension)
101
     */
102
    public function notificationTemplate($comment, $recipient)
103
    {
104
        $template = $this->owner->config()->default_notification_template;
105
106
        $this->owner->invokeWithExtensions('updateNotificationTemplate', $template, $comment, $recipient);
107
108
        return $template;
109
    }
110
111
    /**
112
     * Update the notification email
113
     *
114
     * @param Email $email
115
     * @param Comment $comment
116
     * @param Member|string $recipient
117
     */
118
    public function updateCommentNotification($email, $comment, $recipient)
0 ignored issues
show
Unused Code introduced by
The parameter $comment is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

118
    public function updateCommentNotification($email, /** @scrutinizer ignore-unused */ $comment, $recipient)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $recipient is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

118
    public function updateCommentNotification($email, $comment, /** @scrutinizer ignore-unused */ $recipient)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $email is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

118
    public function updateCommentNotification(/** @scrutinizer ignore-unused */ $email, $comment, $recipient)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
    {
120
        //
121
    }
122
}
123