Completed
Push — master ( 870cb1...1f9632 )
by Milroy
07:07 queued 05:53
created

ContentNegotiation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 21
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 18 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