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

RotatableJWK::getJWK()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
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