<?php

namespace App\Http\Middleware;

use Closure;
use App\Exceptions\ApiHttpException;

class ServiceEndpoints
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        $apiKey = config('auth.service_api_key');

        if (empty($apiKey)) {
            throw new ApiHttpException(500, 'api_key_not_set');
        }

        if ($request->input('apiKey') !== $apiKey) {
            throw new ApiHttpException(403, 'forbidden');
        }

        return $next($request);
    }
}
