Passed
Push — master ( b4ecbf...2b21cc )
by Kevin
05:08 queued 10s
created

ImportCommand::execute()   C

Complexity

Conditions 11
Paths 31

Size

Total Lines 72
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 51
c 1
b 0
f 0
nc 31
nop 2
dl 0
loc 72
rs 6.9224

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Wallabag\ImportBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Config\Definition\Exception\Exception;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
12
13
class ImportCommand extends ContainerAwareCommand
14
{
15
    protected function configure()
16
    {
17
        $this
18
            ->setName('wallabag:import')
19
            ->setDescription('Import entries from a JSON export')
20
            ->addArgument('username', InputArgument::REQUIRED, 'User to populate')
21
            ->addArgument('filepath', InputArgument::REQUIRED, 'Path to the JSON file')
22
            ->addOption('importer', null, InputOption::VALUE_OPTIONAL, 'The importer to use: v1, v2, instapaper, pinboard, readability, firefox or chrome', 'v1')
23
            ->addOption('markAsRead', null, InputOption::VALUE_OPTIONAL, 'Mark all entries as read', false)
24
            ->addOption('useUserId', null, InputOption::VALUE_NONE, 'Use user id instead of username to find account')
25
            ->addOption('disableContentUpdate', null, InputOption::VALUE_NONE, 'Disable fetching updated content from URL')
26
        ;
27
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output)
30
    {
31
        $output->writeln('Start : ' . (new \DateTime())->format('d-m-Y G:i:s') . ' ---');
32
33
        if (!file_exists($input->getArgument('filepath'))) {
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('filepath') can also be of type string[]; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        if (!file_exists(/** @scrutinizer ignore-type */ $input->getArgument('filepath'))) {
Loading history...
34
            throw new Exception(sprintf('File "%s" not found', $input->getArgument('filepath')));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('filepath') can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
            throw new Exception(sprintf('File "%s" not found', /** @scrutinizer ignore-type */ $input->getArgument('filepath')));
Loading history...
35
        }
36
37
        $em = $this->getContainer()->get('doctrine')->getManager();
38
        // Turning off doctrine default logs queries for saving memory
39
        $em->getConnection()->getConfiguration()->setSQLLogger(null);
40
41
        if ($input->getOption('useUserId')) {
42
            $entityUser = $em->getRepository('WallabagUserBundle:User')->findOneById($input->getArgument('username'));
43
        } else {
44
            $entityUser = $em->getRepository('WallabagUserBundle:User')->findOneByUsername($input->getArgument('username'));
45
        }
46
47
        if (!\is_object($entityUser)) {
48
            throw new Exception(sprintf('User "%s" not found', $input->getArgument('username')));
49
        }
50
51
        // Authenticate user for paywalled websites
52
        $token = new UsernamePasswordToken(
53
            $entityUser,
54
            null,
55
            'main',
56
            $entityUser->getRoles());
57
58
        $this->getContainer()->get('security.token_storage')->setToken($token);
59
        $user = $this->getContainer()->get('security.token_storage')->getToken()->getUser();
60
61
        switch ($input->getOption('importer')) {
62
            case 'v2':
63
                $import = $this->getContainer()->get('wallabag_import.wallabag_v2.import');
64
                break;
65
            case 'firefox':
66
                $import = $this->getContainer()->get('wallabag_import.firefox.import');
67
                break;
68
            case 'chrome':
69
                $import = $this->getContainer()->get('wallabag_import.chrome.import');
70
                break;
71
            case 'readability':
72
                $import = $this->getContainer()->get('wallabag_import.readability.import');
73
                break;
74
            case 'instapaper':
75
                $import = $this->getContainer()->get('wallabag_import.instapaper.import');
76
                break;
77
            case 'pinboard':
78
                $import = $this->getContainer()->get('wallabag_import.pinboard.import');
79
                break;
80
            default:
81
                $import = $this->getContainer()->get('wallabag_import.wallabag_v1.import');
82
        }
83
84
        $import->setMarkAsRead($input->getOption('markAsRead'));
85
        $import->setDisableContentUpdate($input->getOption('disableContentUpdate'));
86
        $import->setUser($user);
87
88
        $res = $import
89
            ->setFilepath($input->getArgument('filepath'))
90
            ->import();
91
92
        if (true === $res) {
93
            $summary = $import->getSummary();
94
            $output->writeln('<info>' . $summary['imported'] . ' imported</info>');
95
            $output->writeln('<comment>' . $summary['skipped'] . ' already saved</comment>');
96
        }
97
98
        $em->clear();
99
100
        $output->writeln('End : ' . (new \DateTime())->format('d-m-Y G:i:s') . ' ---');
101
    }
102
}
103