From f29c69cad1a491dd64519b02962ff264852704cb Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 16 Nov 2021 04:45:19 +0000 Subject: [PATCH] Fix checking for duplicate ids. #638 --- src/Service/UniqueIdGenerator.php | 34 ++++++++----------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/src/Service/UniqueIdGenerator.php b/src/Service/UniqueIdGenerator.php index 972a7ef8..0e8f05b0 100644 --- a/src/Service/UniqueIdGenerator.php +++ b/src/Service/UniqueIdGenerator.php @@ -29,7 +29,9 @@ class UniqueIdGenerator $generated_id = $this->generateUniqueId($str_length); // check if generated id already exists - if (!isset($this->cust_generated_ids[$generated_id])) + $cust = $em->getRepository(Customer::class)->findOneBy(['generated_id' => $generated_id]); + + if ($cust == null) return $generated_id; sleep(1); @@ -39,32 +41,14 @@ class UniqueIdGenerator protected function generateUniqueId($str_length) { - return substr(md5(time()), 0, $str_length); - } + $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $rand_string = ''; + $desired_length = 10; - protected function loadGeneratedIds() - { - error_log('loading generated ids '); - $cust_q = $this->em->createQuery('select c from App\Entity\Customer c where c.generated_id is not null'); - $cust_iter = $cust_q->iterate(); + $rand_string = substr(str_shuffle($charset), 0, $desired_length); - $this->cust_generated_ids = []; - foreach ($cust_iter as $row) - { - $customer = $row[0]; - $generated_id = $customer->getGeneratedID(); - error_log('generated id = ' . $generated_id); - if ($generated_id != null) - { - error_log('generated id is not null ' . $generated_id); - if (!isset($this->cust_generated_ids[$generated_id])) - { - error_log('setting to hash ' . $generated_id); - $this->cust_generated_ids[$generated_id] = $customer; - } - } + $salt = time() . $rand_string; - $this->em->detach($row[0]); - } + return substr(md5($salt), 0, $str_length); } }