Skip to content

Commit

Permalink
add mail channel, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tuyakhov committed Oct 9, 2016
1 parent cdb6551 commit f1f97e5
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 63 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"autoload": {
"psr-4": {
"tuyakhov\\notifications\\": "src/"
"tuyakhov\\notifications\\": "src/",
"tuyakhov\\notifications\\tests\\": "tests/"
}
}
}
73 changes: 39 additions & 34 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/NotificationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
namespace tuyakhov\notifications;


use tuyakhov\notifications\messages\AbstractMessage;

interface NotificationInterface
{
/**
* @param mixed $recipient
* @return mixed
* @param string $channel
* @return AbstractMessage
*/
public function export($recipient = null);
public function exportFor($channel);

/**
* @return array
Expand Down
32 changes: 32 additions & 0 deletions src/NotificationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* @copyright Anton Tuyakhov <[email protected]>
*/

namespace tuyakhov\notifications;

use yii\helpers\Inflector;

class NotificationTrait implements NotificationInterface
{
public function broadcastOn()
{
$channels = [];
$methods = get_class_methods($this);
foreach ($methods as $method) {
if (($channel = stristr($method, 'exportFor', true)) !== false) {
$channels[] = $channel;
}
}
return $channels;
}

public function exportFor($channel)
{
if (method_exists($this, $method = 'exportFor'.Inflector::id2camel($channel))) {
return $this->{$method}();
}
throw new \InvalidArgumentException("Cannot find message export for chanel `{$channel}`");
}

}
3 changes: 2 additions & 1 deletion src/channels/ChannelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

namespace tuyakhov\notifications\channels;

use tuyakhov\notifications\NotifiableInterface;
use tuyakhov\notifications\NotificationInterface;

interface ChannelInterface
{
public function send($recipient, NotificationInterface $notification);
public function send(NotifiableInterface $recipient, NotificationInterface $notification);
}
21 changes: 18 additions & 3 deletions src/channels/MailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,41 @@
namespace tuyakhov\notifications\channels;


use tuyakhov\notifications\messages\MailMessage;
use tuyakhov\notifications\NotifiableInterface;
use tuyakhov\notifications\NotificationInterface;
use yii\base\Component;
use yii\di\Instance;
use yii\mail\MailerInterface;

class MailNotificationChannel extends Component implements ChannelInterface
class MailChannel extends Component implements ChannelInterface
{
/**
* @var $mailer MailerInterface|array|string the mailer object or the application component ID of the mailer object.
*/
public $mailer = 'mailer';

/**
* The message sender.
* @var string
*/
public $from;

public function init()
{
parent::init();
$this->mailer = Instance::ensure($this->mailer, 'yii\mail\MailerInterface');
}

public function send($recipient, NotificationInterface $notification)
public function send(NotifiableInterface $recipient, NotificationInterface $notification)
{
$config = $notification->export($recipient);
/**
* @var $message MailMessage
*/
$message = $notification->exportFor('mail');
$this->mailer->compose($message->view, $message->viewData)
->setFrom(isset($message->from) ? $message->from : $this->from)
->setTo($recipient->routeNotificationFor('mail'))
->send();
}
}
25 changes: 19 additions & 6 deletions src/messages/AbstractMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@
namespace tuyakhov\notifications\messages;


abstract class AbstractMessage implements MessageInterface
use yii\base\Object;

abstract class AbstractMessage extends Object
{
protected $notification;
/**
* The "level" of the notification (info, success, error).
* @var string
*/
public $level = 'info';

/**
* The subject of the notification.
* @var string
*/
public $subject;

public function __construct($notification)
{

}
/**
* The notification's message body
* @var string
*/
public $body;
}
28 changes: 28 additions & 0 deletions src/messages/MailMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* @copyright Anton Tuyakhov <[email protected]>
*/

namespace tuyakhov\notifications\messages;


class MailMessage extends AbstractMessage
{
/**
* The view to be used for rendering the message body.
* @var string|array|null $view
*/
public $view;

/**
* The parameters (name-value pairs) that will be extracted and made available in the view file.
* @var array
*/
public $viewData;

/**
* The message sender.
* @var string
*/
public $from;
}
Loading

0 comments on commit f1f97e5

Please sign in to comment.