Completed
Push — master ( 088c30...a2f48f )
by Freek
01:22
created

Profile   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 12
lcom 2
cbo 4

10 Methods

Rating   Name   Duplication   Size   Complexity  
A reportOnly() 0 6 1
A enforce() 0 6 1
A reportTo() 0 14 1
A shouldBeApplied() 0 4 1
A addDirective() 0 8 1
configure() 0 1 ?
A addNonceForDirective() 0 4 1
A applyTo() 0 14 3
A __toString() 0 10 1
A guardAgainstInvalidDirectives() 0 6 2
1
<?php
2
3
namespace Spatie\Csp\Profiles;
4
5
use Spatie\Csp\Directive;
6
use Illuminate\Http\Request;
7
use Spatie\Csp\Exceptions\InvalidDirective;
8
use Symfony\Component\HttpFoundation\Response;
9
10
abstract class Profile
11
{
12
    protected $directives = [];
13
14
    protected $reportOnly = false;
15
16
    abstract public function configure();
17
18
    public function addDirective(string $directive, string $value): self
19
    {
20
        $this->guardAgainstInvalidDirectives($directive);
21
22
        $this->directives[$directive][] = $value;
23
24
        return $this;
25
    }
26
27
    public function reportOnly(): self
28
    {
29
        $this->reportOnly = true;
30
31
        return $this;
32
    }
33
34
    public function enforce(): self
35
    {
36
        $this->reportOnly = false;
37
38
        return $this;
39
    }
40
41
    public function reportTo(string $uri): self
42
    {
43
        $this->directives['report-uri'] = [$uri];
44
45
        $reportToContents = json_encode([
46
            'url' => $uri,
47
            'group-name' => class_basename(static::class),
48
            'max-age' => 60 * 60 * 24 * 7 * 30,
49
        ]);
50
51
        $this->directives['report-to'] = [$reportToContents];
52
53
        return $this;
54
    }
55
56
    public function shouldBeApplied(Request $request, Response $response): bool
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58
        return config('csp.enabled');
59
    }
60
61
    public function addNonceForDirective(string $directive): self
62
    {
63
        return $this->addDirective($directive, "nonce-" . app('csp-nonce'));
64
    }
65
66
    public function applyTo(Response $response)
67
    {
68
        $this->configure();
69
70
        $headerName = $this->reportOnly
71
            ? 'Content-Security-Policy-Report-Only'
72
            : 'Content-Security-Policy';
73
74
        if ($response->headers->has($headerName)) {
75
            return;
76
        }
77
78
        $response->headers->set($headerName, (string) $this);
79
    }
80
81
    public function __toString()
82
    {
83
        return collect($this->directives)
84
            ->map(function (array $values, string $directive) {
85
                $valueString = implode(' ', $values);
86
87
                return "{$directive} {$valueString}";
88
            })
89
            ->implode(';');
90
    }
91
92
    protected function guardAgainstInvalidDirectives(string $directive)
93
    {
94
        if (! Directive::isValid($directive)) {
95
            throw InvalidDirective::notSupported($directive);
96
        }
97
    }
98
}
99