<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class ActivityLog
{
    const LOG_NAME = 'request';
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $causer = Auth::guard('api')->user();
        $requestData = $request->all();
        $description = $request->method();
        $url = $request->url();
        $properties = [
            'requestData' => $requestData,
            'requestUrl' => $url,
            'requestMethod' => $description,
            'visitors_ip' => $request->ip()
        ];

        activity(self::LOG_NAME)
            ->causedBy($causer)
            ->withProperties($properties)
            ->log($description);

        return $next($request);
    }
}
