StatsSend::execute()   B
last analyzed

Complexity

Conditions 6
Paths 35

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 8.4032
c 0
b 0
f 0
cc 6
nc 35
nop 2

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 InfluxDB;
6
use Martial\Transmission\API\Argument\Torrent;
7
use Popstas\Transmission\Console\Helpers\TorrentListUtils;
8
use Popstas\Transmission\Console\Helpers\TorrentUtils;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class StatsSend extends Command
14
{
15
    protected function configure()
16
    {
17
        parent::configure();
18
        $this
19
            ->setName('stats-send')
20
            ->setAliases(['ss'])
21
            ->setDescription('Send metrics to InfluxDB')
22
            ->addOption('transmission-host', null, InputOption::VALUE_OPTIONAL, 'Transmission host')
23
            ->setHelp(<<<EOT
24
## Send statistic to InfluxDB
25
26
Command should called every hour or more, possible every day, but it will less precise.
27
28
Command assume that all torrents have unique names.
29
30
You should to configure InfluxDB to use this functional.
31
EOT
32
            );
33
    }
34
35
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        $config = $this->getApplication()->getConfig();
38
        $logger = $this->getApplication()->getLogger();
39
        $client = $this->getApplication()->getClient();
40
41
        $torrentList = $client->getTorrentData();
42
        if (!$config->get('allow-duplicates')) {
43
            $obsoleteList = TorrentListUtils::getObsoleteTorrents($torrentList);
44
            if (!empty($obsoleteList)) {
45
                $output->writeln('<comment>Found obsolete torrents, '
46
                                 . 'remove it using transmission-cli torrent-remove-duplicates</comment>');
47
                TorrentListUtils::printTorrentsTable($obsoleteList, $output);
48
                return 1;
49
            }
50
        }
51
52
        try {
53
            $influxDbClient = $this->getApplication()->getInfluxDbClient(
54
                $config->get('influxdb-host'),
55
                $config->get('influxdb-port'),
56
                $config->get('influxdb-user'),
57
                $config->get('influxdb-password'),
58
                $config->get('influxdb-database')
59
            );
60
61
            $points = [];
62
63
            $transmissionHost = $config->get('transmission-host');
64
65
            foreach ($torrentList as $torrent) {
66
                $age = TorrentUtils::getTorrentAge($torrent);
67
                $torrentPoint = $influxDbClient->buildPoint($torrent, $transmissionHost);
68
69
                if ($age) {
70
                    $points[] = $torrentPoint;
71
                } else {
72
                    $logger->debug('Skip point: {point}', ['point' => $torrentPoint]);
73
                }
74
            }
75
76
            $points[] = $influxDbClient->buildStatus($torrentList, $transmissionHost);
77
78
            $this->dryRun($input, $output, function () use ($influxDbClient, $points) {
79
                $influxDbClient->writePoints($points);
80
            }, 'dry-run, don\'t really send points');
81
        } catch (\Exception $e) {
82
            $logger->critical($e->getMessage());
83
            return 1;
84
        }
85
86
        return 0;
87
    }
88
}
89