Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. # UniqueRandomDataGeneratorService
  2.  
  3. **src/Service/UniqueRandomDataGeneratorService.php**
  4. ```php
  5. <?php
  6.  
  7. namespace App\Service;
  8.  
  9. use App\Helper\RandomDataGeneratorHelper;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Exception;
  12.  
  13. /**
  14. * Class UniqueRandomDataGeneratorService
  15. *
  16. * Generates cryptographically secure pseudo-random unique data.
  17. * Unique meaning it does not exist yet in database for given entity and property.
  18. *
  19. * @package App\Service
  20. */
  21. class UniqueRandomDataGeneratorService
  22. {
  23. /**
  24. * @var EntityManagerInterface
  25. */
  26. private $em;
  27.  
  28. /**
  29. * UniqueRandomDataGeneratorService constructor.
  30. * @param EntityManagerInterface $em
  31. */
  32. public function __construct(EntityManagerInterface $em)
  33. {
  34. $this->em = $em;
  35. }
  36.  
  37. /**
  38. * @param string $entityClass
  39. * @param string $propertyName
  40. * @param $data
  41. * @return bool
  42. */
  43. private function isUnique(string $entityClass, string $propertyName, $data): bool
  44. {
  45. $duplicate = $this->em->getRepository($entityClass)->findOneBy([
  46. $propertyName => $data
  47. ]);
  48.  
  49. return is_null($duplicate);
  50. }
  51.  
  52. /**
  53. * @param string $entityClass
  54. * @param string $propertyName
  55. * @param int $min
  56. * @param int $max
  57. * @param int $maxDecimalNbr
  58. * @return float
  59. * @throws Exception
  60. */
  61. public function uniqueRandomFloat(
  62. string $entityClass,
  63. string $propertyName,
  64. int $min = 0,
  65. int $max = 2147483647,
  66. int $maxDecimalNbr = 1
  67. ): float
  68. {
  69. while (true) {
  70. $randomFloat = RandomDataGeneratorHelper::randomFloat($min, $max, $maxDecimalNbr);
  71.  
  72. if ($this->isUnique($entityClass, $propertyName, $randomFloat)) {
  73. return $randomFloat;
  74. }
  75. }
  76.  
  77. throw new Exception('While loop should not have broken');
  78. }
  79.  
  80. /**
  81. * @param string $entityClass
  82. * @param string $propertyName
  83. * @param int $min
  84. * @param int $max
  85. * @return int
  86. * @throws Exception
  87. */
  88. public function uniqueRandomInteger(
  89. string $entityClass,
  90. string $propertyName,
  91. int $min = 0,
  92. int $max = 2147483647
  93. ): int
  94. {
  95. while (true) {
  96. $randomInt = RandomDataGeneratorHelper::randomInteger($min, $max);
  97.  
  98. if ($this->isUnique($entityClass, $propertyName, $randomInt)) {
  99. return $randomInt;
  100. }
  101. }
  102.  
  103. throw new Exception('While loop should not have broken');
  104. }
  105.  
  106. /**
  107. * @param string $entityClass
  108. * @param string $propertyName
  109. * @param int $entropy
  110. * @return string
  111. * @throws Exception
  112. */
  113. public function uniqueRandomString(string $entityClass, string $propertyName, int $entropy = 512): string
  114. {
  115. while (true) {
  116. $randomString = RandomDataGeneratorHelper::randomString($entropy);
  117.  
  118. if ($this->isUnique($entityClass, $propertyName, $randomString)) {
  119. return $randomString;
  120. }
  121. }
  122.  
  123. throw new Exception('While loop should not have broken');
  124. }
  125. }
  126. ```
  127.  
  128. **Example use**
  129. ```php
  130. $user->setAccountActivationToken(
  131. $uniqueRandomDataGenerator->uniqueRandomString(
  132. User::class,
  133. 'accountActivationToken'
  134. )
  135. );
  136. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement