Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add model and user to all event constructors #49

Merged
merged 7 commits into from
Feb 17, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*/
Expand Down
1 change: 0 additions & 1 deletion src/Concerns/MustBeApproved.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ protected static function insertApprovalRequest($model)
if ($noNeedToProceed) {
return false;
}

}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Events/ModelApproved.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Cjmellor\Approval\Events;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;

class ModelApproved
{
use Dispatchable;

public function __construct()
{
public function __construct(
public Model $approval,
public Authenticatable|null $user,
) {
}
}
8 changes: 6 additions & 2 deletions src/Events/ModelRejected.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Cjmellor\Approval\Events;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;

class ModelRejected
{
use Dispatchable;

public function __construct()
{
public function __construct(
public Model $approval,
public Authenticatable|null $user,
) {
}
}
8 changes: 6 additions & 2 deletions src/Events/ModelSetPending.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Cjmellor\Approval\Events;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;

class ModelSetPending
{
use Dispatchable;

public function __construct()
{
public function __construct(
public Model $approval,
public Authenticatable|null $user,
) {
}
}
6 changes: 3 additions & 3 deletions src/Scopes/ApprovalStateScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ protected function addApprove(Builder $builder): void
protected function updateApprovalState(Builder $builder, ApprovalStatus $state): int
{
match ($state) {
ApprovalStatus::Approved => Event::dispatch(new ModelApproved()),
ApprovalStatus::Pending => Event::dispatch(new ModelSetPending()),
ApprovalStatus::Rejected => Event::dispatch(new ModelRejected()),
ApprovalStatus::Approved => Event::dispatch(new ModelApproved($builder->getModel(), auth()->user())),
ApprovalStatus::Pending => Event::dispatch(new ModelSetPending($builder->getModel(), auth()->user())),
ApprovalStatus::Rejected => Event::dispatch(new ModelRejected($builder->getModel(), auth()->user())),
};

$auditedData = ['state' => $state];
Expand Down
5 changes: 4 additions & 1 deletion tests/Feature/Scopes/ApprovalStateScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cjmellor\Approval\Models\Approval;
use Cjmellor\Approval\Tests\Models\FakeModel;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;

test('Check if an Approval Model is approved', closure: function (): void {
$this->approvalData = [
Expand Down Expand Up @@ -201,7 +202,7 @@
});

test(description: 'The model approver is listed correctly', closure: function () {
Schema::create('fake_users', callback: function (\Illuminate\Database\Schema\Blueprint $table) {
Schema::create('fake_users', callback: function (Illuminate\Database\Schema\Blueprint $table) {
$table->id();
$table->string(column: 'name');
$table->string(column: 'email')->unique();
Expand All @@ -211,7 +212,9 @@
class FakeUser extends \Illuminate\Foundation\Auth\User
{
protected $guarded = [];

protected $table = 'fake_users';

public $timestamps = false;
}

Expand Down
Loading