Completed
Push — master ( ac84ef...df650f )
by Stanislav
02:18
created

TorrentRemoveDuplicates::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 0
loc 32
rs 8.8571
cc 2
eloc 24
nc 2
nop 2
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\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class TorrentRemoveDuplicates extends Command
11
{
12
    protected function configure()
13
    {
14
        parent::configure();
15
        $this
16
            ->setName('torrent-remove-duplicates')
17
            ->setAliases(['trd'])
18
            ->setDescription('Remove duplicates obsolete torrents')
19
            ->setHelp(<<<EOT
20
The <info>torrent-remove-duplicates</info> removed all torrents with same name and smaller size than other.
21
EOT
22
            );
23
    }
24
25
    protected function execute(InputInterface $input, OutputInterface $output)
26
    {
27
        $config = $this->getApplication()->getConfig();
28
        $client = $this->getApplication()->getClient();
29
30
        $torrentList = $client->getTorrentData();
31
        $obsoleteList = TorrentListUtils::getObsoleteTorrents($torrentList);
32
        if (empty($obsoleteList)) {
33
            $output->writeln('There are no obsolete torrents in Transmission.');
34
            return 0;
35
        }
36
37
        $this->dryRun($input, $output, function () use ($client, $obsoleteList, $config, $input, $output) {
38
            $influxDbClient = $this->getApplication()->getInfluxDbClient(
39
                $config->get('influxdb-host'),
40
                $config->get('influxdb-port'),
41
                $config->get('influxdb-user'),
42
                $config->get('influxdb-password'),
43
                $config->get('influxdb-database')
44
            );
45
            $transmissionHost = $config->overrideConfig($input, 'transmission-host');
46
            $influxDbClient->sendTorrentPoints($obsoleteList, $transmissionHost);
47
48
            $client->removeTorrents($obsoleteList);
49
            $names = TorrentListUtils::getArrayField($obsoleteList, Torrent\Get::NAME);
50
            $output->writeln('Removed torrents:' . implode(', ', $names));
51
        }, 'dry-run, don\'t really remove');
52
53
        $output->writeln('Found and deleted ' . count($obsoleteList) . ' obsolete torrents from transmission:');
54
        TorrentListUtils::printTorrentsTable($obsoleteList, $output);
55
        return 0;
56
    }
57
}
58