SecurityHeaderControllerExtension   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 52
ccs 29
cts 29
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B onAfterInit() 0 21 6
B browserHasWorkingCSPImplementation() 0 25 5
1
<?php
2
3
namespace Guttmann\SilverStripe;
4
5
use Config;
6
use Extension;
7
8
class SecurityHeaderControllerExtension extends Extension
9
{
10
11 2
    public function onAfterInit()
12
    {
13 2
        $response = $this->owner->getResponse();
14
15 2
        $headersToSend = (array) Config::inst()->get('Guttmann\SilverStripe\SecurityHeaderControllerExtension', 'headers');
16 2
        $xHeaderMap = (array) Config::inst()->get('Guttmann\SilverStripe\SecurityHeaderControllerExtension', 'x_headers_map');
17
18 2
        foreach ($headersToSend as $header => $value) {
19 2
            if ($header === 'Content-Security-Policy' && !$this->browserHasWorkingCSPImplementation()) {
20 1
                continue;
21
            }
22
23 2
            $response->addHeader($header, $value);
24
25 2
            if (isset($xHeaderMap[$header])) {
26 2
                foreach ($xHeaderMap[$header] as $xHeader) {
27 2
                    $response->addHeader($xHeader, $value);
28 2
                }
29 2
            }
30 2
        }
31 2
    }
32
33 2
    private function browserHasWorkingCSPImplementation()
34
    {
35 2
        $agent = strtolower(
36 2
            $this->owner->getRequest()->getHeader('User-Agent')
37 2
        );
38
39 2
        if (strpos($agent, 'safari') === false) {
40 1
            return true;
41
        }
42
43 1
        $split = explode('version/', $agent);
44
45 1
        if (!isset($split[1])) {
46 1
            return true;
47
        }
48
49 1
        $version = trim($split[1]);
50 1
        $versions = explode('.', $version);
51
52 1
        if (isset($versions[0]) && $versions[0] <= 5) {
53 1
            return false;
54
        }
55
56 1
        return true;
57
    }
58
59
}
60