Skip to content
This repository was archived by the owner on Jun 1, 2019. It is now read-only.

Commit 570e0d1

Browse files
committed
finishing beforeHandler feature
1 parent 4163be6 commit 570e0d1

File tree

6 files changed

+47
-44
lines changed

6 files changed

+47
-44
lines changed

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,21 @@ public function getActivityDescriptionForEvent($eventName)
122122
```
123123
The result of this function will be logged, unless the result is an empty string.
124124

125-
### Disable logging under certain conditions
126-
If you want to disable logging under certain conditions, such as for a specific user, create a class in your application namespace that implements the `Spatie\Activitylog\Handlers\BeforeHandlerInterface`. This interface defines an `ignore()` method in which you can code any custom logic to determine whether logging should be ignored or not. Returning `true` from this function will ignore logging, returning `false` will continue with the logging. Add the namespaced class name to the `beforeCallback` field in the configuration file:
125+
### Using a before handler.
126+
If you want to disable logging under certain conditions,
127+
such as for a specific user, create a class in your application
128+
namespace that implements the `Spatie\Activitylog\Handlers\BeforeHandlerInterface`.
129+
130+
This interface defines an `shouldLog()` method in which you can code any custom logic to determine
131+
whether logging should be ignored or not. You must return `true` the call should be logged.
132+
133+
To en the namespaced class nameto the `beforeHandler` field in the configuration file:
127134
```php
128-
'beforeCallback' => '\App\Handlers\BeforeHandler',
135+
'beforeHandler' => '\App\Handlers\BeforeHandler',
129136
```
130-
For example, this callback class could look like this to disable logging for the first user:
137+
138+
For example, this callback class could look like this to disable
139+
logging a user with id of 1:
131140
```php
132141
<?php
133142

@@ -137,14 +146,11 @@ use Spatie\Activitylog\Handlers\BeforeHandlerInterface;
137146

138147
class BeforeHandler implements BeforeHandlerInterface
139148
{
140-
public function ignore()
149+
public function shouldLog($text, $userId)
141150
{
142-
if (\Auth::user()->id == 1)
143-
{
144-
return true;
145-
}
146-
147-
return false;
151+
if ($userId == 1) return false;
152+
153+
return true;
148154
}
149155
}
150156
```

src/Spatie/Activitylog/ActivitylogSupervisor.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ class ActivitylogSupervisor
2525
* Also register Laravels Log Handler if needed.
2626
*
2727
* @param Handlers\ActivitylogHandlerInterface $logHandler
28-
* @param Handlers\BeforeHandlerInterface $beforeHandler
2928
* @param Repository $config
3029
* @param Guard $auth
3130
*/
32-
public function __construct(Handlers\ActivitylogHandlerInterface $logHandler, Handlers\BeforeHandler $beforeHandler, Repository $config, Guard $auth)
31+
public function __construct(Handlers\ActivitylogHandlerInterface $logHandler, Repository $config, Guard $auth)
3332
{
3433
$this->config = $config;
3534

@@ -39,12 +38,6 @@ public function __construct(Handlers\ActivitylogHandlerInterface $logHandler, Ha
3938
$this->logHandlers[] = new DefaultLaravelHandler();
4039
}
4140

42-
if ($beforeCallback = $this->config->get('activitylog.beforeCallback')) {
43-
$this->beforeHandler = new $beforeCallback;
44-
} else {
45-
$this->beforeHandler = $beforeHandler;
46-
}
47-
4841
$this->auth = $auth;
4942
}
5043

@@ -58,12 +51,12 @@ public function __construct(Handlers\ActivitylogHandlerInterface $logHandler, Ha
5851
*/
5952
public function log($text, $userId = '')
6053
{
61-
if ($this->beforeHandler->ignore()) {
54+
$userId = $this->normalizeUserId($userId);
55+
56+
if (! $this->shouldLogCall($text, $userId)) {
6257
return false;
6358
}
6459

65-
$userId = $this->normalizeUserId($userId);
66-
6760
$ipAddress = Request::getClientIp();
6861

6962
foreach ($this->logHandlers as $logHandler) {
@@ -114,4 +107,23 @@ public function normalizeUserId($userId)
114107

115108
return '';
116109
}
110+
111+
/**
112+
* Determine if this call should be logged.
113+
*
114+
* @param $text
115+
* @param $userId
116+
*
117+
* @return bool
118+
*/
119+
protected function shouldLogCall($text, $userId)
120+
{
121+
$beforeHandler = $this->config('activitylog.beforeHandler');
122+
123+
if (is_null($beforeHandler) || $beforeHandler == '') {
124+
return true;
125+
}
126+
127+
return app($beforeHandler)->shouldLog($text, $userId);
128+
}
117129
}

src/Spatie/Activitylog/Handlers/BeforeHandler.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Spatie/Activitylog/Handlers/BeforeHandlerInterface.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
interface BeforeHandlerInterface
66
{
77
/**
8-
* Don't log if this function returns true.
8+
* Call to the log will only be made if this function returns true.
9+
*
10+
* @param string $text The text that will be logged
11+
* @param int|null $userId The id of the user that is associated with the log call
912
*
1013
* @return bool
1114
*/
12-
public function ignore();
15+
public function shouldLog($text, $userId);
1316
}

src/config/activitylog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@
4545
| reference it here.
4646
|
4747
*/
48-
'beforeCallback' => null,
48+
'beforeHandler' => null,
4949
];

tests/ActivityLogSupervisorTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use Illuminate\Support\Collection;
43
use Illuminate\Support\Facades\Auth as Auth;
54
use Spatie\Activitylog\ActivitylogSupervisor;
65

@@ -13,12 +12,11 @@ class ActivityLogSupervistorTest extends PHPUnit_Framework_TestCase
1312
public function setUp()
1413
{
1514
$this->logHandler = Mockery::mock('\Spatie\Activitylog\Handlers\EloquentHandler');
16-
$this->beforeHandler = Mockery::mock('\Spatie\Activitylog\Handlers\BeforeHandler');
1715
$this->config = Mockery::mock('\Illuminate\Config\Repository');
1816
$this->auth = Mockery::mock('Illuminate\Auth\Guard');
1917

2018
$this->config->shouldReceive('get')->andReturn(false);
21-
$this->activityLogSupervisor = new ActivitylogSupervisor($this->logHandler, $this->beforeHandler, $this->config, $this->auth);
19+
$this->activityLogSupervisor = new ActivitylogSupervisor($this->logHandler, $this->config, $this->auth);
2220
}
2321

2422
/**

0 commit comments

Comments
 (0)