Browser::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
use Kenjis\Csp\Browser\AdapterInterface;
14
15
class Browser
16
{
17
    /**
18
     * @var \Kenjis\Csp\Browser\AdapterInterface
19
     */
20
    private $browserDetector;
21
22
    /**
23
     * List of browsers and versions which support CSP nonce-source
24
     *
25
     * @var array
26
     */
27
    private $supportedBrowserList = [
28
        // name => version
29
        'Firefox' => 31,    // https://www.mozilla.org/en-US/mobile/31.0/releasenotes/
30
        'Chrome'  => 37,    // At least Chrome 37 supports CSP nonce-source
31
    ];
32
33
    /**
34
     * @param \Kenjis\Csp\Browser\AdapterInterface $browserDetector
35
     */
36
    public function __construct(AdapterInterface $browserDetector)
37
    {
38
        $this->browserDetector = $browserDetector;
39
    }
40
41
    /**
42
     * Does browser support CSP nonce-source or not?
43
     *
44
     * @return boolean
45
     */
46
    public function supportNonceSource()
47
    {
48
        $name = $this->browserDetector->getName();
49
        $version = $this->browserDetector->getVersion();
50
51
        if (! isset($this->supportedBrowserList[$name])) {
52
            return false;
53
        }
54
55
        if ($version >= $this->supportedBrowserList[$name]) {
56
            return true;
57
        }
58
59
        return false;
60
    }
61
}
62