QueueUtils::normalizeEta()   A
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.0777
c 0
b 0
f 0
cc 6
nc 7
nop 1
crap 6
1
<?php
2
3
/*
4
 * This file is part of the Phive Queue package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phive\Queue;
13
14
abstract class QueueUtils
15
{
16
    /**
17
     * @param mixed $eta
18
     *
19
     * @return int The Unix timestamp.
20
     *
21
     * @throws \InvalidArgumentException
22
     */
23 145
    public static function normalizeEta($eta)
24
    {
25 145
        if (null === $eta) {
26 108
            return time();
27
        }
28 44
        if (is_string($eta)) {
29 16
            $eta = date_create($eta);
30 16
        }
31 44
        if ($eta instanceof \DateTime || $eta instanceof \DateTimeInterface) {
32 17
            return $eta->getTimestamp();
33
        }
34 27
        if (is_int($eta)) {
35 24
            return $eta;
36
        }
37
38 3
        throw new \InvalidArgumentException('The eta parameter is not valid.');
39
    }
40
41
    /**
42
     * @param mixed $eta
43
     *
44
     * @return int
45
     */
46 35
    public static function calculateDelay($eta)
47
    {
48 35
        if (null === $eta) {
49 24
            return 0;
50
        }
51
52 13
        $delay = -time() + self::normalizeEta($eta);
53
54 13
        return ($delay < 0) ? 0 : $delay;
55
    }
56
}
57