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

Allow models to require approval only when needed #10

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Concerns/MayBeApproved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Cjmellor\Approval\Concerns;

use Cjmellor\Approval\Enums\ApprovalStatus;
use Cjmellor\Approval\Models\Approval;

trait MayBeApproved
{
use MustBeApproved;
protected static bool $requiresApproval = false;

/**
* Check if the approval can be bypassed, based on static $requiresApproval flag;
*/
public function isApprovalBypassed(): bool
{
return !self::$requiresApproval;
}

/**
* Sets the $requiresApproval flag (defaults to true)
*/
public static function requireApproval($requires = true)
{
self::$requiresApproval = $requires;
}

/**
* Check if the Approval model been created already exists with a 'pending' state
*/
protected static function approvalModelExists($model): bool
{
print "testing " . get_class($model) . " $model->id ";
print_r($model->getDirty());
return Approval::where([
['state', '=', ApprovalStatus::Pending],
['new_data', '=', json_encode($model->getDirty())],
['original_data', '=', json_encode($model->getOriginalMatchingChanges())],
])->exists();
return Approval::where('state', ApprovalStatus::Pending)
->where('approvalable_id', $model->id)
->where('approvalable_type', get_class($model))
->whereJsonContains('new_data', $model->getDirty())
->exists();
}
}
3 changes: 2 additions & 1 deletion src/Concerns/MustBeApproved.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function getOriginalMatchingChanges(): array
}

/**
* Check is the approval can be bypassed.
* Check if the approval can be bypassed.
*/
public function isApprovalBypassed(): bool
{
Expand Down Expand Up @@ -101,4 +101,5 @@ public function withoutApproval(): static

return $this;
}

}
61 changes: 48 additions & 13 deletions src/Models/Approval.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,56 @@

class Approval extends Model
{
protected $guarded = [];
protected $guarded = [];

protected $casts = [
'new_data' => AsArrayObject::class,
'original_data' => AsArrayObject::class,
'state' => ApprovalStatus::class,
];
protected $casts = [
'new_data' => AsArrayObject::class,
'original_data' => AsArrayObject::class,
'state' => ApprovalStatus::class,
];

public static function booted()
{
static::addGlobalScope(new ApprovalStateScope());
}
public static function booted()
{
static::addGlobalScope(new ApprovalStateScope());
}

public function approvalable(): MorphTo
{
return $this->morphTo();
}

public function approvalable(): MorphTo
{
return $this->morphTo();
/**
* Mark the approval as Rejected.
*/
public function reject()
{
$this->update(['state' => ApprovalStatus::Rejected]);
}

/**
* Commit the change to the DB and mark the approval as Approved.
*/
public function commit()
{
if ($this->state != ApprovalStatus::Pending) return false;
if ($this->approvalable_id) {
$model = $this->approvalable_type::find($this->approvalable_id)->withoutApproval()->update($this->new_data->toArray());
$this->update(['state' => ApprovalStatus::Approved]);
return $model;
} else {
$model = new $this->approvalable_type;
$model->fill($this->new_data->toArray());
$model->save();
$this->update(['state' => ApprovalStatus::Approved]);
return $model;
}
}

/**
* Purge database of completed approvals older than $days.
*/
public static function purge(int $days = -1)
{
self::where('created_at', '<', now()->subDays($days))->where('state', '!=', ApprovalStatus::Pending)->forceDelete();
}
}