<?php

namespace Dipper\Foundation\Debugger\Requests;

use Closure;
use Illuminate\Http\Request;

class RequestsMonitorMiddleware
{
    /**
     * @param \Illuminate\Http\Request $request
     * @param \Closure                 $next
     *
     * @return  mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $response = $next($request);

        $output = new Output($request, $response);

        $output->newRequest();
        $output->spaceLine();

        $output->header('REQUEST INFO');
        $output->endpoint();
        $output->version();
        $output->ip();
        $output->format();
        $output->spaceLine();

        $output->header('USER INFO');
        $output->userInfo();
        $output->spaceLine();

        $output->header('REQUEST DATA');
        $output->requestData();
        $output->spaceLine();

        $output->header('RESPONSE DATA');
        $output->responseData();
        $output->spaceLine();

        (new RequestsLogger())->releaseOutput($output);

        return $response;
    }
}
