Skip to content

Commit

Permalink
CmsUser: Constructor is not mandatory dependency in User inteface (be…
Browse files Browse the repository at this point in the history
…cause it is bug in Doctrine 2.0, but will be solved in Doctrine 3.0). Add new injectDefault() method. Thanks @Langriklol
  • Loading branch information
janbarasek committed Aug 18, 2021
1 parent ef63fd1 commit a5680e3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Api/UserEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,13 @@ public function createDefault(
/** @phpstan-ignore-next-line */
$ref = new \ReflectionClass($this->userManager->getDefaultEntity());
/** @var CmsUser $user */
$user = $ref->newInstanceArgs([$email, $password ?: '', $email, CmsUser::ROLE_USER]);
$user = $ref->newInstanceWithoutConstructor();
$user->injectDefault(
username: $email,
password: $password ?: '',
email: $email,
role: CmsUser::ROLE_USER,
);
} catch (\Throwable $e) {
if (class_exists(Debugger::class)) {
Debugger::log($e, ILogger::CRITICAL);
Expand Down
5 changes: 4 additions & 1 deletion src/User/Entity/CmsUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ interface CmsUser extends IIdentity
{
public const ROLE_USER = 'user';

public function __construct(string $username, string $password, string $email, string $role = self::ROLE_USER);
// Constructor will be reverted in Doctrine 3.0
// public function __construct(string $username, string $password, string $email, string $role = self::ROLE_USER);

public function injectDefault(string $username, string $password, string $email, string $role = self::ROLE_USER);

/**
* @return string[]
Expand Down
7 changes: 7 additions & 0 deletions src/User/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ class User implements CmsUser


public function __construct(string $username, string $password, string $email, string $role = CmsUser::ROLE_USER)
{
// back compatibility
$this->injectDefault($username, $password, $email, $role);
}


public function injectDefault(string $username, string $password, string $email, string $role = self::ROLE_USER)
{
$this->username = trim(Strings::lower($username));
$this->password = $password
Expand Down

0 comments on commit a5680e3

Please sign in to comment.