Completed
Push — master ( 511a60...b31476 )
by Freek
01:18
created

AddCspHeaders   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 12 2
A getProfile() 0 10 2
1
<?php
2
3
namespace Spatie\Csp;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Spatie\Csp\Exceptions\InvalidCspProfile;
8
use Spatie\Csp\Profiles\Profile;
9
10
class AddCspHeaders
11
{
12
    public function handle(Request $request, Closure $next)
13
    {
14
        $response = $next($request);
15
16
        $profile = $this->getProfile();
17
18
        if ($profile->shouldBeApplied($request, $response)) {
19
            $profile->applyTo($response);
20
        }
21
22
        return $response;
23
    }
24
25
    protected function getProfile(): Profile
26
    {
27
        $profile = app(Profile::class);
28
29
        if (!is_a($profile, Profile::class, true)) {
30
            throw InvalidCspProfile::create($profile);
31
        }
32
33
        return $profile;
34
    }
35
}
36