Completed
Push — master ( 31d53b...10a888 )
by Matthew
75:43 queued 71:47
created

Util::getMicrotimeStr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 1
eloc 4
c 2
b 2
f 0
nc 1
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Dtc\QueueBundle\Util;
4
5
class Util
6
{
7
    /**
8
     * Copies the members of obj1 that have public getters to obj2 if there exists a public setter in obj2 of the same suffix, it will also copy public variables.
9
     *
10
     * @param object $obj1
11
     * @param object $obj2
12
     */
13 38
    public static function copy($obj1, $obj2)
14
    {
15 38
        if (!is_object($obj1)) {
16
            throw new \InvalidArgumentException('$obj1 must be an object, not '.gettype($obj1));
17
        }
18 38
        if (!is_object($obj2)) {
19
            throw new \InvalidArgumentException('$obj2 must be an object, not '.gettype($obj2));
20
        }
21 38
        $reflection1 = new \ReflectionObject($obj1);
22 38
        $reflection2 = new \ReflectionObject($obj2);
23 38
        self::copyMethods([$obj1, $obj2], $reflection1, $reflection2);
24 38
        self::copyProperties([$obj1, $obj2], $reflection1, $reflection2);
25 38
    }
26
27
    /**
28
     * @param array[object, object] $payload
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[object, object] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
29
     */
30 38
    private static function copyProperties(array $payload, \ReflectionObject $reflection1, \ReflectionObject $reflection2)
31
    {
32 38
        list($obj1, $obj2) = $payload;
33 38
        $publicVars = $reflection1->getProperties(\ReflectionProperty::IS_PUBLIC);
34 38
        foreach ($publicVars as $property) {
35
            $propertyName = $property->getName();
36
            if ($reflection2->hasProperty($propertyName) && $reflection2->getProperty($propertyName)->isPublic()) {
37
                $obj2->$propertyName = $obj1->$propertyName;
38
            }
39
        }
40 38
    }
41
42
    /**
43
     * @param array[object, object] $payload
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[object, object] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
44
     */
45 38
    private static function copyMethods(array $payload, \ReflectionObject $reflection1, \ReflectionObject $reflection2)
46
    {
47 38
        list($obj1, $obj2) = $payload;
48 38
        $methods = $reflection1->getMethods(\ReflectionMethod::IS_PUBLIC);
49 38
        foreach ($methods as $method) {
50 38
            $methodName = $method->name;
51 38
            if (0 === strpos($methodName, 'get')) {
52 38
                $getMethod = $methodName;
53 38
                $setMethod = $methodName;
54 38
                $setMethod[0] = 's';
55 38
                self::copyMethod([$obj1, $obj2], $getMethod, $setMethod, $reflection2);
56
            }
57
        }
58 38
    }
59
60
    /**
61
     * @param array[object, object] $payload
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[object, object] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
62
     * @param string                $getMethod
63
     * @param string                $setMethod
64
     */
65 38
    private static function copyMethod(array $payload, $getMethod, $setMethod, \ReflectionObject $reflection2)
66
    {
67 38
        list($obj1, $obj2) = $payload;
68 38
        if ($reflection2->hasMethod($setMethod)) {
69 38
            $value = $obj1->$getMethod();
70 38
            if (null !== $value) {
71 38
                $obj2->$setMethod($value);
72
            }
73
        }
74 38
    }
75
76
    /**
77
     * @param string $varName
78
     * @param int    $var
79
     * @param int    $pow
80
     */
81 4
    public static function validateIntNull($varName, $var, $pow)
82
    {
83 4
        if (null === $var) {
0 ignored issues
show
introduced by
The condition null === $var is always false.
Loading history...
84 4
            return null;
85
        }
86 4
        if (!ctype_digit(strval($var))) {
87 1
            throw new \InvalidArgumentException("$varName must be an integer");
88
        }
89
90 4
        if (strval(intval($var)) !== strval($var) || $var < 0 || $var >= pow(2, $pow)) {
91 1
            throw new \InvalidArgumentException("$varName must be an base 10 integer within 2^$pow");
92
        }
93
94 4
        return intval($var);
95
    }
96
97 130
    public static function getMicrotimeStr()
98
    {
99 130
        $parts = explode(' ', microtime());
100 130
        $pos1 = strpos($parts[0], '.');
101
102 130
        $timeU = $parts[1].'.'.substr($parts[0], $pos1 + 1, 6);
103
104 130
        return $timeU;
105
    }
106
107 8
    public static function getMicrotimeFloatDateTime($microtime)
108
    {
109 8
        if (!is_float($microtime)) {
110
            throw new \RuntimeException("Could not create date time expected-float microtime: $microtime");
111
        }
112 8
        $result = \DateTime::createFromFormat('U.u', number_format($microtime, 6, '.', ''));
113 8
        if (!$result) {
114
            throw new \RuntimeException("Could not create date time from float microtime: $microtime");
115
        }
116 8
        $result->setTimezone(new \DateTimeZone(date_default_timezone_get()));
117
118 8
        return $result;
119
    }
120
121
    /**
122
     * @throws \RuntimeException
123
     *
124
     * @return \DateTime
125
     */
126 129
    public static function getMicrotimeDateTime()
127
    {
128 129
        $result = \DateTime::createFromFormat('U.u', $microtime = self::getMicrotimeStr());
129 129
        if (!$result) {
130
            throw new \RuntimeException("Could not create date time from $microtime");
131
        }
132 129
        $result->setTimezone(new \DateTimeZone(date_default_timezone_get()));
133
134 129
        return $result;
135
    }
136
137 25
    public static function getMicrotimeInteger()
138
    {
139 25
        return self::getMicrotimeIntegerFormat(self::getMicrotimeDateTime());
140
    }
141
142 48
    public static function getMicrotimeIntegerFormat(\DateTime $dateTime)
143
    {
144 48
        $dateTimeUs = $dateTime->format('Uu');
145 48
        $dateTimeUs = str_pad($dateTimeUs, 18, '0', STR_PAD_RIGHT);
146
147 48
        return $dateTimeUs;
148
    }
149
150 34
    public static function getDateTimeFromDecimalFormat($decimal)
151
    {
152 34
        $len = strlen((string) time());
153 34
        $timePart = substr($decimal, 0, $len);
154 34
        $decimalPart = substr($decimal, $len, 6);
155
156 34
        $result = \DateTime::createFromFormat('U.u', "${timePart}.${decimalPart}");
157 34
        if (!$result) {
158
            throw new \RuntimeException("Could not create date time from ${timePart}.${decimalPart}");
159
        }
160 34
        $result->setTimezone(new \DateTimeZone(date_default_timezone_get()));
161
162 34
        return $result;
163
    }
164
}
165