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

Profile::addDirective()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Spatie\Csp\Profiles;
4
5
use Illuminate\Http\Request;
6
use Spatie\Csp\Directive;
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
    public function addDirective(string $directive, string $value): self
17
    {
18
        $this->guardAgainstInvalidDirectives($directive);
19
20
        $this->directives[$directive][] = $value;
21
22
        return $this;
23
    }
24
25
    abstract public function registerDirectives();
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 applyTo(Response $response)
62
    {
63
        $this->registerDirectives();
64
65
        $headerName = $this->reportOnly
66
            ? 'Content-Security-Policy-Report-Only'
67
            : 'Content-Security-Policy';
68
69
        $response->headers->set($headerName, (string)$this);
70
    }
71
72
    protected function guardAgainstInvalidDirectives(string $directive)
73
    {
74
        if (!Directive::isValid($directive)) {
75
            throw InvalidDirective::notSupported($directive);
76
        }
77
    }
78
79
    public function __toString()
80
    {
81
        return collect($this->directives)
82
            ->map(function (array $values, string $directive) {
83
                $valueString = implode(' ', $values);
84
85
                return "{$directive} {$valueString}";
86
            })
87
            ->implode(';');
88
    }
89
}
90