Skip to content

Commit

Permalink
CodeGenerator (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneld authored Feb 29, 2024
1 parent d2b547a commit d099f3a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 15 deletions.
4 changes: 2 additions & 2 deletions actions-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'");
}
Expand Down
20 changes: 7 additions & 13 deletions common.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use BikeShare\Credit\CodeGenerator\CodeGenerator;
use BikeShare\Credit\CodeGenerator\CodeGeneratorInterface;
use BikeShare\Mail\DebugMailSender;
use BikeShare\Mail\MailSenderInterface;
use BikeShare\Mail\PHPMailerMailSender;
Expand Down Expand Up @@ -52,6 +54,11 @@
$db
);

/**
* @var CodeGeneratorInterface $codeGenerator
*/
$codeGenerator = new CodeGenerator();


function error($message)
{
Expand All @@ -61,19 +68,6 @@ function error($message)
}


function generatecodes($numcodes,$codelength,$wastage=25)
{
// exclude problem chars: B8G6I1l0OQDS5Z2
// acceptable characters:
$goodchars='ACEFHJKMNPRTUVWXY4937';
// build array allowing for possible wastage through duplicate values
for ($i=0;$i<=$numcodes+$wastage+1;$i++)
{
$codes[]=substr(str_shuffle($goodchars),0,$codelength);
}
return array_slice($codes,0,($numcodes+1));
}

function getprivileges($userid)
{
global $db;
Expand Down
19 changes: 19 additions & 0 deletions src/Credit/CodeGenerator/CodeGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace BikeShare\Credit\CodeGenerator;

class CodeGenerator implements CodeGeneratorInterface
{
// exclude problem chars: B8G6I1l0OQDS5Z2
private $acceptableChars = 'ACEFHJKMNPRTUVWXY4937';

public function generate($count, $length, $wastage = 25)
{
// build array allowing for possible wastage through duplicate values
for ($i = 0; $i <= $count + $wastage + 1; $i++) {
$codes[] = substr(str_shuffle($this->acceptableChars), 0, $length);
}

return array_slice($codes, 0, ($count));
}
}
8 changes: 8 additions & 0 deletions src/Credit/CodeGenerator/CodeGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace BikeShare\Credit\CodeGenerator;

interface CodeGeneratorInterface
{
public function generate($count, $length, $wastage);
}
26 changes: 26 additions & 0 deletions tests/Credit/CodeGenerator/CodeGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Test\BikeShare\Credit\CodeGenerator;

use BikeShare\Credit\CodeGenerator\CodeGenerator;
use PHPUnit\Framework\TestCase;

class CodeGeneratorTest extends TestCase
{
private $acceptableChars = 'ACEFHJKMNPRTUVWXY4937';

public function testGenerate()
{
$count = 10;
$length = 8;
$wastage = 25;

$codeGenerator = new CodeGenerator();
$codes = $codeGenerator->generate($count, $length, $wastage);
$this->assertCount($count, $codes);
foreach ($codes as $code) {
$this->assertEquals($length, strlen($code));
$this->assertRegExp('/^[' . $this->acceptableChars . ']{' . $length . '}$/', $code);
}
}
}

0 comments on commit d099f3a

Please sign in to comment.