Skip to content

Commit

Permalink
Set context using a callback/closure (#27)
Browse files Browse the repository at this point in the history
* removed incorrect docblock

* Set Custom context array by supplying a callback function/closure

* updated .travis.yml

* added a unit test

* updated README.md

* updated README.md

* fixed docblocks
  • Loading branch information
tylercd100 committed Apr 15, 2016
1 parent 698f1ba commit eb1c6a0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 14 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,35 @@ LERN::notify($e); //Notify the Exception

```

#### Changing the context

This function lets you change the context that is passed on to the Monolog handlers
```php
LERN::setContext(function(Exception $e, $context = []){

$context['exception'] = $e;

$app = app();

if(isset($app['auth']) && $user = $app['auth']->user())
{
if(empty($context['user']) or !is_array($context['user']))
{
$context['user'] = [];
}

if(!isset($context['user']['id']) && method_exists($user, 'getAuthIdentifier'))
{
$context['user']['id'] = $user->getAuthIdentifier();
}
}

return $context;
});

LERN::handle($exception);
```

## Further Reading and How-Tos
- [Creating relationships between Exceptions and Users](https://github.com/tylercd100/lern/wiki/Creating-relationships-between-exceptions-and-users)

Expand Down
46 changes: 32 additions & 14 deletions src/Components/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Notifier extends Component {
protected $log;
protected $messageCb;
protected $subjectCb;
protected $contextCb;

/**
* You can provide a Monolog Logger instance to use in the constructor
Expand Down Expand Up @@ -90,6 +91,36 @@ public function getSubject(Exception $e) {
}
}

/**
* Set an array or a closure to be called that will generate the context array for the notification
* @param callable|array $cb A closure or array that will be set for the context
*/
public function setContext($cb)
{
$this->contextCb = $this->wrapValueInClosure($cb);
return $this;
}

/**
* Returns the result of the context closure
* @param Exception $e The Exception instance that you want to build the context around
* @return array The context array
*/
public function getContext(Exception $e, $context = []) {

//This needs a better solution. How do I specific context needs for different drivers?
if (in_array('pushover', $this->config['drivers'])) {
$context['sound'] = $this->config['pushover']['sound'];
}

// Call the callback or return the default
if (is_callable($this->contextCb)) {
return $this->contextCb->__invoke($e, $context);
} else {
return $context;
}
}

/**
* Pushes on another Monolog Handler
* @param HandlerInterface $handler The handler instance to add on
Expand All @@ -114,10 +145,9 @@ public function send(Exception $e, array $context = []) {

$message = $this->getMessage($e);
$subject = $this->getSubject($e);
$context = $this->getContext($e, $context);

try {
$context = $this->buildContext($context);

$notify = new Notify($this->config, $this->log, $subject);

$notify->critical($message, $context);
Expand All @@ -128,16 +158,4 @@ public function send(Exception $e, array $context = []) {
throw new NotifierFailedException($e->getMessage(), $code, $e);
}
}

/**
* Builds a context array to pass to Monolog
* @param array $context Additional information that you would like to pass to Monolog
* @return array The modified context array
*/
protected function buildContext(array $context = []) {
if (in_array('pushover', $this->config['drivers'])) {
$context['sound'] = $this->config['pushover']['sound'];
}
return $context;
}
}
6 changes: 6 additions & 0 deletions tests/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public function testNotifierReturnsTheCorrectMessageWhenUsingClosure(){
$this->assertEquals($result,"This is a test");
}

public function testNotifierReturnsTheCorrectContextWhenUsingClosure(){
$this->notifier->setContext(function($e,$context){return ["text"=>"This is a test"];});
$result = $this->notifier->getContext(new Exception);
$this->assertEquals($result,["text"=>"This is a test"]);
}

public function testNotifierReturnsTheCorrectMessageWhenUsingString(){
$this->notifier->setMessage("This is a test");
$result = $this->notifier->getMessage(new Exception);
Expand Down

0 comments on commit eb1c6a0

Please sign in to comment.