Completed
Push — master ( 6f7267...183492 )
by Stanislav
10:16
created

Command::initialize()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 3
Metric Value
c 7
b 1
f 3
dl 0
loc 40
rs 8.5806
cc 4
eloc 25
nc 8
nop 2
1
<?php
2
3
namespace Popstas\Transmission\Console\Command;
4
5
use GuzzleHttp;
6
use InfluxDB;
7
use Martial\Transmission\API;
8
use Popstas\Transmission\Console;
9
use Popstas\Transmission\Console\Application;
10
use Popstas\Transmission\Console\Config;
11
use Popstas\Transmission\Console\TransmissionClient;
12
use Psr\Log\LogLevel;
13
use Symfony\Component\Console\Command\Command as BaseCommand;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Logger\ConsoleLogger;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class Command extends BaseCommand
20
{
21
    protected function configure()
22
    {
23
        $this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Dry run, don\'t change any data');
24
        $this->addOption('config', null, InputOption::VALUE_OPTIONAL, 'Configuration file');
25
        $this->addOption('transmission-host', null, InputOption::VALUE_OPTIONAL, 'Transmission host');
26
        $this->addOption('transmission-port', null, InputOption::VALUE_OPTIONAL, 'Transmission port');
27
        $this->addOption('transmission-user', null, InputOption::VALUE_OPTIONAL, 'Transmission user');
28
        $this->addOption('transmission-password', null, InputOption::VALUE_OPTIONAL, 'Transmission password');
29
    }
30
31
    /**
32
     * @return Application
33
     */
34
    public function getApplication()
35
    {
36
        return parent::getApplication();
37
    }
38
39
    protected function initialize(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Coding Style introduced by
initialize uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
40
    {
41
        // logger
42
        $logger = $this->getApplication()->getLogger();
43
        if (!isset($logger)) {
44
            $verbosityLevelMap = [
45
                LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
46
                LogLevel::INFO   => OutputInterface::VERBOSITY_VERBOSE,
47
                LogLevel::DEBUG  => OutputInterface::VERBOSITY_DEBUG,
48
            ];
49
            $logger = new ConsoleLogger($output, $verbosityLevelMap);
50
            $this->getApplication()->setLogger($logger);
51
        }
52
53
        // config
54
        $config = $this->getApplication()->getConfig();
55
        if (!isset($config)) {
56
            $config = new Config($input->getOption('config'));
57
            $this->getApplication()->setConfig($config);
58
        }
59
60
        // client
61
        $client = $this->getApplication()->getClient();
62
        if (!isset($client)) {
63
            $client = $this->createTransmissionClient(
64
                $config->overrideConfig($input, 'transmission-host'),
65
                $config->overrideConfig($input, 'transmission-port'),
66
                $config->overrideConfig($input, 'transmission-user'),
67
                $config->overrideConfig($input, 'transmission-password')
68
            );
69
            $this->getApplication()->setClient($client);
70
        }
71
72
        $logger->info('[{date}] command: {args}', [
73
            'date' => date('Y-m-d H:i:s'),
74
            'args' => implode(' ', array_slice($_SERVER['argv'], 1)),
75
        ]);
76
77
        parent::initialize($input, $output);
78
    }
79
80
    private function createTransmissionClient($host, $port, $user, $password)
81
    {
82
        $logger = $this->getApplication()->getLogger();
83
84
        $connect = ['host' => $host, 'port' => $port, 'user' => $user, 'password' => $password];
85
86
        $baseUri = 'http://' . $connect['host'] . ':' . $connect['port'] . '/transmission/rpc';
87
        $httpClient = new GuzzleHttp\Client(['base_uri' => $baseUri]);
88
89
        $api = new API\RpcClient($httpClient, $connect['user'], $connect['password']);
90
        $api->setLogger($logger);
91
92
        $logger->debug('Connect Transmission using: {user}:{password}@{host}:{port}', $connect);
93
94
        return new TransmissionClient($api);
95
    }
96
97
    public function dryRun(
98
        InputInterface $input,
99
        OutputInterface $output,
100
        \Closure $callback,
101
        $message = 'dry-run, don\'t doing anytning'
102
    ) {
103
        if (!$input->getOption('dry-run')) {
104
            $callback();
105
        } else {
106
            $output->writeln($message);
107
        }
108
    }
109
}
110