<?php

namespace App\Http\Middleware;

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

class PusherCredential {
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle( Request $request, Closure $next ) {
        try {
            $pusher = PusherCredentail::first();
            $pusherConfig = [
                'driver'  => 'pusher',
                'key'     => $pusher->app_key,
                'secret'  => $pusher->app_secret,
                'app_id'  => $pusher->app_id,
                'options' => [
                    'cluster'   => $pusher->app_cluster,
                    'useTLS'    => true,
                    'encrypted' => true,
                ],
            ];
        } catch ( \Exception $e ) {
            if ( app()->isLocal() ) {
                // Log::error( $e );
            }
        }
        // config(['broadcasting.connections.pusher' => $pusherConfig]);
        // dd(config('broadcasting.connections.pusher'));
        return $next( $request );
    }
}
