Completed
Push — master ( 41e944...02198f )
by Stanislav
02:22
created

TorrentRemove   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 27.27 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 6
dl 15
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 15 15 1
B execute() 0 36 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Popstas\Transmission\Console\Command;
4
5
use Martial\Transmission\API\Argument\Torrent;
6
use Popstas\Transmission\Console\Helpers\TorrentUtils;
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\Console\Question\ConfirmationQuestion;
12
13
class TorrentRemove extends Command
14
{
15 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
    {
17
        parent::configure();
18
        $this
19
            ->setName('torrent-remove')
20
            ->setAliases(['tr'])
21
            ->setDescription('Remove torrents')
22
            ->addArgument('torrent-ids', InputArgument::IS_ARRAY, 'List of torrent ids for remove')
23
            ->addOption('soft', null, InputOption::VALUE_NONE, 'Remove only from Transmission, not delete data')
24
            ->addOption('yes', 'y', InputOption::VALUE_NONE, 'Don\'t ask confirmation')
25
            ->setHelp(<<<EOT
26
The <info>torrent-remove</info> removes torrents torrents.
27
EOT
28
            );
29
    }
30
31
    protected function execute(InputInterface $input, OutputInterface $output)
32
    {
33
        $client = $this->getApplication()->getClient();
34
        $torrentIds = $input->getArgument('torrent-ids');
35
        $torrentList = $client->getTorrentData($torrentIds);
36
        $notExistsIds = array_diff($torrentIds, TorrentUtils::getTorrentsField($torrentList, Torrent\Get::ID));
37
38
        if (count($notExistsIds)) {
39
            foreach ($notExistsIds as $notExistsId) {
40
                $output->writeln($notExistsId . ' not exists');
41
            }
42
            return 1;
43
        }
44
45
        $output->writeln('Torrents for remove:');
46
        TorrentUtils::printTorrentsTable($torrentList, $output);
47
48
        if (!$input->getOption('yes')) {
49
            $helper = $this->getHelper('question');
50
            $question = new ConfirmationQuestion('Continue with this action? ', false);
51
            if (!$helper->ask($input, $output, $question)) {
52
                $output->writeln('Aborting.');
53
                return 1;
54
            }
55
        }
56
57
        $deleteLocalData = !$input->getOption('soft');
58
        $client->removeTorrents($torrentList, $deleteLocalData);
59
60
        $output->writeln('Torrents removed.');
61
        if (!$deleteLocalData) {
62
            $output->writeln('Data don\'t removed.');
63
        }
64
        
65
        return 0;
66
    }
67
}
68