Skip to content

Commit b484979

Browse files
authored
Merge pull request #49 from nunodonato/main
Add model and user to all event constructors
2 parents b716e4e + 2b0982d commit b484979

7 files changed

+27
-12
lines changed

database/migrations/2023_11_17_002135_add_audited_by_column_to_approvals_table.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
return new class extends Migration {
7+
return new class extends Migration
8+
{
89
/**
910
* Run the migrations.
1011
*/

src/Concerns/MustBeApproved.php

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ protected static function insertApprovalRequest($model)
5858
if ($noNeedToProceed) {
5959
return false;
6060
}
61-
6261
}
6362

6463
/**

src/Events/ModelApproved.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
namespace Cjmellor\Approval\Events;
44

5+
use Illuminate\Contracts\Auth\Authenticatable;
6+
use Illuminate\Database\Eloquent\Model;
57
use Illuminate\Foundation\Events\Dispatchable;
68

79
class ModelApproved
810
{
911
use Dispatchable;
1012

11-
public function __construct()
12-
{
13+
public function __construct(
14+
public Model $approval,
15+
public Authenticatable|null $user,
16+
) {
1317
}
1418
}

src/Events/ModelRejected.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
namespace Cjmellor\Approval\Events;
44

5+
use Illuminate\Contracts\Auth\Authenticatable;
6+
use Illuminate\Database\Eloquent\Model;
57
use Illuminate\Foundation\Events\Dispatchable;
68

79
class ModelRejected
810
{
911
use Dispatchable;
1012

11-
public function __construct()
12-
{
13+
public function __construct(
14+
public Model $approval,
15+
public Authenticatable|null $user,
16+
) {
1317
}
1418
}

src/Events/ModelSetPending.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
namespace Cjmellor\Approval\Events;
44

5+
use Illuminate\Contracts\Auth\Authenticatable;
6+
use Illuminate\Database\Eloquent\Model;
57
use Illuminate\Foundation\Events\Dispatchable;
68

79
class ModelSetPending
810
{
911
use Dispatchable;
1012

11-
public function __construct()
12-
{
13+
public function __construct(
14+
public Model $approval,
15+
public Authenticatable|null $user,
16+
) {
1317
}
1418
}

src/Scopes/ApprovalStateScope.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ protected function addApprove(Builder $builder): void
127127
protected function updateApprovalState(Builder $builder, ApprovalStatus $state): int
128128
{
129129
match ($state) {
130-
ApprovalStatus::Approved => Event::dispatch(new ModelApproved()),
131-
ApprovalStatus::Pending => Event::dispatch(new ModelSetPending()),
132-
ApprovalStatus::Rejected => Event::dispatch(new ModelRejected()),
130+
ApprovalStatus::Approved => Event::dispatch(new ModelApproved($builder->getModel(), auth()->user())),
131+
ApprovalStatus::Pending => Event::dispatch(new ModelSetPending($builder->getModel(), auth()->user())),
132+
ApprovalStatus::Rejected => Event::dispatch(new ModelRejected($builder->getModel(), auth()->user())),
133133
};
134134

135135
$auditedData = ['state' => $state];

tests/Feature/Scopes/ApprovalStateScopeTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Cjmellor\Approval\Models\Approval;
88
use Cjmellor\Approval\Tests\Models\FakeModel;
99
use Illuminate\Support\Facades\Event;
10+
use Illuminate\Support\Facades\Schema;
1011

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

203204
test(description: 'The model approver is listed correctly', closure: function () {
204-
Schema::create('fake_users', callback: function (\Illuminate\Database\Schema\Blueprint $table) {
205+
Schema::create('fake_users', callback: function (Illuminate\Database\Schema\Blueprint $table) {
205206
$table->id();
206207
$table->string(column: 'name');
207208
$table->string(column: 'email')->unique();
@@ -211,7 +212,9 @@
211212
class FakeUser extends \Illuminate\Foundation\Auth\User
212213
{
213214
protected $guarded = [];
215+
214216
protected $table = 'fake_users';
217+
215218
public $timestamps = false;
216219
}
217220

0 commit comments

Comments
 (0)