CspStaticProxy::setReportOnly()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * CSP
4
 *
5
 * @author     Kenji Suzuki <https://github.com/kenjis>
6
 * @license    MIT License
7
 * @copyright  2014 Kenji Suzuki
8
 * @link       https://github.com/kenjis/php-csp-nonce-source
9
 */
10
11
namespace Kenjis\Csp;
12
13
class CspStaticProxy
14
{
15
    public static $browserDetector = 'Woothee';
16
17
    /**
18
     * @var \Kenjis\Csp\Csp
19
     */
20
    protected static $csp;
21
22
    protected static function createCsp()
23
    {
24
        if (static::$csp !== null) {
25
            return;
26
        }
27
28
        $className = __NAMESPACE__ . '\\Browser\\' . static::$browserDetector;
29
        $browserDetector = new $className($_SERVER['HTTP_USER_AGENT']);
30
        $browser = new Browser($browserDetector);
31
        $nonce = new Nonce($browser);
32
        static::$csp = new Csp($nonce);
33
        static::$csp->setNonceSource();
34
        static::$csp->addPolicy('report-uri', '/csp-report.php');
35
    }
36
37
    public static function setReportOnly()
38
    {
39
        static::createCsp();
40
        static::$csp->setReportOnly();
41
    }
42
43
    public static function sendHeader()
44
    {
45
        static::createCsp();
46
        static::$csp->sendHeader();
47
    }
48
49
    /**
50
     * @param string $directive
51
     * @param string $value
52
     */
53
    public static function addPolicy($directive, $value)
54
    {
55
        static::createCsp();
56
        static::$csp->addPolicy($directive, $value);
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public static function getNonce()
63
    {
64
        static::createCsp();
65
        return static::$csp->getNonce();
66
    }
67
68
    public static function resetCsp()
69
    {
70
        static::$csp = null;
71
    }
72
}
73