Skip to content

Commit 36651dd

Browse files
authored
Refactor microseconds converstion to helper and add tests (#43)
2 parents 707a02b + cf59b05 commit 36651dd

File tree

5 files changed

+83
-13
lines changed

5 files changed

+83
-13
lines changed

src/Analytics.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,16 @@ public function setUserId(string $id)
7171

7272
public function setTimestampMicros(int|float $microOrUnix)
7373
{
74-
$secondInMicro = 1_000_000;
75-
$offsetLimit = (strtotime('-3 days') + 90) * $secondInMicro;
74+
$min = Helper\ConvertHelper::timeAsMicro(strtotime('-3 days') + 10);
75+
$max = Helper\ConvertHelper::timeAsMicro(time() + 3);
7676

77-
$formattedTime = floor($microOrUnix * $secondInMicro);
78-
if ($formattedTime < $offsetLimit) {
77+
$time = Helper\ConvertHelper::timeAsMicro($microOrUnix);
78+
79+
if ($time < $min || $time > $max) {
7980
throw Ga4Exception::throwMicrotimeExpired();
8081
}
8182

82-
$this->timestamp_micros = intval($formattedTime);
83+
$this->timestamp_micros = $time;
8384
return $this;
8485
}
8586

src/Exception/Ga4Exception.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public static function throwMissingApiSecret()
4242
return new static("Timestamp must be numeric", static::REQUEST_MISSING_API_SECRET);
4343
}
4444

45+
public static function throwMicrotimeInvalid($inp)
46+
{
47+
return new static("Timestamp $inp is not valid", static::MICROTIME_INVALID_FORMAT);
48+
}
49+
4550
public static function throwMicrotimeExpired()
4651
{
4752
return new static("Timestamp is too old, max 3 days", static::MICROTIME_EXPIRED);

src/Helper/ConvertHelper.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,32 @@
33
namespace AlexWestergaard\PhpGa4\Helper;
44

55
use AlexWestergaard\PhpGa4\Facade\Type\EventType;
6+
use AlexWestergaard\PhpGa4\Exception\Ga4Exception;
67

78
class ConvertHelper
89
{
10+
/**
11+
* Converts unix or microtime to microseconds; 1 second = 1,000,000 microseconds
12+
*
13+
* @param int|float $unixOrMicro time() or microtime(true)
14+
*
15+
* @return int
16+
* @throws Ga4Exception
17+
*/
18+
public static function timeAsMicro(int|float $unixOrMicro)
19+
{
20+
$secondAsMicro = 1_000_000;
21+
22+
$input = strlen(intval($unixOrMicro));
23+
$current = strlen(time()) + 1;
24+
25+
if ($input > $current) {
26+
throw Ga4Exception::throwMicrotimeInvalid($unixOrMicro);
27+
}
28+
29+
return intval($secondAsMicro * $unixOrMicro);
30+
}
31+
932
/**
1033
* @param string $input
1134
* @return string snake_case

test/Unit/AnalyticsTest.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use AlexWestergaard\PhpGa4\UserProperty;
66
use AlexWestergaard\PhpGa4\Facade;
7-
use AlexWestergaard\PhpGa4\Exception\Ga4Exception;
87
use AlexWestergaard\PhpGa4\Event;
98
use AlexWestergaard\PhpGa4\Analytics;
109
use AlexWestergaard\PhpGa4Test\TestCase;
@@ -61,7 +60,7 @@ public function test_converts_to_full_microtime_stamp()
6160

6261
public function test_throws_if_microtime_older_than_three_days()
6362
{
64-
$this->expectException(Ga4Exception::class);
63+
$this->expectException(Facade\Type\Ga4ExceptionType::class);
6564
$this->expectExceptionCode(Facade\Type\Ga4ExceptionType::MICROTIME_EXPIRED);
6665

6766
$this->analytics->setTimestampMicros(strtotime('-1 week'));
@@ -106,16 +105,16 @@ public function test_exports_events_to_array()
106105

107106
public function test_throws_missing_measurement_id()
108107
{
109-
$this->expectException(Ga4Exception::class);
110-
$this->expectExceptionCode(Ga4Exception::REQUEST_MISSING_MEASUREMENT_ID);
108+
$this->expectException(Facade\Type\Ga4ExceptionType::class);
109+
$this->expectExceptionCode(Facade\Type\Ga4ExceptionType::REQUEST_MISSING_MEASUREMENT_ID);
111110

112111
Analytics::new('', $this->prefill['api_secret'], true)->post();
113112
}
114113

115114
public function test_throws_missing_apisecret()
116115
{
117-
$this->expectException(Ga4Exception::class);
118-
$this->expectExceptionCode(Ga4Exception::REQUEST_MISSING_API_SECRET);
116+
$this->expectException(Facade\Type\Ga4ExceptionType::class);
117+
$this->expectExceptionCode(Facade\Type\Ga4ExceptionType::REQUEST_MISSING_API_SECRET);
119118

120119
Analytics::new($this->prefill['measurement_id'], '', true)->post();
121120
}
@@ -128,8 +127,8 @@ public function test_throws_on_too_large_request_package()
128127
$preparyKB .= 'AAAAAAAA'; // 8 bytes
129128
}
130129

131-
$this->expectException(Ga4Exception::class);
132-
$this->expectExceptionCode(Ga4Exception::REQUEST_TOO_LARGE);
130+
$this->expectException(Facade\Type\Ga4ExceptionType::class);
131+
$this->expectExceptionCode(Facade\Type\Ga4ExceptionType::REQUEST_TOO_LARGE);
133132

134133
$userProperty = UserProperty::new()->setName('large_package');
135134

@@ -141,4 +140,24 @@ public function test_throws_on_too_large_request_package()
141140

142141
$this->analytics->addUserProperty($userProperty)->post();
143142
}
143+
144+
public function test_timeasmicro_throws_exceeding_max()
145+
{
146+
$time = time() + 60;
147+
148+
$this->expectException(Facade\Type\Ga4ExceptionType::class);
149+
$this->expectExceptionCode(Facade\Type\Ga4ExceptionType::MICROTIME_EXPIRED);
150+
151+
$this->analytics->setTimestampMicros($time);
152+
}
153+
154+
public function test_timeasmicro_throws_exceeding_min()
155+
{
156+
$time = strtotime('-1 month');
157+
158+
$this->expectException(Facade\Type\Ga4ExceptionType::class);
159+
$this->expectExceptionCode(Facade\Type\Ga4ExceptionType::MICROTIME_EXPIRED);
160+
161+
$this->analytics->setTimestampMicros($time);
162+
}
144163
}

test/Unit/HelperTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace AlexWestergaard\PhpGa4Test\Unit;
44

55
use AlexWestergaard\PhpGa4\Helper;
6+
use AlexWestergaard\PhpGa4\Facade;
67
use AlexWestergaard\PhpGa4\Event;
78
use AlexWestergaard\PhpGa4Test\TestCase;
89

@@ -36,4 +37,25 @@ public function test_camelcase_helper_transforms_snakecase_names()
3637
$output = Helper\ConvertHelper::camel('snake_case');
3738
$this->assertEquals('snakeCase', $output);
3839
}
40+
41+
public function test_timeasmicro_converts_to_microseconds()
42+
{
43+
$time = time();
44+
$secondAsMicro = 1_000_000;
45+
$timeAsMicro = $time * $secondAsMicro;
46+
47+
$convert = Helper\ConvertHelper::timeAsMicro($time);
48+
49+
$this->assertEquals($timeAsMicro, $convert);
50+
}
51+
52+
public function test_timeasmicro_throws_too_large()
53+
{
54+
$time = time() * 100;
55+
56+
$this->expectException(Facade\Type\Ga4ExceptionType::class);
57+
$this->expectExceptionCode(Facade\Type\Ga4ExceptionType::MICROTIME_INVALID_FORMAT);
58+
59+
$this->analytics->setTimestampMicros($time);
60+
}
3961
}

0 commit comments

Comments
 (0)