OperationLog.php 3.72 KB
<?php

namespace App\Helpers;

use App\Models\SystemOperateLog;
use App\Models\User;
use Auth;
use Illuminate\Database\Eloquent\Model;
use Str;

class OperationLog
{
    protected string $guard;

    protected string $action = '';

    protected string $content = '';

    protected Model $subject;

    protected int $causer;

    protected array $append = [];


    private function __construct(string $guard)
    {
        $this->guard  = $guard;
        $this->causer = request()?->user()?->getKey() ?? 0;
    }

    /**
     * @return self
     */
    public static function admin(): self
    {
        return new self('Admin');
    }

    /**
     * @return self
     */
    public static function manage(): self
    {
        return new self('Manage');
    }

    /**
     * @return self
     */
    public static function guard(): self
    {
        return new self(Str::ucfirst(Auth::getDefaultDriver()));
    }

    /**
     * @return $this
     */
    public function createAction(): OperationLog
    {
        $this->action = '新增操作';
        return $this;
    }

    /**
     * @return $this
     */
    public function updateAction(): OperationLog
    {
        $this->action = '内容调整';
        return $this;
    }

    /**
     * @return $this
     */
    public function statusAction(): OperationLog
    {
        $this->action = '状态变更';
        return $this;
    }

    /**
     * @return $this
     */
    public function deleteAction(): OperationLog
    {
        $this->action = '删除操作';
        return $this;
    }

    /**
     * @param string $action
     * @return $this
     */
    public function action(string $action): OperationLog
    {
        $this->action = $action;
        return $this;
    }

    /**
     * @param \Illuminate\Database\Eloquent\Model $subject
     * @return $this
     */
    public function subject(Model $subject): OperationLog
    {
        $this->subject = $subject;
        return $this;
    }

    /**
     * @param int|\App\Models\User $causer
     * @return $this
     */
    public function causer(User|int $causer): OperationLog
    {
        $this->causer = $causer instanceof User ? $causer->getKey() : (int)$causer;
        return $this;
    }

    /**
     * @param array $append
     * @return $this
     */
    public function append(array $append): OperationLog
    {
        $this->append = $append;
        return $this;
    }

    /**
     * @param string $format
     * @param mixed  ...$values
     * @return SystemOperateLog
     */
    public function content(string $format, mixed ...$values): SystemOperateLog
    {
        $this->content = sprintf($format, ...$values);
        return $this->save();
    }

    /**
     * @return SystemOperateLog
     */
    private function save(): SystemOperateLog
    {
        if (!in_array($this->guard, ['Admin', 'Manage'], true)) {
            return new SystemOperateLog([
                    'guard'        => $this->guard,
                    'user_id'      => $this->causer,
                    'action'       => $this->action,
                    'content'      => $this->content,
                    'subject_type' => $this->subject->getMorphClass(),
                    'subject_id'   => $this->subject->getKey(),
                ] + $this->append);
        }

        return SystemOperateLog::query()->create([
                'guard'        => $this->guard,
                'user_id'      => $this->causer,
                'path'         => explode(',', request()?->header('Route', '')),
                'action'       => $this->action,
                'content'      => $this->content,
                'subject_type' => $this->subject->getMorphClass(),
                'subject_id'   => $this->subject->getKey(),
            ] + $this->append);
    }

}