ContentNegotiation   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 9
dl 0
loc 19
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sarala\Http\Middleware;
6
7
use Closure;
8
use Sarala\Exceptions\NotAcceptableMediaTypeHttpException;
9
use Sarala\Exceptions\UnsupportedMediaTypeHttpException;
10
use Sarala\Sarala;
11
12
class ContentNegotiation
13
{
14
    public function handle($request, Closure $next)
15
    {
16
        $supportedMediaTypes = resolve(Sarala::class)->getSupportedMediaTypes();
17
18
        if (! in_array($request->header('Content-Type'), $supportedMediaTypes)) {
19
            throw new UnsupportedMediaTypeHttpException();
20
        }
21
22
        if (! $request->accepts($supportedMediaTypes)) {
23
            throw new NotAcceptableMediaTypeHttpException();
24
        }
25
26
        $response = $next($request);
27
28
        $response->header('Content-Type', $request->header('Accept'));
29
30
        return $response;
31
    }
32
}
33