TorrentRemove   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 8.6 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 5
dl 8
loc 93
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 36 1
B execute() 8 53 7

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\TorrentListUtils;
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
    protected function configure()
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
## Remove torrents
27
28
Torrents removes only by id, you can see torrent id in `torrent-list` output.
29
30
By default torrents removes with data! Data deletes to trash on Mac OS and totally removes on Windows!
31
32
Remove one torrent:
33
```
34
transmission-cli torrent-remove 1
35
```
36
37
Remove several torrents:
38
```
39
transmission-cli torrent-remove 1 2
40
```
41
42
Remove only torrent from transmission, not delete data:
43
```
44
transmission-cli torrent-remove 1 --soft
45
```
46
47
For select not popular torrents and remove it, see `transmission-cli stats-get --help`
48
EOT
49
            );
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $client = $this->getApplication()->getClient();
55
        $config = $this->getApplication()->getConfig();
56
57
        $torrentIds = $input->getArgument('torrent-ids');
58
        $torrentList = $client->getTorrentData($torrentIds);
59
        $notExistsIds = array_diff($torrentIds, TorrentListUtils::getArrayField($torrentList, Torrent\Get::ID));
60
61
        if (count($notExistsIds)) {
62
            foreach ($notExistsIds as $notExistsId) {
63
                $output->writeln($notExistsId . ' not exists');
64
            }
65
            return 1;
66
        }
67
68
        $output->writeln('Torrents for remove:');
69
        TorrentListUtils::printTorrentsTable($torrentList, $output);
70
71 View Code Duplication
        if (!$input->getOption('yes')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
72
            $helper = $this->getHelper('question');
73
            $question = new ConfirmationQuestion('Continue with this action? ', false);
74
            if (!$helper->ask($input, $output, $question)) {
75
                $output->writeln('Aborting.');
76
                return 1;
77
            }
78
        }
79
80
        $this->dryRun($input, $output, function () use ($config, $torrentList, $client, $input, $output) {
81
            try {
82
                $influxDbClient = $this->getApplication()->getInfluxDbClient(
83
                    $config->get('influxdb-host'),
84
                    $config->get('influxdb-port'),
85
                    $config->get('influxdb-user'),
86
                    $config->get('influxdb-password'),
87
                    $config->get('influxdb-database')
88
                );
89
                $transmissionHost = $config->get('transmission-host');
90
                $influxDbClient->sendTorrentPoints($torrentList, $transmissionHost);
91
            } catch (\Exception $exception) {
92
                $output->writeln('InfluxDB not available');
93
            }
94
95
            $deleteLocalData = !$input->getOption('soft');
96
            $client->removeTorrents($torrentList, $deleteLocalData);
97
            $output->writeln('Torrents removed.');
98
            if (!$deleteLocalData) {
99
                $output->writeln('Data don\'t removed.');
100
            }
101
        }, 'dry-run, don\'t really remove torrents');
102
103
        return 0;
104
    }
105
}
106