CommentNotifier::onAfterPostComment()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 13
rs 10
cc 4
nc 3
nop 1
1
<?php
2
3
namespace SilverStripe\CommentNotifications\Extensions;
4
5
use SilverStripe\Comments\Model\Comment;
6
use SilverStripe\Security\Member;
7
use SilverStripe\Control\Email\Email;
8
use SilverStripe\Core\Extension;
9
10
/**
11
 * Extension applied to CommentingController to invoke notifications
12
 *
13
 * Relies on the parent object to {@see Comment} having the {@see CommentNotifiable} extension applied..
14
 */
15
class CommentNotifier extends Extension
16
{
17
18
    /**
19
     * Notify Members of the post there is a new comment.
20
     *
21
     * @param Comment $comment
22
     */
23
    public function onAfterPostComment(Comment $comment)
24
    {
25
        $parent = $comment->Parent();
26
27
        if (!$parent || !$parent->hasMethod('notificationRecipients')) {
28
            return;
29
        }
30
31
        // Ask parent to submit all recipients
32
        $recipients = $parent->notificationRecipients($comment);
33
34
        foreach ($recipients as $recipient) {
35
            $this->notifyCommentRecipient($comment, $parent, $recipient);
36
        }
37
    }
38
39
    /**
40
     * Send comment notification to a given recipient
41
     *
42
     * @param Comment $comment
43
     * @param DataObject $parent Object with the {@see CommentNotifiable} extension applied
0 ignored issues
show
Bug introduced by
The type SilverStripe\CommentNoti...s\Extensions\DataObject 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...
44
     * @param Member|string $recipient Either a member object or an email address to which notifications should be sent
45
     */
46
    public function notifyCommentRecipient($comment, $parent, $recipient)
47
    {
48
        $subject = $parent->notificationSubject($comment, $recipient);
49
        $sender = $parent->notificationSender($comment, $recipient);
50
        $template = $parent->notificationTemplate($comment, $recipient);
51
52
        // Validate email
53
        // Important in case of the owner being a default-admin or a username with no contact email
54
        $to = ($recipient instanceof Member) ? $recipient->Email : $recipient;
55
56
        if (!Email::is_valid_address($to)) {
57
            return;
58
        }
59
60
        // Prepare the email
61
        $email = Email::create();
62
        $email->setSubject($subject);
63
        $email->setFrom($sender);
64
        $email->setTo($to);
65
        $email->setHTMLTemplate($template);
66
67
        if ($recipient instanceof Member) {
68
            $email->setData([
69
                'Parent' => $parent,
70
                'Comment' => $comment,
71
                'Recipient' => $recipient,
72
                'ApproveLink' => $comment->ApproveLink($recipient),
73
                'HamLink' => $comment->HamLink($recipient),
74
                'SpamLink' => $comment->SpamLink($recipient),
75
                'DeleteLink' => $comment->DeleteLink($recipient),
76
            ]);
77
        } else {
78
            $email->setData([
79
                'Parent' => $parent,
80
                'Comment' => $comment,
81
                'ApproveLink' => false,
82
                'SpamLink' => false,
83
                'DeleteLink' => false,
84
                'HamLink' => false,
85
                'Recipient' => $recipient
86
            ]);
87
        }
88
89
        $this->owner->invokeWithExtensions('updateCommentNotification', $email, $comment, $recipient);
90
91
        return $email->send();
92
    }
93
}
94