Completed
Push — master ( e1baba...a0d507 )
by Stanislav
02:30
created

TorrentList   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 103
Duplicated Lines 70.87 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 14
Bugs 0 Features 5
Metric Value
wmc 2
c 14
b 0
f 5
lcom 0
cbo 6
dl 73
loc 103
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 73 73 1
B execute() 0 26 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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