Skip to content

Commit

Permalink
Removed duplication in the attach/detach permission code
Browse files Browse the repository at this point in the history
  • Loading branch information
santigarcor committed Feb 12, 2017
1 parent b4bf9c5 commit 7a5db26
Showing 1 changed file with 9 additions and 25 deletions.
34 changes: 9 additions & 25 deletions src/Laratrust/Traits/LaratrustUserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,7 @@ public function syncRoles($roles = [])
*/
public function attachPermission($permission)
{
if (is_object($permission)) {
$permission = $permission->getKey();
}

if (is_array($permission)) {
$permission = $permission['id'];
}

$this->permissions()->attach($permission);
$this->permissions()->attach($this->getIdFor($permission));
$this->flushCache();

return $this;
Expand All @@ -367,15 +359,7 @@ public function attachPermission($permission)
*/
public function detachPermission($permission)
{
if (is_object($permission)) {
$permission = $permission->getKey();
}

if (is_array($permission)) {
$permission = $permission['id'];
}

$this->permissions()->detach($permission);
$this->permissions()->detach($this->getIdFor($permission));
$this->flushCache();

return $this;
Expand Down Expand Up @@ -465,19 +449,19 @@ public function flushCache()
}

/**
* @param $role
* @param $object
* @return mixed
*/
private function getIdFor($role)
private function getIdFor($object)
{
if (is_object($role)) {
$role = $role->getKey();
if (is_object($object)) {
$object = $object->getKey();
}

if (is_array($role)) {
$role = $role['id'];
if (is_array($object)) {
$object = $object['id'];
}

return $role;
return $object;
}
}

0 comments on commit 7a5db26

Please sign in to comment.