|
5 | 5 | use Doctrine\Common\Annotations\Reader; |
6 | 6 | use Doctrine\ORM\Event\LifecycleEventArgs; |
7 | 7 | use Vivait\StringGeneratorBundle\Annotation as Vivait; |
8 | | -use Vivait\StringGeneratorBundle\Generator\StringGenerator; |
9 | | -use Vivait\StringGeneratorBundle\Model\StringGeneratorInterface; |
| 8 | +use Vivait\StringGeneratorBundle\Model\GeneratorInterface; |
10 | 9 |
|
11 | | -class IdGeneratorListener |
| 10 | +class StringGeneratorListener |
12 | 11 | { |
13 | 12 | private $reader; |
14 | 13 | private $repo; |
| 14 | + /** |
| 15 | + * @var GeneratorInterface |
| 16 | + */ |
| 17 | + private $generator; |
15 | 18 |
|
16 | | - public function __construct(Reader $reader) |
| 19 | + public function __construct(Reader $reader, GeneratorInterface $generator) |
17 | 20 | { |
18 | 21 | $this->reader = $reader; |
| 22 | + $this->generator = $generator; |
19 | 23 | } |
20 | 24 |
|
21 | 25 | /** |
@@ -58,22 +62,23 @@ public function prePersist(LifecycleEventArgs $args) |
58 | 62 | */ |
59 | 63 | private function generateId($property, Vivait\StringGenerator $annotation) |
60 | 64 | { |
61 | | - $generator = new StringGenerator(); |
62 | | - $generator |
| 65 | + $this->generator |
63 | 66 | ->setAlphabet($annotation->alphabet) |
64 | 67 | ->setLength($annotation->length); |
65 | 68 |
|
66 | | - $id = $generator->generate(); |
| 69 | + $str = $this->generator->generate(); |
67 | 70 |
|
68 | 71 | if ($annotation->prefix) { |
69 | | - $id = sprintf("%s%s%s", $annotation->prefix, $annotation->separator, $id); |
| 72 | + $str = sprintf("%s%s%s", $annotation->prefix, $annotation->separator, $str); |
70 | 73 | } |
71 | 74 |
|
72 | | - if ($this->repo->findOneBy([$property => $id])) { |
| 75 | + if ($this->repo->findOneBy([$property => $str])) { |
73 | 76 | $this->generateId($property, $annotation); |
74 | 77 | } else { |
75 | | - return $id; |
| 78 | + return $str; |
76 | 79 | } |
| 80 | + |
| 81 | + |
77 | 82 | } |
78 | 83 |
|
79 | 84 |
|
|
0 commit comments