Skip to content

Commit 6fb2ba5

Browse files
committed
Feature: Implemented batch for better management of large generations
1 parent cb168e1 commit 6fb2ba5

File tree

9 files changed

+134
-10
lines changed

9 files changed

+134
-10
lines changed

Entity/SmartCode.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,17 @@ class SmartCode implements SmartCodeInterface
8585
*/
8686
protected $createdAt;
8787

88+
/**
89+
* @ORM\Column(type="string", length=255)
90+
*
91+
* @var string
92+
*/
93+
protected $batch;
94+
8895
/**
8996
* @ORM\ManyToMany(targetEntity="Intracto\SmartCodeBundle\Entity\SubjectInterface", mappedBy="smartCodes", fetch="EXTRA_LAZY")
9097
**/
91-
private $subjects;
98+
protected $subjects;
9299

93100
public function __construct(PayloadInterface $payload)
94101
{
@@ -257,6 +264,22 @@ public function setSubjects($subjects)
257264
$this->subjects = $subjects;
258265
}
259266

267+
/**
268+
* @return string
269+
*/
270+
public function getBatch()
271+
{
272+
return $this->batch;
273+
}
274+
275+
/**
276+
* @param string $batch
277+
*/
278+
public function setBatch($batch)
279+
{
280+
$this->batch = $batch;
281+
}
282+
260283
/**
261284
* {@inheritdoc}
262285
*/

Generator/SmartCodeGenerator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\ORM\EntityManagerInterface;
66
use Intracto\SmartCodeBundle\Entity\SmartCode;
77
use Intracto\SmartCodeBundle\Entity\PayloadInterface;
8+
use Symfony\Component\Config\Tests\Loader\Validator;
89

910
/**
1011
* Default smart code generator.
@@ -36,6 +37,7 @@ public function generate(PayloadInterface $payload, SmartCodeOptions $options)
3637
$smartCode->setUsageLimit($options->getUsageLimit());
3738
$smartCode->setExpiresAt($options->getExpiresAt());
3839
$smartCode->setStartsAt($options->getStartsAt());
40+
$smartCode->setBatch($options->getBatch());
3941

4042
$codes[] = $smartCode;
4143
$this->manager->persist($smartCode);

Generator/SmartCodeOptions.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,56 @@
22

33
namespace Intracto\SmartCodeBundle\Generator;
44

5+
use Symfony\Component\Validator\Constraints as Assert;
6+
57
class SmartCodeOptions
68
{
9+
/**
10+
* @Assert\NotBlank()
11+
* @Assert\GreaterThan(value = 0)
12+
* @Assert\Type(
13+
* type="numeric",
14+
* message="The value {{ value }} is not a valid {{ type }}."
15+
* )
16+
*/
717
protected $amount;
18+
19+
/**
20+
* @Assert\NotBlank()
21+
* @Assert\Length(
22+
* max = 255,
23+
* maxMessage = "The title cannot be longer than {{ limit }} characters"
24+
* )
25+
*/
26+
protected $batch;
27+
28+
/**
29+
* @Assert\NotBlank()
30+
* @Assert\GreaterThan(value = 0)
31+
* @Assert\Type(
32+
* type="numeric",
33+
* message="The value {{ value }} is not a valid {{ type }}."
34+
* )
35+
*/
836
protected $usageLimit;
37+
38+
/**
39+
* @Assert\DateTime()
40+
*/
941
protected $expiresAt;
42+
43+
/**
44+
* @Assert\DateTime()
45+
*/
1046
protected $startsAt;
1147

1248
public function __construct()
1349
{
50+
$now = new \DateTime();
1451
$this->amount = 1;
1552
$this->usageLimit = 1;
16-
$this->startsAt = new \DateTime();
53+
$this->startsAt = $now;
54+
$this->batch = $now->format('d-m-Y H:i:s');
1755
}
1856

1957
public function getAmount()
@@ -63,4 +101,14 @@ public function setStartsAt($startsAt)
63101

