-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Adherent] Generate unique public ids
- Loading branch information
Showing
16 changed files
with
85 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace App\Adherent; | ||
|
||
use App\Repository\AdherentRepository; | ||
|
||
class PublicIdGenerator | ||
{ | ||
public function __construct(public readonly AdherentRepository $adherentRepository) | ||
{ | ||
} | ||
|
||
public function generate(): string | ||
{ | ||
$publicId = self::build(); | ||
|
||
return !$this->checkIfAlreadyExists($publicId) | ||
? $publicId | ||
: $this->generate(); | ||
} | ||
|
||
public static function build(): string | ||
{ | ||
$characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; | ||
|
||
$charactersArray = str_split($characters); | ||
|
||
shuffle($charactersArray); | ||
|
||
$block1 = self::generateRandomBlock($charactersArray, 3); | ||
$block2 = self::generateRandomBlock($charactersArray, 3); | ||
|
||
return $block1.'-'.$block2; | ||
} | ||
|
||
private static function generateRandomBlock(array $characters, int $length): string | ||
{ | ||
$block = ''; | ||
$maxIndex = \count($characters) - 1; | ||
|
||
for ($i = 0; $i < $length; ++$i) { | ||
$block .= $characters[random_int(0, $maxIndex)]; | ||
} | ||
|
||
return $block; | ||
} | ||
|
||
private function checkIfAlreadyExists(string $publicId): bool | ||
{ | ||
return $this->adherentRepository->count(['publicId' => $publicId]) > 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ class LoadBannedAdherentData extends Fixture | |
{ | ||
public function load(ObjectManager $manager): void | ||
{ | ||
$adherent = Adherent::createBlank('male', 'test', 'test', 'FR', PostAddress::createEmptyAddress(), '[email protected]', null, new \DateTime('-18 years')); | ||
$adherent = Adherent::createBlank('ABC-234', 'male', 'test', 'test', 'FR', PostAddress::createEmptyAddress(), '[email protected]', null, new \DateTime('-18 years')); | ||
|
||
$manager->persist(BannedAdherent::createFromAdherent($adherent)); | ||
$manager->flush(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -381,6 +381,7 @@ private function createNewAdherent(string $gender = Genders::MALE, ?string $birt | |
{ | ||
return Adherent::create( | ||
Uuid::fromString('c0d66d5f-e124-4641-8fd1-1dd72ffda563'), | ||
'ABC-234', | ||
'[email protected]', | ||
'password', | ||
$gender, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,7 @@ private function createNewAdherent(): Adherent | |
{ | ||
return Adherent::create( | ||
Uuid::fromString(self::ADHERENT_UUID), | ||
'ABC-234', | ||
'[email protected]', | ||
'password', | ||
'male', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ public function createUser(): Adherent | |
{ | ||
return Adherent::create( | ||
Uuid::uuid4(), | ||
'ABC-234', | ||
'[email protected]', | ||
'', | ||
'male', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,7 @@ private function createNewAdherent(string $address): Adherent | |
{ | ||
return Adherent::create( | ||
Uuid::fromString('d3522426-1bac-4da4-ade8-5204c9e2caae'), | ||
'ABC-234', | ||
'[email protected]', | ||
'super-password', | ||
'male', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ private function createAdherent(): Adherent | |
{ | ||
return Adherent::create( | ||
Adherent::createUuid('[email protected]'), | ||
'ABC-234', | ||
'[email protected]', | ||
'super-password', | ||
'male', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters