Skip to content

Commit 8c5c662

Browse files
author
Robin Cawser
committed
Update existing generators
1 parent bbb96dd commit 8c5c662

6 files changed

Lines changed: 51 additions & 31 deletions

File tree

spec/Vivait/StringGeneratorBundle/EventListener/GeneratorListenerSpec.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Vivait\StringGeneratorBundle\Annotation\GeneratorAnnotation;
1616
use Vivait\StringGeneratorBundle\Generator\StringGenerator;
1717
use Vivait\StringGeneratorBundle\Model\ConfigurableGeneratorInterface;
18+
use Vivait\StringGeneratorBundle\Model\GeneratorInterface;
1819
use Vivait\StringGeneratorBundle\Registry\Registry;
1920

2021
class GeneratorListenerSpec extends ObjectBehavior
@@ -60,7 +61,7 @@ function it_can_get_callback_values_from_annotated_object(StringGenerator $gener
6061
$this->performCallbacks($generator, $annotation, $this->mockEntity);
6162
}
6263

63-
function it_sets_null_properties_if_override_set_to_true(Registry $registry, Reader $reader, LifecycleEventArgs $args, StringGenerator $generator)
64+
function it_sets_null_properties_if_override_set_to_true(Registry $registry, Reader $reader, LifecycleEventArgs $args, GeneratorInterface $generator)
6465
{
6566
$annotation = new GeneratorAnnotation([]);
6667
$annotation->override = false;
@@ -74,7 +75,7 @@ function it_sets_null_properties_if_override_set_to_true(Registry $registry, Rea
7475

7576
$this->prePersist($args);
7677
}
77-
function it_wont_set_non_null_properties_if_override_set_to_true(Reader $reader, LifecycleEventArgs $args, StringGenerator $generator)
78+
function it_wont_set_non_null_properties_if_override_set_to_true(Reader $reader, LifecycleEventArgs $args, GeneratorInterface $generator)
7879
{
7980
$this->mockEntity->setName('Robin');
8081
$annotation = new GeneratorAnnotation([]);
@@ -86,7 +87,7 @@ function it_wont_set_non_null_properties_if_override_set_to_true(Reader $reader,
8687
$this->prePersist($args);
8788
}
8889

89-
function it_generates_a_string_on_a_property(Registry $registry, Reader $reader, LifecycleEventArgs $args, StringGenerator $generator)
90+
function it_generates_a_string_on_a_property(Registry $registry, Reader $reader, LifecycleEventArgs $args, GeneratorInterface $generator)
9091
{
9192
$annotation = new GeneratorAnnotation([]);
9293

spec/Vivait/StringGeneratorBundle/Generator/StringGeneratorSpec.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@ function it_generates_a_string_based_on_length()
1818
$this->generate()->shouldHaveStrlen(10);
1919
}
2020

21-
function it_should_throw_an_exception_if_it_has_no_chars()
22-
{
23-
$this->shouldThrow('\OutOfBoundsException')->duringSetChars('');
24-
$this->shouldThrow('\OutOfBoundsException')->duringSetChars(null);
25-
$this->shouldThrow('\OutOfBoundsException')->duringSetChars(true);
26-
}
27-
28-
function it_should_error_if_its_length_is_set_to_less_than_1()
29-
{
30-
$this->shouldThrow('\OutOfBoundsException')->duringSetLength(0);
31-
$this->shouldThrow('\OutOfBoundsException')->duringSetLength(-1);
32-
$this->shouldThrow('\OutOfBoundsException')->duringSetLength("ten");
33-
}
34-
3521
function getMatchers()
3622
{
3723
return [

src/Vivait/StringGeneratorBundle/Generator/SecureBytesGenerator.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Vivait\StringGeneratorBundle\Generator;
44

5+
use Symfony\Component\OptionsResolver\OptionsResolver;
56
use Symfony\Component\Security\Core\Util\SecureRandom;
6-
use Vivait\StringGeneratorBundle\Model\GeneratorInterface;
7+
use Vivait\StringGeneratorBundle\Model\ConfigurableGeneratorInterface;
78

8-
class SecureBytesGenerator implements GeneratorInterface
9+
class SecureBytesGenerator implements ConfigurableGeneratorInterface
910
{
1011
/**
1112
* @var SecureRandom
@@ -28,7 +29,6 @@ public function __construct(SecureRandom $secureRandom)
2829
public function setLength($length)
2930
{
3031
$this->length = $length;
31-
return $this;
3232
}
3333

3434
/**
@@ -39,11 +39,23 @@ public function generate()
3939
return $this->secureRandom->nextBytes($this->length);
4040
}
4141

42+
/**
43+
* @param OptionsResolver $resolver
44+
* @return mixed
45+
*/
46+
public function getDefaultOptions(OptionsResolver $resolver)
47+
{
48+
$resolver->setDefaults([
49+
'length' => $this->length,
50+
]);
51+
}
52+
4253
/**
4354
* @param array $options
55+
* @return mixed
4456
*/
4557
public function setOptions(array $options)
4658
{
47-
// TODO: Implement setOptions() method.
59+
$this->length = $options['length'];
4860
}
4961
}

src/Vivait/StringGeneratorBundle/Generator/SecureStringGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function getGenerator($strength = 'medium')
6363
}
6464
}
6565

66-
public function setOptions(array $options = [])
66+
public function setOptions(array $options)
6767
{
6868
$this->generator = $this->getGenerator($options['strength']);
6969
$this->length = $options['length'];

src/Vivait/StringGeneratorBundle/Generator/StringGenerator.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Vivait\StringGeneratorBundle\Generator;
44

5-
use Vivait\StringGeneratorBundle\Model\GeneratorInterface;
5+
use Symfony\Component\OptionsResolver\OptionsResolver;
6+
use Vivait\StringGeneratorBundle\Model\ConfigurableGeneratorInterface;
67

7-
class StringGenerator implements GeneratorInterface
8+
class StringGenerator implements ConfigurableGeneratorInterface
89
{
910

1011
/**
@@ -16,10 +17,15 @@ class StringGenerator implements GeneratorInterface
1617
* @var string
1718
*/
1819
private $chars = 'abcdefjhijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12345567890';
20+
21+
/**
22+
* @var string
23+
*/
1924
private $prefix = '';
2025

2126
/**
2227
* @param string $chars
28+
* @deprecated options should be used instead
2329
*/
2430
public function setChars($chars)
2531
{
@@ -37,14 +43,13 @@ public function setChars($chars)
3743
*/
3844
public function setLength($length)
3945
{
40-
if ($length < 1) {
41-
throw new \OutOfBoundsException('Length must be greater than 0');
42-
}
43-
4446
$this->length = $length;
45-
return $this;
4647
}
4748

49+
/**
50+
* @param $prefix
51+
* @deprecated options should be used instead
52+
*/
4853
public function setPrefix($prefix)
4954
{
5055
$this->prefix = $prefix;
@@ -69,9 +74,25 @@ public function generate()
6974

7075
/**
7176
* @param array $options
77+
* @return mixed|void
7278
*/
7379
public function setOptions(array $options)
7480
{
75-
// TODO: Implement setOptions() method.
81+
$this->chars = $options['chars'];
82+
$this->length = $options['length'];
83+
$this->prefix = $options['prefix'];
84+
}
85+
86+
/**
87+
* @param OptionsResolver $resolver
88+
* @return mixed
89+
*/
90+
public function getDefaultOptions(OptionsResolver $resolver)
91+
{
92+
$resolver->setDefaults([
93+
'chars' => $this->chars,
94+
'length' => $this->length,
95+
'prefix' => $this->prefix,
96+
]);
7697
}
7798
}

src/Vivait/StringGeneratorBundle/Model/ConfigurableGeneratorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface ConfigurableGeneratorInterface extends GeneratorInterface
1010
* @param array $options
1111
* @return mixed
1212
*/
13-
public function setOptions(array $options = []);
13+
public function setOptions(array $options);
1414

1515
/**
1616
* @param OptionsResolver $resolver

0 commit comments

Comments
 (0)