From 3459f2e8ad4cbe49609bc4a3f5f4bf8ae2c14d82 Mon Sep 17 00:00:00 2001 From: Sveneld Date: Thu, 29 Feb 2024 20:28:21 +0100 Subject: [PATCH] CodeGenerator --- actions-web.php | 4 +-- common.php | 20 +++++--------- src/Credit/CodeGenerator/CodeGenerator.php | 19 ++++++++++++++ .../CodeGenerator/CodeGeneratorInterface.php | 8 ++++++ .../CodeGenerator/CodeGeneratorTest.php | 26 +++++++++++++++++++ 5 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 src/Credit/CodeGenerator/CodeGenerator.php create mode 100644 src/Credit/CodeGenerator/CodeGeneratorInterface.php create mode 100644 tests/Credit/CodeGenerator/CodeGeneratorTest.php diff --git a/actions-web.php b/actions-web.php index 0dd8e60..fb4ae50 100644 --- a/actions-web.php +++ b/actions-web.php @@ -682,14 +682,14 @@ function getcouponlist() function generatecoupons($multiplier) { - global $db, $credit; + global $db, $credit, $codeGenerator; if (iscreditenabled() == false) { return; } // if credit system disabled, exit $requiredcredit = $credit['min'] + $credit['rent'] + $credit['longrental']; $value = $requiredcredit * $multiplier; - $codes = generatecodes(10, 6); + $codes = $codeGenerator->generate(10, 6); foreach ($codes as $code) { $result = $db->query("INSERT IGNORE INTO coupons SET coupon='" . $code . "',value='" . $value . "',status='0'"); } diff --git a/common.php b/common.php index f96fa0d..782a099 100644 --- a/common.php +++ b/common.php @@ -1,5 +1,7 @@ acceptableChars), 0, $length); + } + + return array_slice($codes, 0, ($count)); + } +} diff --git a/src/Credit/CodeGenerator/CodeGeneratorInterface.php b/src/Credit/CodeGenerator/CodeGeneratorInterface.php new file mode 100644 index 0000000..cd2cf13 --- /dev/null +++ b/src/Credit/CodeGenerator/CodeGeneratorInterface.php @@ -0,0 +1,8 @@ +generate($count, $length, $wastage); + $this->assertCount($count, $codes); + foreach ($codes as $code) { + $this->assertEquals($length, strlen($code)); + $this->assertRegExp('/^[' . $this->acceptableChars . ']{' . $length . '}$/', $code); + } + } +}