Skip to content

Commit

Permalink
Use Auth facade to support Lumen
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmcdonald-uk committed Oct 21, 2019
1 parent c689c86 commit e530ea5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/Listeners/Creating.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Wildside\Userstamps\Listeners;

use Illuminate\Support\Facades\Auth;

class Creating
{
/**
Expand All @@ -17,11 +19,11 @@ public function handle($model)
}

if (is_null($model->{$model->getCreatedByColumn()})) {
$model->{$model->getCreatedByColumn()} = auth()->id();
$model->{$model->getCreatedByColumn()} = Auth::id();
}

if (is_null($model->{$model->getUpdatedByColumn()}) && ! is_null($model->getUpdatedByColumn())) {
$model->{$model->getUpdatedByColumn()} = auth()->id();
$model->{$model->getUpdatedByColumn()} = Auth::id();
}
}
}
4 changes: 3 additions & 1 deletion src/Listeners/Deleting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Wildside\Userstamps\Listeners;

use Illuminate\Support\Facades\Auth;

class Deleting
{
/**
Expand All @@ -17,7 +19,7 @@ public function handle($model)
}

if (is_null($model->{$model->getDeletedByColumn()})) {
$model->{$model->getDeletedByColumn()} = auth()->id();
$model->{$model->getDeletedByColumn()} = Auth::id();
}

$dispatcher = $model->getEventDispatcher();
Expand Down
6 changes: 4 additions & 2 deletions src/Listeners/Updating.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Wildside\Userstamps\Listeners;

use Illuminate\Support\Facades\Auth;

class Updating
{
/**
Expand All @@ -12,10 +14,10 @@ class Updating
*/
public function handle($model)
{
if (! $model->isUserstamping() || is_null(auth()->id())) {
if (! $model->isUserstamping() || is_null(Auth::id())) {
return;
}

$model->{$model->getUpdatedByColumn()} = auth()->id();
$model->{$model->getUpdatedByColumn()} = Auth::id();
}
}
9 changes: 5 additions & 4 deletions src/UserstampsScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Support\Facades\Auth;

class UserstampsScope implements Scope
{
Expand All @@ -29,22 +30,22 @@ public function apply(Builder $builder, Model $model)
public function extend(Builder $builder)
{
$builder->macro('updateWithUserstamps', function (Builder $builder, $values) {
if (! $builder->getModel()->isUserstamping() || is_null(auth()->id())) {
if (! $builder->getModel()->isUserstamping() || is_null(Auth::id())) {
return $builder->update($values);
}

$values[$builder->getModel()->getUpdatedByColumn()] = auth()->id();
$values[$builder->getModel()->getUpdatedByColumn()] = Auth::id();

return $builder->update($values);
});

$builder->macro('deleteWithUserstamps', function (Builder $builder) {
if (! $builder->getModel()->isUserstamping() || is_null(auth()->id())) {
if (! $builder->getModel()->isUserstamping() || is_null(Auth::id())) {
return $builder->delete();
}

$builder->update([
$builder->getModel()->getDeletedByColumn() => auth()->id(),
$builder->getModel()->getDeletedByColumn() => Auth::id(),
]);

return $builder->delete();
Expand Down

0 comments on commit e530ea5

Please sign in to comment.