These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | private $rawHelp; |
||
22 | |||
23 | protected function configure() |
||
24 | { |
||
25 | $this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Dry run, don\'t change any data'); |
||
26 | $this->addOption('config', null, InputOption::VALUE_OPTIONAL, 'Configuration file'); |
||
27 | $this->addOption('transmission-host', null, InputOption::VALUE_OPTIONAL, 'Transmission host'); |
||
28 | $this->addOption('transmission-port', null, InputOption::VALUE_OPTIONAL, 'Transmission port'); |
||
29 | $this->addOption('transmission-user', null, InputOption::VALUE_OPTIONAL, 'Transmission user'); |
||
30 | $this->addOption('transmission-password', null, InputOption::VALUE_OPTIONAL, 'Transmission password'); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @return Application |
||
35 | */ |
||
36 | public function getApplication() |
||
37 | { |
||
38 | return parent::getApplication(); |
||
39 | } |
||
40 | |||
41 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
0 ignored issues
–
show
|
|||
42 | { |
||
43 | // logger |
||
44 | $logger = $this->getApplication()->getLogger(); |
||
45 | if (!isset($logger)) { |
||
46 | $verbosityLevelMap = [ |
||
47 | LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL, |
||
48 | LogLevel::INFO => OutputInterface::VERBOSITY_VERBOSE, |
||
49 | LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG, |
||
50 | ]; |
||
51 | $logger = new ConsoleLogger($output, $verbosityLevelMap); |
||
52 | $this->getApplication()->setLogger($logger); |
||
53 | } |
||
54 | |||
55 | // config |
||
56 | $config = $this->getApplication()->getConfig(); |
||
57 | if (!isset($config)) { |
||
58 | $config = new Config($input->getOption('config')); |
||
59 | try { |
||
60 | $config->loadConfigFile(); |
||
61 | } catch (\RuntimeException $e) { |
||
62 | $logger->critical($e->getMessage()); |
||
63 | $this->setCode(function () { |
||
64 | return 1; |
||
65 | }); |
||
66 | return; |
||
67 | } |
||
68 | $this->getApplication()->setConfig($config); |
||
69 | } |
||
70 | |||
71 | // compatibility: first client available at old config names |
||
72 | // this overrides config values to options from command line arguments |
||
73 | $firstTransmission = $config->get('transmission')[0]; |
||
74 | $vars = ['host', 'port', 'user', 'password']; |
||
75 | foreach ($vars as $var) { |
||
76 | $configName = 'transmission-' . $var; |
||
77 | $config->set($configName, $this->getInputOption($input, $configName, $firstTransmission[$var])); |
||
78 | } |
||
79 | |||
80 | // client |
||
81 | $client = $this->getApplication()->getClient(); |
||
82 | if (!isset($client)) { |
||
83 | $client = $this->createTransmissionClient( |
||
84 | $config->get('transmission-host'), |
||
85 | $config->get('transmission-port'), |
||
86 | $config->get('transmission-user'), |
||
87 | $config->get('transmission-password') |
||
88 | ); |
||
89 | $this->getApplication()->setClient($client); |
||
90 | } |
||
91 | |||
92 | $logger->info('[{date}] command: {args}', [ |
||
93 | 'date' => date('Y-m-d H:i:s'), |
||
94 | 'args' => implode(' ', array_slice($_SERVER['argv'], 1)), |
||
95 | ]); |
||
96 | |||
97 | parent::initialize($input, $output); |
||
98 | } |
||
99 | |||
100 | private function createTransmissionClient($host, $port, $user, $password) |
||
101 | { |
||
102 | $logger = $this->getApplication()->getLogger(); |
||
103 | |||
104 | $connect = ['host' => $host, 'port' => $port, 'user' => $user, 'password' => $password]; |
||
105 | |||
106 | $baseUri = 'http://' . $connect['host'] . ':' . $connect['port'] . '/transmission/rpc'; |
||
107 | $httpClient = new GuzzleHttp\Client(['base_uri' => $baseUri]); |
||
108 | |||
109 | $api = new API\RpcClient($httpClient, $connect['user'], $connect['password'], $logger); |
||
110 | |||
111 | $logger->debug('Connect Transmission using: {user}:{password}@{host}:{port}', $connect); |
||
112 | |||
113 | return new TransmissionClient($api); |
||
114 | } |
||
115 | |||
116 | private function getInputOption(InputInterface $input, $optionName, $defaultValue = null) |
||
117 | { |
||
118 | $value = $defaultValue; |
||
119 | $optionValue = $input->hasOption($optionName) ? $input->getOption($optionName) : null; |
||
120 | if (isset($optionValue)) { |
||
121 | $value = $optionValue; |
||
122 | } |
||
123 | return $value; |
||
124 | } |
||
125 | |||
126 | public function dryRun( |
||
127 | InputInterface $input, |
||
128 | OutputInterface $output, |
||
129 | \Closure $callback, |
||
130 | $message = 'dry-run, don\'t doing anything' |
||
131 | ) { |
||
132 | if (!$input->getOption('dry-run')) { |
||
133 | return $callback(); |
||
134 | } else { |
||
135 | $output->writeln($message); |
||
136 | return true; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | public function setHelp($help) |
||
141 | { |
||
142 | $this->rawHelp = $help; |
||
143 | $help = preg_replace('/```\n(.*?)\n```/mis', '<info>$1</info>', $help); |
||
144 | $help = preg_replace('/(\n|^)#+ (.*)/', '$1<question>$2</question>', $help); |
||
145 | $help = preg_replace('/`([^`]*?)`/', '<comment>$1</comment>', $help); |
||
146 | $help = preg_replace('/\*\*(.*?)\*\*/', '<comment>$1</comment>', $help); |
||
147 | return parent::setHelp($help); |
||
148 | } |
||
149 | |||
150 | public function getRawHelp() |
||
151 | { |
||
152 | return $this->rawHelp; |
||
153 | } |
||
154 | } |
||
155 |
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: