Skip to content

Commit

Permalink
[Adherent] Generate unique public ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Remg committed Jan 10, 2025
1 parent 4c3632d commit ea81d1b
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 36 deletions.
52 changes: 52 additions & 0 deletions src/Adherent/PublicIdGenerator.php
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;
}
}
2 changes: 1 addition & 1 deletion src/DataFixtures/ORM/LoadBannedAdherentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/Doctrine/Hydrators/EventHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected function hydrateRowData(array $row, array &$result)
if ($uuidOrganizer = $row['adherent_uuid'] ? Uuid::fromString($row['adherent_uuid']) : null) {
$organizer = Adherent::create(
$uuidOrganizer,
$row['adherent_public_id'],
$row['adherent_email_address'],
$uuidOrganizer,
$row['adherent_gender'],
Expand Down
7 changes: 4 additions & 3 deletions src/Entity/Adherent.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
use App\Scope\ScopeEnum;
use App\Subscription\SubscriptionTypeEnum;
use App\Utils\AreaUtils;
use App\Utils\PublicIdGenerator;
use App\Validator\UniqueMembership;
use App\Validator\ZoneBasedRoles as AssertZoneBasedRoles;
use App\ValueObject\Genders;
Expand Down Expand Up @@ -555,6 +554,7 @@ public function __construct()
}

public static function createBlank(
string $publicId,
string $gender,
string $firstName,
string $lastName,
Expand All @@ -569,7 +569,7 @@ public static function createBlank(
$adherent = new self();

$adherent->uuid = Uuid::uuid4();
$adherent->publicId = PublicIdGenerator::generatePublicIdFromUuid($adherent->uuid);
$adherent->publicId = $publicId;
$adherent->gender = $gender;
$adherent->firstName = $firstName;
$adherent->lastName = $lastName;
Expand All @@ -588,6 +588,7 @@ public static function createBlank(

public static function create(
UuidInterface $uuid,
string $publicId,
string $emailAddress,
?string $password,
?string $gender,
Expand All @@ -609,7 +610,7 @@ public static function create(
$adherent = new self();

$adherent->uuid = $uuid;
$adherent->publicId = PublicIdGenerator::generatePublicIdFromUuid($uuid);
$adherent->publicId = $publicId;
$adherent->password = $password;
$adherent->gender = $gender;
$adherent->firstName = $firstName;
Expand Down
16 changes: 15 additions & 1 deletion src/Membership/AdherentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Membership;

use App\Address\PostAddressFactory;
use App\Adherent\PublicIdGenerator;
use App\Adherent\Tag\TagEnum;
use App\Adhesion\AdhesionStepEnum;
use App\Adhesion\Request\MembershipRequest;
Expand All @@ -21,11 +22,13 @@
class AdherentFactory
{
private PasswordHasherInterface $hasher;
private PublicIdGenerator $publicIdGenerator;
private PostAddressFactory $addressFactory;

public function __construct(PasswordHasherFactoryInterface $hasherFactory, ?PostAddressFactory $addressFactory = null)
public function __construct(PasswordHasherFactoryInterface $hasherFactory, PublicIdGenerator $publicIdGenerator, ?PostAddressFactory $addressFactory = null)
{
$this->hasher = $hasherFactory->getPasswordHasher(Adherent::class);
$this->publicIdGenerator = $publicIdGenerator;
$this->addressFactory = $addressFactory ?: new PostAddressFactory();
}

Expand All @@ -42,6 +45,7 @@ private function createFromAvecVousMembershipRequest(AvecVousMembershipRequest $
{
$adherent = Adherent::create(
Adherent::createUuid($request->getEmailAddress()),
$this->generatePublicId(),
$request->getEmailAddress(),
$this->hashPassword(Uuid::uuid4()),
null,
Expand All @@ -62,6 +66,7 @@ private function createFromJeMengageMembershipRequest(JeMengageMembershipRequest
{
$adherent = Adherent::create(
Adherent::createUuid($request->getEmailAddress()),
$this->generatePublicId(),
$request->getEmailAddress(),
$this->hashPassword(Uuid::uuid4()),
$request->gender,
Expand All @@ -83,6 +88,7 @@ public function createFromRenaissanceMembershipRequest(MembershipRequest $member
{
$adherent = Adherent::create(
uuid: Uuid::uuid4(),
publicId: $this->generatePublicId(),
emailAddress: $membershipRequest->email,
password: null,
gender: $membershipRequest->civility,
Expand All @@ -102,6 +108,7 @@ public function createFromBesoinDEuropeMembershipRequest(InscriptionRequest $ins
{
$adherent = Adherent::create(
uuid: Uuid::uuid4(),
publicId: $this->generatePublicId(),
emailAddress: $inscriptionRequest->email,
password: null,
gender: $inscriptionRequest->civility,
Expand All @@ -128,6 +135,7 @@ public function createFromAdminAdherentCreateCommand(
Administrator $administrator,
): Adherent {
$adherent = Adherent::createBlank(
$this->generatePublicId(),
$command->gender,
$command->firstName,
$command->lastName,
Expand Down Expand Up @@ -155,6 +163,7 @@ public function createFromArray(array $data): Adherent

return Adherent::create(
isset($data['uuid']) ? Uuid::fromString($data['uuid']) : Adherent::createUuid($data['email']),
$data['public_id'] ?? $this->generatePublicId(),
$data['email'],
$this->hashPassword($data['password']),
$data['gender'] ?? null,
Expand Down Expand Up @@ -191,4 +200,9 @@ private function hashPassword(string $password): string
{
return $this->hasher->hash($password);
}

private function generatePublicId(): string
{
return $this->publicIdGenerator->generate();
}
}
4 changes: 3 additions & 1 deletion src/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ public function searchAllEvents(SearchParametersFilter $search): array
committees.address_address AS committee_address_address, committees.address_country AS committee_address_country,
committees.address_city_name AS committee_address_city_name, committees.address_city_insee AS committee_address_city_insee,
committees.address_postal_code AS committee_address_postal_code, committees.address_latitude AS committee_address_latitude,
committees.address_longitude AS committee_address_longitude, adherents.uuid AS adherent_uuid,
committees.address_longitude AS committee_address_longitude,
adherents.uuid AS adherent_uuid,
adherents.public_id AS adherent_public_id,
adherents.email_address AS adherent_email_address,
adherents.gender AS adherent_gender, adherents.first_name AS adherent_first_name,
adherents.last_name AS adherent_last_name, adherents.birthdate AS adherent_birthdate,
Expand Down
30 changes: 0 additions & 30 deletions src/Utils/PublicIdGenerator.php

This file was deleted.

1 change: 1 addition & 0 deletions tests/Committee/CommitteeAdherentMandateManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/Committee/CommitteeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testCreateCommitteeFromCommitteeCreationCommand()

$adherent = Adherent::create(
$uuid,
'ABC-234',
$email,
'password',
'male',
Expand Down
1 change: 1 addition & 0 deletions tests/Donation/DonationRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function testCreateDonationRequestFromAdherent()

$adherent = Adherent::create(
$uuid,
'ABC-234',
$email,
'password',
'male',
Expand Down
1 change: 1 addition & 0 deletions tests/Entity/AdherentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ private function createNewAdherent(

return Adherent::create(
Adherent::createUuid($email),
'ABC-234',
$email,
'super-password',
'male',
Expand Down
1 change: 1 addition & 0 deletions tests/Entity/CommitteeMembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private function createNewAdherent(): Adherent
{
return Adherent::create(
Uuid::fromString(self::ADHERENT_UUID),
'ABC-234',
'[email protected]',
'password',
'male',
Expand Down
1 change: 1 addition & 0 deletions tests/Entity/OAuth/UserAuthorizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function createUser(): Adherent
{
return Adherent::create(
Uuid::uuid4(),
'ABC-234',
'[email protected]',
'',
'male',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions tests/Security/AdherentLoginTimestampRecorderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private function createAdherent(): Adherent
{
return Adherent::create(
Adherent::createUuid('[email protected]'),
'ABC-234',
'[email protected]',
'super-password',
'male',
Expand Down
1 change: 1 addition & 0 deletions tests/TestHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ protected function createAdherent(?string $email = null): Adherent

return Adherent::create(
Adherent::createUuid($email),
'ABC-234',
$email,
'super-password',
'male',
Expand Down

0 comments on commit ea81d1b

Please sign in to comment.