Improve randomness generated codes

[MAILPOET-6451]
This commit is contained in:
Pavel Dohnal
2025-01-29 09:15:01 +01:00
committed by Ján Mikláš
parent f20237ab1c
commit 66741b9bc1

View File

@ -146,18 +146,27 @@ class CouponPreProcessor {
}, $items); }, $items);
} }
private function generateRandomSegment($length) {
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$segment = '';
for ($i = 0; $i < $length; $i++) {
$randomIndex = rand(0, strlen($characters) - 1);
$segment .= $characters[$randomIndex];
}
return $segment;
}
/** /**
* Generates Coupon code for XXXX-XXXXXX-XXXX pattern * Generates Coupon code for XXXX-XXXXXX-XXXX pattern
*/ */
private function generateRandomCode(): string { private function generateRandomCode(): string {
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $part1 = $this->generateRandomSegment(4);
$length = strlen($chars); $part2 = $this->generateRandomSegment(6);
return sprintf( $part3 = $this->generateRandomSegment(4);
"%s-%s-%s",
substr($chars, rand(0, $length - 5), 4), return $part1 . '-' . $part2 . '-' . $part3;
substr($chars, rand(0, $length - 8), 7),
substr($chars, rand(0, $length - 5), 4)
);
} }
private function shouldGenerateCoupon(array $block): bool { private function shouldGenerateCoupon(array $block): bool {