Completed
Push — master ( df650f...2515f9 )
by Stanislav
07:24
created

TorrentAdd   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 5
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 20 1
A execute() 0 14 2
A addFile() 0 6 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\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class TorrentAdd extends Command
12
{
13
    protected function configure()
14
    {
15
        parent::configure();
16
        $this
17
            ->setName('torrent-add')
18
            ->setAliases(['ta'])
19
            ->setDescription('Remove torrents')
20
            ->addArgument('torrent-files', InputArgument::IS_ARRAY, 'List of torrent files to add')
21
            ->setHelp(<<<EOT
22
By default, Transmission may to freeze if you add several torrents at same time.
23
Therefore, preferred way to add torrents - with `torrent-add`.
24
After each add file command sleeps for 10 seconds for give time to freeze Transmission.
25
After that command waits for Transmission answer and add next file, etc.
26
27
```
28
transmission-cli torrent-add file|url [file2] [fileX]
29
```
30
EOT
31
            );
32
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        $client = $this->getApplication()->getClient();
37
38
        $torrentFiles = $input->getArgument('torrent-files');
39
40
        foreach ($torrentFiles as $torrentFile) {
41
            $this->addFile($input, $output, $client, $torrentFile);
42
            $output->writeln($torrentFile . ' added. Waiting for Transmission...');
43
            $client->waitForTransmission(10);
44
        }
45
46
        $output->writeln('All torrents added.');
47
    }
48
49
    private function addFile(InputInterface $input, OutputInterface $output, TransmissionClient $client, $torrentFile)
50
    {
51
        $this->dryRun($input, $output, function () use ($torrentFile, $client, $input, $output) {
52
            $client->addTorrent($torrentFile);
53
        }, 'dry-run, don\'t really add torrents');
54
    }
55
}
56