Fixing circular dependency in UserFactory.

This commit is contained in:
Alexey Skobkin 2019-02-23 21:10:53 +03:00
parent 26ee4522fc
commit 05aaa1d4e1

View file

@ -7,7 +7,6 @@ use Skobkin\Bundle\PointToolsBundle\DTO\Api\User as UserDTO;
use Skobkin\Bundle\PointToolsBundle\Entity\User; use Skobkin\Bundle\PointToolsBundle\Entity\User;
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository; use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository;
use Skobkin\Bundle\PointToolsBundle\Exception\Factory\InvalidUserDataException; use Skobkin\Bundle\PointToolsBundle\Exception\Factory\InvalidUserDataException;
use Skobkin\Bundle\PointToolsBundle\Service\Api\UserApi;
class UserFactory extends AbstractFactory class UserFactory extends AbstractFactory
{ {
@ -16,29 +15,10 @@ class UserFactory extends AbstractFactory
/** @var UserRepository */ /** @var UserRepository */
private $userRepository; private $userRepository;
/** @var UserApi */ public function __construct(LoggerInterface $logger, UserRepository $userRepository)
private $userApi;
public function __construct(LoggerInterface $logger, UserRepository $userRepository, UserApi $userApi)
{ {
parent::__construct($logger); parent::__construct($logger);
$this->userRepository = $userRepository; $this->userRepository = $userRepository;
$this->userApi = $userApi;
}
public function findOrCreateByLogin(string $login, bool $retrieveMissingFromApi = true): User
{
/** @var User $user */
if (null === $user = $this->userRepository->findBy(['login' => $login])) {
if ($retrieveMissingFromApi) {
$user = $this->userApi->getUserByLogin($login);
} else {
// TODO neen more specific exception
throw new \RuntimeException(sprintf('User \'%s\' not found in the database. Api retrieval disabled.', $login));
}
}
return $user;
} }
/** /**
@ -60,7 +40,7 @@ class UserFactory extends AbstractFactory
if (null === ($user = $this->userRepository->find($userData->getId()))) { if (null === ($user = $this->userRepository->find($userData->getId()))) {
$user = new User( $user = new User(
$userData->getId(), $userData->getId(),
\DateTime::createFromFormat('Y-m-d_H:i:s', $userData->getCreated()) ?: new \DateTime() \DateTime::createFromFormat(self::DATE_FORMAT, $userData->getCreated()) ?: new \DateTime()
); );
$this->userRepository->add($user); $this->userRepository->add($user);
} }