Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
CheckBrand
n/a
0 / 0
n/a
0 / 0
3
n/a
0 / 0
 handle
n/a
0 / 0
n/a
0 / 0
3
1<?php
2
3namespace App\Http\Middleware;
4
5use App\Lib\Helper;
6use App\Models\Service;
7use Closure;
8use Illuminate\Database\Eloquent\ModelNotFoundException;
9use Illuminate\Http\Request;
10
11/**
12 * @codeCoverageIgnore
13 */
14class CheckBrand
15{
16    /**
17     * Handle an incoming request.
18     *
19     * @param  \Illuminate\Http\Request $request
20     * @param  \Closure                 $next
21     * @return mixed
22     */
23    public function handle(Request $request, Closure $next)
24    {
25        $apiToken = Helper::getBearerTokenFromRequest($request) ?? $request->api_token ?? '';
26
27        if ($apiToken === null) {
28            abort(401, 'Le token d\'authentification est incorrect ' . \json_encode($request->all()));
29        }
30
31        try {
32            $brand = Service::getFromBearerToken($apiToken)->brand;
33            $request->request->add(['brand_id' => $brand->id]);
34        } catch (ModelNotFoundException $e) {
35            abort(401, 'Le token d\'authentification est incorrect ' . $e->getMessage());
36        }
37
38        return $next($request);
39    }
40}