entityManagerInterface = $entityManagerInterface; $this->parameterBagInterface = $parameterBagInterface; } public function init(string $address): User { // Return existing user if ($result = $this->entityManagerInterface ->getRepository(User::class) ->findOneByAddressField($address)) { return $result; } // Create new user $user = new User(); $user->setAddress($address); $user->setAdded(time()); $user->setApproved(false); $user->setModerator(false); $user->setStatus(true); $user->setLocale( $this->parameterBagInterface->get('app.locale') ); $user->setLocales( explode('|', $this->parameterBagInterface->get('app.locales')) ); $user->setTheme( $this->parameterBagInterface->get('app.theme') ); $this->save($user); // Set initial user as approved & moderator if (1 === $user->getId()) { $user->setApproved(true); $user->setModerator(true); $this->save($user); } // Return user data return $user; } public function getUser(int $userId): ?User { return $this->entityManagerInterface ->getRepository(User::class) ->getUser($userId); } public function identicon( mixed $value, int $size = 16, array $style = [ 'backgroundColor' => 'rgba(255, 255, 255, 0)', 'padding' => 0 ], string $format = 'webp' ): string { $identicon = new \Jdenticon\Identicon(); $identicon->setValue($value); $identicon->setSize($size); $identicon->setStyle($style); return $identicon->getImageDataUri($format); } public function save(User $user) : void // @TODO delete { $this->entityManagerInterface->persist($user); $this->entityManagerInterface->flush(); } // User star public function findUserStar( int $userId, int $userIdTarget ): ?UserStar { return $this->entityManagerInterface ->getRepository(UserStar::class) ->findUserStar($userId, $userIdTarget); } public function findUserStarsTotalByUserIdTarget(int $torrentId): int { return $this->entityManagerInterface ->getRepository(UserStar::class) ->findUserStarsTotalByUserIdTarget($torrentId); } public function toggleUserStar( int $userId, int $userIdTarget, int $added ): void { if ($userStar = $this->findUserStar($userId, $userIdTarget)) { $this->entityManagerInterface->remove($userStar); $this->entityManagerInterface->flush(); } else { $userStar = new UserStar(); $userStar->setUserId($userId); $userStar->setUserIdTarget($userIdTarget); $userStar->setAdded($added); $this->entityManagerInterface->persist($userStar); $this->entityManagerInterface->flush(); } } }