/** * @return User */ public function getUser() { $user = new User(); try { $user->setUsername($this->variables[self::TV_USERNAME]); } catch (\InvalidArgumentException $e) { $length = $e->getMessage(); switch ($e->getCode()) { case User::TOO_SHORT: $this->addError("The username is too short, a minimum of {$length} characters is required"); break; case User::TOO_LONG: $this->addError("The username is too long, a maximum of {$length} characters is required"); break; } } if ($this->variables[self::TV_PASSWORD] !== $this->variables[self::TV_PASSWORD2]) { $this->addError('The passwords does not match'); } else { try { $user->setPassword($this->variables[self::TV_PASSWORD]); } catch (\InvalidArgumentException $e) { $this->addError('The password can not be empty'); } } return $user; }
/** * @param User $user * @throws \InvalidArgumentException if user isn't valid * @throws \DomainException if username is taken */ public function create(User $user) { if (!$user->isValid()) { throw new \InvalidArgumentException('User is not valid'); } if ($this->getByUsername($user->getUsername())) { throw new \DomainException('Username is taken'); } $this->database->insert($user); }
/** * @param User $user * @return Diagram[] All diagrams the user have saved */ public function getByUser(User $user) { return $this->database->select(Diagram::class, '`userId` = ?', [$user->getId()]); }
/** * @param User $user */ public function login(User $user) { $this->user = $user; $this->setSession($user->getUsername()); }
public function __construct($id, User $user) { $this->id = $id; $this->userId = $user->getId(); }