ReplayProtection::accept()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
1
<?php
2
3
namespace Rezzza\SecurityBundle\Security\Firewall;
4
5
class ReplayProtection
6
{
7
    private $enabled;
8
9
    private $lifetime;
10
11
    public function __construct($enabled, $lifetime)
12
    {
13
        $this->guardValidLifetime($lifetime);
14
        $this->enabled = (bool) $enabled;
15
        $this->lifetime = (int) $lifetime;
16
    }
17
18
    /**
19
     * @param integer $signatureTime Should be a unix timestamp
20
     * @param integer $referenceTime Should be a unix timestamp
21
     */
22
    public function accept($signatureTime, $referenceTime)
23
    {
24
        if (!$this->enabled) {
25
            return true;
26
        }
27
28
        // We validate only now the signatureTime because before we are not sure we need it.
29
        if (!is_numeric($signatureTime)) {
30
            throw new ExpiredSignatureException(sprintf('Signature TTL "%s" is not valid', $signatureTime));
31
        }
32
33
        return $this->lifetime >= abs($referenceTime - $signatureTime);
34
    }
35
36
    private function guardValidLifetime($lifetime)
37
    {
38
        if (!is_numeric($lifetime)) {
39
            throw new \LogicException('ReplayProtection lifetime should be a numeric value');
40
        }
41
    }
42
}
43