Skip to content

Commit

Permalink
fix: add returns to make methods chainable
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Frey <[email protected]>
  • Loading branch information
lukas-frey committed May 9, 2024
1 parent e1e0fa5 commit b3f50b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/Concerns/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,31 @@

trait HasPermissions
{
public function addPermission(Permission $permission, ?Model $target = null): void
public function addPermission(Permission $permission, ?Model $target = null): static
{
$query = $this->permissions();

if ($this->modifyPermissionsQuery($query, $permission, $target)->exists()) {
return;
return $this;
}

$query->create([
'permission' => $permission,
'targettable_type' => $target?->getMorphClass(),
'targettable_id' => $target?->getKey(),
]);

return $this;
}

public function removePermission(Permission $permission, ?Model $target = null): void
public function removePermission(Permission $permission, ?Model $target = null): static
{
/** @var Permissionable $record */
if ($record = $this->modifyPermissionsQuery($this->permissions(), $permission, $target)->first()) {
$record->delete();
}

return $this;
}

public function hasPermission(Permission $permission, ?Model $target = null): bool
Expand Down
10 changes: 7 additions & 3 deletions src/Concerns/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,32 @@

trait HasRoles
{
public function addRole(string | Role $role, ?Model $target = null): void
public function addRole(string | Role $role, ?Model $target = null): static
{
$role = $role instanceof Role ? $role::class : $role;

if ($this->modifyRolesQuery($this->roles(), $role, $target)->exists()) {
return;
return $this;
}

$this->roles()->create([
'role' => $role,
'targettable_type' => $target?->getMorphClass(),
'targettable_id' => $target?->getKey(),
]);

return $this;
}

public function removeRole(string | Role $role, ?Model $target = null): void
public function removeRole(string | Role $role, ?Model $target = null): static
{
$role = $role instanceof Role ? $role::class : $role;
/** @var Roleable $record */
if ($record = $this->modifyRolesQuery($this->roles(), $role, $target)->first()) {
$record->delete();
}

return $this;
}

public function hasRole(string | Role $role, ?Model $target = null): bool
Expand Down

0 comments on commit b3f50b7

Please sign in to comment.