TimeUtils   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A callAt() 0 25 4
A setTime() 0 9 1
A unsetTime() 0 5 1
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\Tests;
13
14
abstract class TimeUtils
15
{
16
    public static function callAt($timestamp, \Closure $func, $forceSleep = null)
17
    {
18
        if (!function_exists('uopz_function')) {
19
            $forceSleep = true;
20
        }
21
22
        if ($forceSleep) {
23
            sleep(-time() + $timestamp);
24
25
            return $func();
26
        }
27
28
        self::setTime($timestamp);
29
30
        try {
31
            $result = $func();
32
        } catch (\Exception $e) {
33
            self::unsetTime();
34
            throw $e;
35
        }
36
37
        self::unsetTime();
38
39
        return $result;
40
    }
41
42
    private static function setTime($timestamp)
43
    {
44
        $handler = function () use ($timestamp) {
45
            return $timestamp;
46
        };
47
48
        uopz_function('time', $handler);
49
        uopz_function('DateTime', 'getTimestamp', $handler);
50
    }
51
52
    private static function unsetTime()
53
    {
54
        uopz_restore('time');
55
        uopz_restore('DateTime', 'getTimestamp');
56
    }
57
}
58