Completed
Push — master ( a0d507...b6b7ad )
by Stanislav
02:33
created

TorrentAdd::removeDuplicates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
namespace Popstas\Transmission\Console\Command;
4
5
use Martial\Transmission\API\Argument\Torrent;
6
use Popstas\Transmission\Console\TransmissionClient;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class TorrentAdd extends Command
13
{
14
    protected function configure()
15
    {
16
        parent::configure();
17
        $this
18
            ->setName('torrent-add')
19
            ->setAliases(['ta'])
20
            ->setDescription('Add torrents to Transmission')
21
            ->addArgument('torrent-files', InputArgument::IS_ARRAY, 'List of torrent files to add')
22
            ->setHelp(<<<EOT
23
## Add torrents
24
25
By default, Transmission may to freeze if you add several torrents at same time.
26
Therefore, preferred way to add torrents - with `torrent-add`.
27
After each add file command sleeps for 10 seconds for give time to freeze Transmission.
28
After that command waits for Transmission answer and add next file, etc.
29
30
```
31
transmission-cli torrent-add file|url [file2] [fileX]
32
```
33
EOT
34
            );
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $client = $this->getApplication()->getClient();
40
41
        $torrentFiles = $input->getArgument('torrent-files');
42
        foreach ($torrentFiles as $torrentFile) {
43
            $this->addFile($input, $output, $client, $torrentFile);
44
        }
45
46
        $output->writeln('All torrents added.');
47
48
        $this->removeDuplicates($output);
49
    }
50
51
    private function addFile(InputInterface $input, OutputInterface $output, TransmissionClient $client, $torrentFile)
52
    {
53
        $this->dryRun($input, $output, function () use ($torrentFile, $client, $input, $output) {
54
            $torrentAdded = $client->addTorrent($torrentFile);
55
            if ($torrentAdded) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $torrentAdded of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
56
                $output->writeln($torrentFile . ' added. Waiting for Transmission...');
57
                $client->waitForTransmission(10);
58
            } else {
59
                $output->writeln($torrentFile . ' was not added. Probably it was added before.');
60
            }
61
        }, 'dry-run, don\'t really add torrents');
62
    }
63
64
    private function removeDuplicates(OutputInterface $output)
65
    {
66
        $config = $this->getApplication()->getConfig();
67
68
        $command = $this->getApplication()->find('torrent-remove-duplicates');
69
        $arguments = array(
70
            'command'             => 'torrent-remove-duplicates',
71
            '--transmission-host' => $config->get('transmission-host')
72
        );
73
        $commandInput = new ArrayInput($arguments);
74
        $command->run($commandInput, $output);
75
    }
76
}
77