TorrentList::configure()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 8.589
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Popstas\Transmission\Console\Helpers\TorrentUtils;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class TorrentList extends Command
13
{
14
    protected function configure()
15
    {
16
        parent::configure();
17
        $this
18
            ->setName('torrent-list')
19
            ->setAliases(['tl'])
20
            ->setDescription('List torrents')
21
            ->addOption('name', null, InputOption::VALUE_OPTIONAL, 'Sort by torrent name (regexp)')
22
            ->addOption('age', null, InputOption::VALUE_OPTIONAL, 'Sort by torrent age, ex. \'>1 <5\'')
23
            ->addOption('sort', null, InputOption::VALUE_OPTIONAL, 'Sort by column number', 4)
24
            ->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'Limit torrent list')
25
            ->setHelp(<<<EOT
26
## List torrents
27
28
You can list torrents from transmission with `torrent-list` command:
29
```
30
transmission-cli torrent-list [--sort=column_number] [--name='name'] [--age='>0'] [--limit=10]
31
```
32
33
**List columns:**
34
35
- Name
36
- Id - need for `torrent-remove` command
37
- Age - days from torrent done date
38
- Size - size of downloaded data
39
- Uploaded - size of uploaded data
40
- Per day - average uploaded GB per day
41
42
43
#### Sorting list
44
45
You can sort torrents by `Per day` column and estimate unpopular torrents:
46
```
47
transmission-cli torrent-list --sort=6
48
```
49
50
For reverse sort ordering, add `-` to column number:
51
```
52
transmission-cli torrent-list --sort=-6
53
```
54
55
56
#### Filtering torrent list
57
58
**By age:**
59
```
60
transmission-cli torrent-list --age '>10'
61
transmission-cli torrent-list --age '< 20'
62
transmission-cli torrent-list --age '>0 <5'
63
```
64
65
**By name:**
66
You can use simple regular expression except `.` and `/` symbols.
67
68
Filter FullHD series:
69
```
70
transmission-cli torrent-list --name 'season*1080*'
71
```
72
73
Filter all mkv and avi:
74
```
75
transmission-cli torrent-list --name '.mkv|.avi'
76
```
77
78
#### Limiting torrent list
79
80
Output 10 worst torrents:
81
```
82
transmission-cli torrent-list --sort=6 --limit 10
83
```
84
EOT
85
            );
86
    }
87
88
    protected function execute(InputInterface $input, OutputInterface $output)
89
    {
90
        $client = $this->getApplication()->getClient();
91
92
        $torrentList = $client->getTorrentData();
93
94
        $torrentList = array_map(function ($torrent) {
95
            $torrent['age'] = TorrentUtils::getTorrentAgeInDays($torrent);
96
            return $torrent;
97
        }, $torrentList);
98
99
        $torrentList = TorrentListUtils::filterTorrents($torrentList, [
100
            'age' => $input->getOption('age'),
101
            'name' => $input->getOption('name'),
102
        ]);
103
104
        TorrentListUtils::printTorrentsTable(
105
            $torrentList,
106
            $output,
107
            $input->getOption('sort'),
108
            $input->getOption('limit')
109
        );
110
111
        $freeSpace = $client->getFreeSpace();
112
        $output->writeln('Free space: ' . TorrentUtils::getSizeInGb($freeSpace) . ' GB');
113
    }
114
}
115