]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Command/ExportCommand.php
Fix phpcs and tests
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / ExportCommand.php
CommitLineData
8303b037
TC
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Doctrine\ORM\NoResultException;
6use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7use Symfony\Component\Console\Input\InputArgument;
8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Output\OutputInterface;
8303b037
TC
10
11class ExportCommand extends ContainerAwareCommand
12{
13 protected function configure()
14 {
15 $this
16 ->setName('wallabag:export')
17 ->setDescription('Export all entries for an user')
18 ->setHelp('This command helps you to export all entries for an user')
19 ->addArgument(
20 'username',
21 InputArgument::REQUIRED,
22 'User from which to export entries'
23 )
24 ->addArgument(
a607b7a9 25 'filepath',
8303b037
TC
26 InputArgument::OPTIONAL,
27 'Path of the exported file'
28 )
29 ;
30 }
31
32 protected function execute(InputInterface $input, OutputInterface $output)
33 {
34 try {
3b0380f0 35 $user = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($input->getArgument('username'));
8303b037
TC
36 } catch (NoResultException $e) {
37 $output->writeln(sprintf('<error>User "%s" not found.</error>', $input->getArgument('username')));
3b0380f0 38
8303b037
TC
39 return 1;
40 }
3b0380f0 41
8303b037
TC
42 $entries = $this->getDoctrine()
43 ->getRepository('WallabagCoreBundle:Entry')
44 ->getBuilderForAllByUser($user->getId())
45 ->getQuery()
46 ->getResult();
47
48 $output->write(sprintf('Exporting %d entrie(s) for user « <comment>%s</comment> »... ', count($entries), $user->getUserName()));
49
a607b7a9 50 $filePath = $input->getArgument('filepath');
3b0380f0 51
8303b037 52 if (!$filePath) {
3b0380f0 53 $filePath = $this->getContainer()->getParameter('kernel.root_dir').'/../'.sprintf('%s-export.json', $user->getUsername());
8303b037 54 }
3b0380f0 55
8303b037
TC
56 try {
57 $data = $this->getContainer()->get('wallabag_core.helper.entries_export')
58 ->setEntries($entries)
59 ->updateTitle('All')
60 ->exportJsonData();
61 file_put_contents($filePath, $data);
62 } catch (\InvalidArgumentException $e) {
63 $output->writeln(sprintf('<error>Error: "%s"</error>', $e->getMessage()));
3b0380f0
TC
64
65 return 1;
8303b037
TC
66 }
67
68 $output->writeln('<info>Done.</info>');
8303b037 69
3b0380f0 70 return 0;
8303b037
TC
71 }
72
73 private function getDoctrine()
74 {
75 return $this->getContainer()->get('doctrine');
76 }
77}