ReplayProtection   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 38
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A accept() 0 13 3
A guardValidLifetime() 0 6 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