Failed Conditions
Branch v3.x (aec8e0)
by Chad
01:59
created

AbstractCache::getDefaultTTL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Chadicus\Marvel\Api\Cache;
4
5
/**
6
 * Base class for all cache objects.
7
 */
8
abstract class AbstractCache implements CacheInterface
9
{
10
    /**
11
     * Default time to live in seconds.
12
     *
13
     * @var integer
14
     */
15
    private $defaultTimeToLive = CacheInterface::MAX_TTL;
16
17
    /**
18
     * Sets the default time to live in seconds.
19
     *
20
     * @param integer $defaultTimeToLive The time in seconds.
21
     *
22
     * @return void
23
     */
24
    final public function setDefaultTTL(int $defaultTimeToLive)
0 ignored issues
show
Coding Style introduced by
Unknown type hint "int" found for $defaultTimeToLive
Loading history...
25
    {
26
        $this->defaultTimeToLive = self::ensureTTL($defaultTimeToLive);
27
    }
28
29
    /**
30
     * Returns the default time to live in seconds.
31
     *
32
     * @return integer The time in seconds.
33
     */
34
    final public function getDefaultTTL()
35
    {
36
        return $this->defaultTimeToLive;
37
    }
38
39
    /**
40
     * Helper method to check TTL value.
41
     *
42
     * @param integer $ttl The time value to check in seconds.
43
     *
44
     * @return integer The valid $ttl value.
45
     *
46
     * @throws \InvalidArgumentException Thrown if $ttl is < 1 or > CacheInterface::MAX_TTL.
47
     */
48
    final protected static function ensureTTL(int $ttl)
0 ignored issues
show
Coding Style introduced by
Unknown type hint "int" found for $ttl
Loading history...
49
    {
50
        if ($ttl < 1 || $ttl > CacheInterface::MAX_TTL) {
51
            throw new \InvalidArgumentException('TTL value must be an integer >= 1 and <= ' . CacheInterface::MAX_TTL);
52
        }
53
54
        return $ttl;
55
    }
56
}
57