64102
return $this;
65103
}
104+
105+
public function getBatch()
106+
{
107+
return $this->batch;
108+
}
109+
110+
public function setBatch($batch)
111+
{
112+
$this->batch = $batch;
113+
}
66114
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ containing all your options.
8686
protected $usageLimit;
8787
protected $expiresAt;
8888
protected $startsAt;
89+
protected $batch;
8990
9091
...
9192
}
@@ -95,6 +96,7 @@ containing all your options.
9596
- **UsageLimit**: The amount of times a smart code can be used.
9697
- **ExpiresAt**: The expiry date for a smart code.
9798
- **StartsAt**: The date a smart code can start being used.
99+
- **Batch**: A name or description you want to give the current generation.
98100

99101
The last thing you would probably want to do is to be able to use these smart codes you just generated.
100102
This is possible via the SmartCodeAction service, which you can also overwrite by implementing the SmartCodeActionInterface.

Repository/SmartCodeRepository.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,32 @@
77

88
class SmartCodeRepository extends EntityRepository
99
{
10-
public function findAllCreationDatesForPayload(PayloadInterface $payload)
10+
public function findAllBatchesForPayload(PayloadInterface $payload)
1111
{
1212
return $this->getEntityManager()
1313
->createQuery(
14-
'SELECT sc.createdAt, sc.usageLimit, sc.startsAt, sc.expiresAt, count(sc.id) as total
14+
'SELECT sc.batch, sc.createdAt, sc.usageLimit, sc.startsAt, sc.expiresAt, count(sc.id) as total
1515
FROM SmartCodeBundle:SmartCode sc
1616
WHERE sc.payload = :payload
17-
GROUP BY sc.createdAt
17+
GROUP BY sc.batch
1818
ORDER BY sc.id ASC'
1919
)
2020
->setParameter('payload', $payload)
2121
->getResult();
2222
}
2323

24-
public function findAllByCreationDate(\DateTime $creationDate, PayloadInterface $payload)
24+
public function findAllByBatch($batch, PayloadInterface $payload)
2525
{
2626
return $this->getEntityManager()
2727
->createQuery(
2828
'SELECT sc
2929
FROM SmartCodeBundle:SmartCode sc
30-
WHERE sc.createdAt = :creationDate
30+
WHERE sc.batch = :batch
3131
AND sc.payload = :payload
3232
ORDER BY sc.id ASC'
3333
)
3434
->setParameter('payload', $payload)
35-
->setParameter('creationDate', $creationDate)
35+
->setParameter('batch', $batch)
3636
->getResult();
3737
}
3838
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Intracto\SmartCodeBundle\Tests\Generator;
4+
5+
use Intracto\SmartCodeBundle\Generator\SmartCodeOptions;
6+
use Intracto\SmartCodeBundle\Tests\BaseTest;
7+
use Symfony\Component\Validator\Validation;
8+
9+
class SmartCodeOptionsTest extends BaseTest
10+
{
11+
private $validator;
12+
13+
public function setUp()
14+
{
15+
16+
$this->validator = Validation::createValidatorBuilder()
17+
->enableAnnotationMapping()
18+
->getValidator();
19+
}
20+
21+
public function testSmartCodeOptionsCorrect()
22+
{
23+
$options = new SmartCodeOptions();
24+
$options->setAmount(500);
25+
$options->setBatch("Test batch");
26+
27+
$violations = $this->validator->validate($options);
28+
29+
$this->assertCount(0, $violations);
30+
}
31+
32+
public function testSmartCodeOptionsFailed()
33+
{
34+
$options = new SmartCodeOptions();
35+
$options->setAmount(-1);
36+
$options->setBatch("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.");
37+
$options->setUsageLimit(0);
38+
$options->setExpiresAt("Test failure");
39+
40+
$violations = $this->validator->validate($options);
41+
42+
$this->assertCount(4, $violations);
43+
}
44+
}

Tests/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
include __DIR__ . '/../vendor/autoload.php';
3+
4+
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader('class_exists');

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"require": {
1616
"php": ">=5.3.9",
1717
"doctrine/orm": "~2.2",
18-
"symfony/framework-bundle": "~2.1"
18+
"symfony/framework-bundle": "~2.3",
19+
"symfony/validator": "~2.3"
1920
},
2021

2122
"config": {

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8" ?>
2-
<phpunit bootstrap="./vendor/autoload.php">
2+
<phpunit bootstrap="./Tests/bootstrap.php">
33

44
<testsuites>
55
<testsuite name="SmartCodeTestSuite">

0 commit comments

Comments
 (0)