Skip to content

Commit 3a9ab1a

Browse files
committed
Merge pull request #25 from robcaw/master
Generator created at point of calling generate()
2 parents 1757768 + c6e4e6f commit 3a9ab1a

2 files changed

Lines changed: 8 additions & 18 deletions

File tree

spec/Vivait/StringGeneratorBundle/Generator/SecureStringGeneratorSpec.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111

1212
class SecureStringGeneratorSpec extends ObjectBehavior
1313
{
14-
15-
/**
16-
* @var Factory
17-
*/
18-
private $factory;
19-
2014
function it_is_initializable()
2115
{
2216
$this->shouldHaveType('Vivait\StringGeneratorBundle\Generator\SecureStringGenerator');
@@ -37,11 +31,6 @@ function let(Factory $factory, Generator $low, Generator $medium)
3731
$this->setOptions($defaults);
3832
}
3933

40-
function it_chooses_medium_strength_gen_by_default(Generator $medium)
41-
{
42-
$this->getGenerator()->shouldReturn($medium);
43-
}
44-
4534
function it_chooses_a_specified_strength_gen(Generator $medium, Generator $low)
4635
{
4736
$this->getGenerator('medium')->shouldReturn($medium);

src/Vivait/StringGeneratorBundle/Generator/SecureStringGenerator.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ class SecureStringGenerator implements ConfigurableGeneratorInterface
1111
{
1212

1313
/**
14-
* @var
14+
* @var int
1515
*/
1616
private $length = 32;
1717

1818
/**
19-
* @var Generator
19+
* @var string
2020
*/
21-
private $generator;
22-
2321
private $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
2422

23+
/**
24+
* @var string
25+
*/
2526
private $strength = 'medium';
2627

2728
/**
@@ -44,10 +45,10 @@ public function __construct(Factory $factory)
4445
*/
4546
public function generate()
4647
{
47-
return $this->generator->generateString($this->length, $this->chars);
48+
return $this->getGenerator($this->strength)->generateString($this->length, $this->chars);
4849
}
4950

50-
public function getGenerator($strength = 'medium')
51+
public function getGenerator($strength)
5152
{
5253
switch ($strength) {
5354
case 'low':
@@ -63,7 +64,7 @@ public function getGenerator($strength = 'medium')
6364

6465
public function setOptions(array $options)
6566
{
66-
$this->generator = $this->getGenerator($options['strength']);
67+
$this->strength = $options['strength'];
6768
$this->length = $options['length'];
6869
$this->chars = $options['chars'];
6970
}

0 commit comments

Comments
 (0)