TorrentAdd   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 23 1
A execute() 0 16 3
A addFile() 0 14 3
A removeDuplicates() 0 14 1
1
<?php
2
3
namespace Popstas\Transmission\Console\Command;
4
5
use Popstas\Transmission\Console\TransmissionClient;
6
use Symfony\Component\Console\Input\ArrayInput;
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
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
            ->addOption('yes', 'y', InputOption::VALUE_NONE, 'Don\'t ask confirmation')
22
            ->addArgument('torrent-files', InputArgument::IS_ARRAY, 'List of torrent files to add')
23
            ->setHelp(<<<EOT
24
## Add torrents
25
26
By default, Transmission may to freeze if you add several torrents at same time.
27
Therefore, preferred way to add torrents - with `torrent-add`.
28
After each add file command sleeps for 10 seconds for give time to freeze Transmission.
29
After that command waits for Transmission answer and add next file, etc.
30
31
```
32
transmission-cli torrent-add file|url [file2] [fileX]
33
```
34
EOT
35
            );
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $client = $this->getApplication()->getClient();
41
        $config = $this->getApplication()->getConfig();
42
43
        $torrentFiles = $input->getArgument('torrent-files');
44
        foreach ($torrentFiles as $torrentFile) {
45
            $this->addFile($input, $output, $client, $torrentFile);
46
        }
47
48
        $output->writeln('All torrents added.');
49
50
        if (!$config->get('allow-duplicates')) {
51
            $this->removeDuplicates($input, $output);
52
        }
53
    }
54
55
    private function addFile(InputInterface $input, OutputInterface $output, TransmissionClient $client, $torrentFile)
56
    {
57
        $this->dryRun($input, $output, function () use ($torrentFile, $client, $input, $output) {
58
            $torrentAdded = $client->addTorrent($torrentFile);
59
            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...
60
                if (isset($torrentAdded['duplicate'])) {
61
                    $output->writeln($torrentFile . ' was not added. Probably it was added before.');
62
                } else {
63
                    $output->writeln($torrentFile . ' added. Waiting for Transmission...');
64
                    $client->waitForTransmission(10);
65
                }
66
            }
67
        }, 'dry-run, don\'t really add torrents');
68
    }
69
70
    private function removeDuplicates(InputInterface $input, OutputInterface $output)
71
    {
72
        $config = $this->getApplication()->getConfig();
73
74
        $command = $this->getApplication()->find('torrent-remove-duplicates');
75
        $arguments = array(
76
            'command'             => 'torrent-remove-duplicates',
77
            '--transmission-host' => $config->get('transmission-host'),
78
            '--yes'               => $input->getOption('yes'),
79
            '--dry-run'           => $input->getOption('dry-run')
80
        );
81
        $commandInput = new ArrayInput($arguments);
82
        $command->run($commandInput, $output);
83
    }
84
}
85