Completed
Push — master ( 27dee6...22d68a )
by Stanislav
05:48
created

src/Command/TorrentRemove.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
            $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->get('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