<?php

namespace App\Http\Middleware;

use App\Models\Setting;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class Timezone {
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle( Request $request, Closure $next ) {
        try {
            $setting = Setting::first();
            config( ['app.timezone' => $setting->timezone] );
            date_default_timezone_set( $setting->timezone );
        } catch ( \Exception $e ) {
            if ( app()->isLocal() ) {
                // Log::error( $e );
            }
        }

        return $next( $request );
    }
}
