WeburgSeriesAdd   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 64
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 24 1
A execute() 0 35 4
1
<?php
2
3
namespace Popstas\Transmission\Console\Command;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class WeburgSeriesAdd extends Command
10
{
11
12
    protected function configure()
13
    {
14
        parent::configure();
15
        $this
16
            ->setName('weburg-series-add')
17
            ->setAliases(['wsa'])
18
            ->setDescription('Add series to monitoring list')
19
            ->addArgument('series-id', InputArgument::REQUIRED, 'series id or series url')
20
            ->setHelp(<<<EOT
21
## Add series to download list
22
23
You can automatically download new series. To do this, you should add series to download list:
24
```
25
transmission-cli weburg-series-add http://weburg.net/series/info/12345
26
```
27
28
After that command `weburg-download` also will download series from list for last day.
29
If you don't want to download popular torrents, but only new series, use command:
30
```
31
transmission-cli weburg-download --series
32
```
33
EOT
34
            );
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $config = $this->getApplication()->getConfig();
40
41
        $weburgClient = $this->getApplication()->getWeburgClient();
42
43
        $seriesArgument = $input->getArgument('series-id');
44
        $seriesId = $weburgClient->cleanMovieId($seriesArgument);
45
46
        if (!$seriesId) {
47
            $output->writeln($seriesArgument . ' seems not weburg series url');
48
            return 1;
49
        }
50
51
        $seriesList = $config->get('weburg-series-list');
52
        if (!empty(array_filter($seriesList, function ($item) use ($seriesId) {
53
            // can be array or id
54
            return is_array($item) ? $item['id'] == $seriesId : $item == $seriesId;
55
        }))) {
56
            $output->writeln($seriesId . ' already in list');
57
            return 0;
58
        }
59
60
        $seriesInfo = $weburgClient->getMovieInfoById($seriesId);
61
        $seriesList[] = [
62
            'id' => $seriesId,
63
            'title' => $seriesInfo['title']
64
        ];
65
66
        $config->set('weburg-series-list', $seriesList);
67
        $config->saveConfigFile();
68
69
        $output->writeln('Series ' . $seriesId . ' ' . $seriesInfo['title'] . ' added to list');
70
        return 0;
71
    }
72
}
73