3 namespace Wallabag\FederationBundle\Command
;
5 use Doctrine\ORM\NoResultException
;
6 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
;
7 use Symfony\Component\Console\Input\InputArgument
;
8 use Symfony\Component\Console\Input\InputInterface
;
9 use Symfony\Component\Console\Output\OutputInterface
;
10 use Wallabag\FederationBundle\Entity\Account
;
11 use Wallabag\FederationBundle\Entity\Instance
;
12 use Wallabag\UserBundle\Entity\User
;
14 class CreateAccountsCommand
extends ContainerAwareCommand
16 /** @var OutputInterface */
19 protected $created = 0;
21 protected function configure()
24 ->setName('wallabag:federation:create-accounts')
25 ->setDescription('Creates missing federation accounts')
26 ->setHelp('This command creates accounts for federation for missing users')
29 InputArgument
::OPTIONAL
,
30 'User to create an account for'
34 protected function execute(InputInterface
$input, OutputInterface
$output)
36 $this->output
= $output;
38 $domainName = $this->getContainer()->getParameter('domain_name');
39 $instance = $this->checkInstance($domainName);
41 $username = $input->getArgument('username');
45 $user = $this->getUser($username);
46 $this->createAccount($user, $instance);
47 } catch (NoResultException
$e) {
48 $output->writeln(sprintf('<error>User "%s" not found.</error>', $username));
53 $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll();
55 $output->writeln(sprintf('Creating through %d user federated accounts', count($users)));
57 foreach ($users as $user) {
58 $output->writeln(sprintf('Processing user %s', $user->getUsername()));
59 $this->createAccount($user, $instance);
61 $output->writeln(sprintf('Creating user federated accounts. %d accounts created in total', $this->created
));
71 private function createAccount(User
$user, Instance
$instance)
73 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
74 $repo = $em->getRepository('WallabagFederationBundle:Account');
76 if ($repo->findBy(['user' => $user->getId()])) {
80 $account = new Account();
81 $account->setUsername($user->getUsername())
83 ->setServer($instance);
85 $em->persist($account);
88 $user->setAccount($account);
89 $em->persist($account);
95 private function checkInstance($domainName)
97 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
98 $repo = $em->getRepository('WallabagFederationBundle:Instance');
100 $instance = $repo->findOneByDomain($domainName);
102 $instance = new Instance($domainName);
104 $em->persist($instance);
111 * Fetches a user from its username.
113 * @param string $username
115 * @return \Wallabag\UserBundle\Entity\User
117 private function getUser($username)
119 return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username);
122 private function getDoctrine()
124 return $this->getContainer()->get('doctrine');