Skip to content

Commit

Permalink
Replace extend with invokeWithExtensions (Fixes silverstripe#16)
Browse files Browse the repository at this point in the history
Allows concrete commenting classes to define behaviour rather than requiring configuration to be set on an extension to the class.
  • Loading branch information
wilr committed Sep 27, 2017
1 parent 15d4ff5 commit 7c188b8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
17 changes: 11 additions & 6 deletions src/Extensions/CommentNotifiable.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ class CommentNotifiable extends DataExtension
*/
public function notificationRecipients($comment)
{
// Override this in your extending class to declare recipients
$list = array();
$list = [];

if ($adminEmail = Email::config()->admin_email) {
$list[] = $adminEmail;
}

$this->owner->extend('updateNotificationRecipients', $list, $comment);
$this->owner->invokeWithExtensions('updateNotificationRecipients', $list, $comment);

return $list;
}
Expand All @@ -62,7 +61,9 @@ public function notificationRecipients($comment)
public function notificationSubject($comment, $recipient)
{
$subject = $this->owner->config()->default_notification_subject;
$this->owner->extend('updateNotificationSubject', $subject, $comment, $recipient);

$this->owner->invokeWithExtensions('updateNotificationSubject', $subject, $comment, $recipient);

return $subject;
}

Expand All @@ -83,7 +84,8 @@ public function notificationSender($comment, $recipient)
: 'localhost';
$sender = preg_replace('/{host}/', $host, $sender);

$this->owner->extend('updateNotificationSender', $sender, $comment, $recipient);
$this->owner->invokeWithExtensions('updateNotificationSender', $sender, $comment, $recipient);

return $sender;
}

Expand All @@ -97,7 +99,9 @@ public function notificationSender($comment, $recipient)
public function notificationTemplate($comment, $recipient)
{
$template = $this->owner->config()->default_notification_template;
$this->owner->extend('updateNotificationTemplate', $template, $comment, $recipient);

$this->owner->invokeWithExtensions('updateNotificationTemplate', $template, $comment, $recipient);

return $template;
}

Expand All @@ -110,5 +114,6 @@ public function notificationTemplate($comment, $recipient)
*/
public function updateCommentNotification($email, $comment, $recipient)
{
//
}
}
7 changes: 1 addition & 6 deletions src/Extensions/CommentNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,7 @@ public function notifyCommentRecipient($comment, $parent, $recipient)
]);
}

// Until invokeWithExtensions supports multiple arguments
if ($this->owner->hasMethod('updateCommentNotification')) {
$this->owner->updateCommentNotification($email, $comment, $recipient);
}

$this->owner->extend('updateCommentNotification', $email, $comment, $recipient);
$this->owner->invokeWithExtensions('updateCommentNotification', $email, $comment, $recipient);

return $email->send();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/CommentNotifiableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testGetRecipients()
$item1 = $this->objFromFixture(CommentNotifiableTestDataObject::class, 'item1');
$item2 = $this->objFromFixture(CommentNotifiableTestDataObject::class, 'item2');

$this->assertEquals(array('[email protected]'), $item1->notificationRecipients($comment1));
$this->assertEquals(array('[email protected]', '[email protected]'), $item1->notificationRecipients($comment1));
$this->assertEquals(array('[email protected]'), $item2->notificationRecipients($comment2));
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Model/CommentNotifiableTestDataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ class CommentNotifiableTestDataObject extends DataObject implements TestOnly

private static $table_name = 'CommentNotifiableTestDataObject';

public function notificationRecipients($comment)
public function updateNotificationRecipients(&$list, $comment)
{
$author = $this->Author();

if ($author && $author->exists()) {
return [$author->Email];
$list[] = $author->Email;
}

return parent::notificationRecipients($comment);
}

public function Link($action = false)
Expand Down

0 comments on commit 7c188b8

Please sign in to comment.