Failed Conditions
Pull Request — master (#126)
by Florent
02:54
created

RotatableJWK   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getJWK() 0 11 3
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Object;
13
14
use Assert\Assertion;
15
16
/**
17
 * Class RotatableJWK.
18
 */
19
final class RotatableJWK extends StorableJWK implements RotatableJWKInterface
20
{
21
    /**
22
     * @var int
23
     */
24
    protected $ttl;
25
26
    public function __construct($filename, array $parameters, $ttl)
27
    {
28
        Assertion::integer($ttl);
29
        Assertion::greaterThan($ttl, 0, 'The parameter TTL must be at least 0.');
30
        $this->ttl = $ttl;
31
        parent::__construct($filename, $parameters);
32
    }
33
34
    /**
35
     * @return \Jose\Object\JWKInterface
36
     */
37
    protected function getJWK()
38
    {
39
        if (file_exists($this->getFilename())) {
40
            $mtime = filemtime($this->getFilename());
41
            if ($mtime + $this->ttl <= time()) {
42
                unlink($this->getFilename());
43
            }
44
        }
45
46
        return parent::getJWK();
47
    }
48
}
49