Completed
Push — master ( 2515f9...556e6f )
by Stanislav
03:05
created

TorrentRemove::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 36
rs 8.8571
cc 1
eloc 12
nc 1
nop 0
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
        if (!$input->getOption('yes')) {
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
            $influxDbClient = $this->getApplication()->getInfluxDbClient(
82
                $config->get('influxdb-host'),
83
                $config->get('influxdb-port'),
84
                $config->get('influxdb-user'),
85
                $config->get('influxdb-password'),
86
                $config->get('influxdb-database')
87
            );
88
            $transmissionHost = $config->overrideConfig($input, 'transmission-host');
89
            $influxDbClient->sendTorrentPoints($torrentList, $transmissionHost);
90
91
            $deleteLocalData = !$input->getOption('soft');
92
            $client->removeTorrents($torrentList, $deleteLocalData);
93
            $output->writeln('Torrents removed.');
94
            if (!$deleteLocalData) {
95
                $output->writeln('Data don\'t removed.');
96
            }
97
        }, 'dry-run, don\'t really remove torrents');
98
99
        return 0;
100
    }
101
}
102