Skip to content

Instantly share code, notes, and snippets.

@ahmadshah
Last active August 29, 2015 14:07
Show Gist options
  • Save ahmadshah/4988c153aa20b4d2ec45 to your computer and use it in GitHub Desktop.
Save ahmadshah/4988c153aa20b4d2ec45 to your computer and use it in GitHub Desktop.
Email notifier
<?php namespace Support\Notifier;
use Model\User;
use Illuminate\Support\Arr;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
abstract class AbstractableNotifier {
/**
* Expression language component
*
* @var \Symfony\Component\ExpressionLanguage\ExpressionLanguage
*/
protected $expression;
/**
* Notifier settings and configuration
*
* @var array
*/
protected $settings = [];
/**
* User eloquent model
*
* @var \Rokki\Model\User
*/
protected $recipient;
/**
* [$data description]
* @var [type]
*/
protected $data = [];
/**
* [$rules description]
* @var [type]
*/
protected $variables = [];
/**
* [__construct description]
* @param ExpressionLanguage $expression [description]
*/
public function __construct(ExpressionLanguage $expression)
{
$this->expression = $expression;
}
/**
* [setRecipient description]
* @param User $user [description]
*/
public function setRecipient(User $user)
{
$this->recipient = $user;
return $this;
}
/**
* [getRecipient description]
* @return [type] [description]
*/
public function getRecipient()
{
return $this->recipient;
}
/**
* [setSubject description]
* @param [type] $subject [description]
*/
public function setSubject($subject)
{
Arr::set($this->settings, 'subject', $subject);
return $this;
}
/**
* [getSubject description]
* @return [type] [description]
*/
public function getSubject()
{
return Arr::get($this->settings, 'subject', null);
}
/**
* [setView description]
* @param [type] $view [description]
*/
public function setView($view)
{
Arr::set($this->settings, 'view', $view);
return $this;
}
/**
* [getView description]
* @return [type] [description]
*/
public function getView()
{
return Arr::get($this->settings, 'view', null);
}
/**
* [setData description]
* @param array $data [description]
*/
public function setData(array $data)
{
if ($this->evaluateData($data)) {
$this->data = $data;
}
return $this;
}
/**
* [getData description]
* @return [type] [description]
*/
public function getData()
{
return $this->data;
}
/**
* [notify description]
* @return [type] [description]
*/
public function notify()
{
return $this->recipient->notify($this->getSubject, $this->getView(), $this->getData());
}
/**
* [evaluateData description]
* @param [type] $data [description]
* @return [type] [description]
*/
protected function evaluateData($data)
{
$rule = $this->buildExpressionRule();
return $this->expression->evaluate($rule, ['data' => $data]);
}
/**
* [buildExpressionRule description]
* @return [type] [description]
*/
protected function buildExpressionRule()
{
$variables = [];
foreach ($this->variables as $variable) {
$variables[] = 'data["'.$variable.'"]';
}
return implode(' in data and ', $variables);
}
}
<?php namespace Support\Notifier;
class ActivationNotifier extends AbstractableNotifier {
protected $settings = [
'subject' => 'Account activation notification',
'view' => 'app::emails.notifier.activation'
];
protected $variables = [
'first_name',
'last_name',
];
}
Route::get('expression', function () {
$test = ['first_name' => 'Foo', 'last_name' => 'Bar'];
$notifier = App::make('notifier.activation')->setData($test);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment