<commit_before><?php  namespace Artesaos\Defender\Traits;

trait HasPermissionsTrait
{

    /**
     * Get the a permission using the permission name.
     *
     * @param string $permission
     * @param bool   $inherit
     *
     * @return bool|null
     */
    public function getPermission($permission, $inherit = true)
    {
        foreach ($this->permissions as $_permission) {
            if ($_permission->name === $permission) {
                if (is_null($_permission->pivot->expires) or $_permission->pivot->expires->isFuture()) {
                    return $_permission->pivot->value;
                }
            }
        }

        return $inherit ? null : false;
    }

    /**
     * Attach the given permission.
     *
     * @param array|Permission $permission
     * @param array            $options
     */
    public function attachPermission($permission, array $options = [])
    {
        return $this->permissions()->attach($permission, [
            'value' => array_get($options, 'value', true),
            'expires' => array_get($options, 'expires', null),
        ]);
    }

    /**
     * Detach the given permission from the model.
     *
     * @param $permission
     *
     * @return int
     */
    public function detachPermission($permission)
    {
        return $this->permissions()->detach($permission);
    }

    /**
     * Alias to the detachPermission method.
     *
     * @param $permission
     * @return int
     */
    public function revokePermission($permission)
    {
        return $this->detachPermission($permission);
    }

    /**
     * Sync the given permissions.
     *
     * @param array $permissions
     *
     * @return array
     */
    public function syncPermissions(array $permissions)
    {
        return $this->permissions()->sync($permissions);
    }

    /**
     * Revoke all user permissions.
     *
     * @return int
     */
    public function revokePermissions()
    {
        return $this->permissions()->detach();
    }

    /**
     * Revoke expired user permissions.
     *
     * @return int|null
     */
    public function revokeExpiredPermissions()
    {
        $expiredPermissions = $this->permissions()->wherePivot('expires', '<', Carbon::now())->get();

        if ($expiredPermissions->count() > 0) {
            return $this->permissions()->detach($expiredPermissions->modelKeys());
        }

        return;
    }

	/**
	 * Extend an existing temporary permission.
	 *
	 * @param $permission
	 * @param array $options
	 * @return bool|null
	 */
    public function extendPermission($permission, array $options)
    {
		foreach ($this->permissions as $_permission) {
			if ($_permission->name === $permission) {
				return $this->permissions()->updateExistingPivot($_permission->id,
					array_only($options, ['value', 'expires']
				));
			}
		}

		return null;
    }
}<commit_msg>PSR-2 Fix<commit_after><?php  namespace Artesaos\Defender\Traits;

trait HasPermissionsTrait
{
    /**
     * Get the a permission using the permission name.
     *
     * @param string $permission
     * @param bool   $inherit
     *
     * @return bool|null
     */
    public function getPermission($permission, $inherit = true)
    {
        foreach ($this->permissions as $_permission) {
            if ($_permission->name === $permission) {
                if (is_null($_permission->pivot->expires) or $_permission->pivot->expires->isFuture()) {
                    return $_permission->pivot->value;
                }
            }
        }

        return $inherit ? null : false;
    }

    /**
     * Attach the given permission.
     *
     * @param array|Permission $permission
     * @param array            $options
     */
    public function attachPermission($permission, array $options = [])
    {
        return $this->permissions()->attach($permission, [
            'value' => array_get($options, 'value', true),
            'expires' => array_get($options, 'expires', null),
        ]);
    }

    /**
     * Detach the given permission from the model.
     *
     * @param $permission
     *
     * @return int
     */
    public function detachPermission($permission)
    {
        return $this->permissions()->detach($permission);
    }

    /**
     * Alias to the detachPermission method.
     *
     * @param $permission
     * @return int
     */
    public function revokePermission($permission)
    {
        return $this->detachPermission($permission);
    }

    /**
     * Sync the given permissions.
     *
     * @param array $permissions
     *
     * @return array
     */
    public function syncPermissions(array $permissions)
    {
        return $this->permissions()->sync($permissions);
    }

    /**
     * Revoke all user permissions.
     *
     * @return int
     */
    public function revokePermissions()
    {
        return $this->permissions()->detach();
    }

    /**
     * Revoke expired user permissions.
     *
     * @return int|null
     */
    public function revokeExpiredPermissions()
    {
        $expiredPermissions = $this->permissions()->wherePivot('expires', '<', Carbon::now())->get();

        if ($expiredPermissions->count() > 0) {
            return $this->permissions()->detach($expiredPermissions->modelKeys());
        }

        return;
    }

    /**
     * Extend an existing temporary permission.
     *
     * @param $permission
     * @param array $options
     * @return bool|null
     */
    public function extendPermission($permission, array $options)
    {
        foreach ($this->permissions as $_permission) {
            if ($_permission->name === $permission) {
                return $this->permissions()->updateExistingPivot($_permission->id,
                    array_only($options, ['value', 'expires']
                ));
            }
        }

        return null;
    }
}
{'fraction_non_alphanumeric': 0.13475661827497865, 'fraction_numerical': 0.0005123825789923143, 'mean_word_length': 2.040498442367601, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7238951', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1593, 'n_words': 347}