Use client id max length as substr length

This commit is contained in:
dinamiko 2022-01-24 16:21:41 +01:00
parent 82727ac2a1
commit f39ae459b1

View file

@ -38,14 +38,13 @@ class CustomerRepository {
* @return string
*/
public function customer_id_for_user( int $user_id ): string {
$unique_id = $this->generate_unique_id();
if ( 0 === $user_id ) {
$guest_customer_id = WC()->session->get( 'ppcp_guest_customer_id' );
if ( is_string( $guest_customer_id ) && $guest_customer_id ) {
return $guest_customer_id;
}
$unique_id = $this->generate_unique_id();
$unique_id = substr( $this->prefix . strrev( uniqid() ), 0, self::CLIENT_ID_MAX_LENGTH );
WC()->session->set( 'ppcp_guest_customer_id', $unique_id );
return $unique_id;
@ -58,17 +57,4 @@ class CustomerRepository {
return $this->prefix . (string) $user_id;
}
/**
* Generates a unique id based on the length of the prefix.
*
* @return string
*/
protected function generate_unique_id(): string {
$offset = self::CLIENT_ID_MAX_LENGTH - strlen( $this->prefix );
return strlen( uniqid() ) > $offset
? $this->prefix . substr( uniqid(), $offset )
: $this->prefix . uniqid();
}
}