Passed
Push — master ( 48948a...a5130b )
by Christian
02:28
created

Assertions::assertValidTTL()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 9.6111
cc 5
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\PHPCacheAdapter;
6
7
use RemotelyLiving\PHPCacheAdapter\Exceptions;
8
9
final class Assertions
10
{
11
    /**
12
     * @param mixed $ttl
13
     *
14
     * @throws \RemotelyLiving\PHPCacheAdapter\Exceptions\InvalidArgument
15
     */
16
    public static function assertValidTTL($ttl): void
17
    {
18
        if (is_null($ttl) || is_int($ttl) || (is_object($ttl) && $ttl instanceof \DateTime)) {
19
            return;
20
        }
21
22
        throw Exceptions\InvalidArgument::invalidTTL();
23
    }
24
25
    /**
26
     * @param mixed $key
27
     *
28
     * @throws \RemotelyLiving\PHPCacheAdapter\Exceptions\InvalidArgument
29
     */
30
    public static function assertValidKey($key): void
31
    {
32
        if (!is_string($key) || preg_match('/\s/im', $key)) {
33
            throw Exceptions\InvalidArgument::invalidKey($key);
34
        }
35
    }
36
37
    /**
38
     * @param mixed $keys
39
     *
40
     * @throws \RemotelyLiving\PHPCacheAdapter\Exceptions\InvalidArgument
41
     */
42
    public static function assertValidKeys($keys): void
43
    {
44
        self::assertIterable($keys);
45
46
        foreach ($keys as $key) {
47
            self::assertValidKey($key);
48
        }
49
    }
50
51
    /**
52
     * @param mixed $iterable
53
     *
54
     * @throws \RemotelyLiving\PHPCacheAdapter\Exceptions\InvalidArgument
55
     */
56
    public static function assertIterable($iterable): void
57
    {
58
        if (!is_iterable($iterable)) {
59
            throw Exceptions\InvalidArgument::invalidIterable();
60
        }
61
    }
62
63
    /**
64
     * @throws \RemotelyLiving\PHPCacheAdapter\Exceptions\RuntimeError
65
     */
66
    public static function assertExtensionLoaded(string $extension): void
67
    {
68
        if (!extension_loaded($extension)) {
69
            throw Exceptions\RuntimeError::extensionNotLoaded($extension);
70
        }
71
    }
72
}
73