1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popstas\Transmission\Console; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp; |
6
|
|
|
use InfluxDB; |
7
|
|
|
use Popstas\Transmission\Console\Command; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion; |
10
|
|
|
use Symfony\Component\Console\Application as BaseApplication; |
11
|
|
|
|
12
|
|
|
class Application extends BaseApplication |
13
|
|
|
{ |
14
|
|
|
const VERSION = 'dev'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Config $config |
18
|
|
|
*/ |
19
|
|
|
private $config; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var LoggerInterface $logger |
23
|
|
|
*/ |
24
|
|
|
private $logger; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var TransmissionClient $client |
28
|
|
|
*/ |
29
|
|
|
private $client; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var WeburgClient |
33
|
|
|
*/ |
34
|
|
|
private $weburgClient; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var InfluxDbClient |
38
|
|
|
*/ |
39
|
|
|
private $influxDbClient; |
40
|
|
|
|
41
|
|
|
public function __construct($name = 'Transmission CLI', $version = '@git-version@') |
42
|
|
|
{ |
43
|
|
|
parent::__construct($name, $version); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return array|\Symfony\Component\Console\Command\Command[] |
48
|
|
|
*/ |
49
|
|
|
protected function getDefaultCommands() |
50
|
|
|
{ |
51
|
|
|
$commands = array_merge(parent::getDefaultCommands(), [ |
52
|
|
|
new BashCompletion\CompletionCommand(), |
53
|
|
|
|
54
|
|
|
new Command\StatsGet(), |
55
|
|
|
new Command\StatsSend(), |
56
|
|
|
new Command\TorrentAdd(), |
57
|
|
|
new Command\TorrentList(), |
58
|
|
|
new Command\TorrentRemove(), |
59
|
|
|
new Command\TorrentRemoveDuplicates(), |
60
|
|
|
new Command\WeburgDownload(), |
61
|
|
|
new Command\WeburgSeriesAdd(), |
62
|
|
|
]); |
63
|
|
|
return $commands; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getLongVersion() |
67
|
|
|
{ |
68
|
|
|
if (('@' . 'git-version@') !== $this->getVersion()) { |
69
|
|
|
return sprintf( |
70
|
|
|
'<info>%s</info> version <comment>%s</comment> build <comment>%s</comment>', |
71
|
|
|
$this->getName(), |
72
|
|
|
$this->getVersion(), |
73
|
|
|
'@git-commit@' |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
return '<info>' . $this->getName() . '</info> (repo)'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return TransmissionClient |
81
|
|
|
*/ |
82
|
|
|
public function getClient() |
83
|
|
|
{ |
84
|
|
|
return $this->client; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param TransmissionClient $client |
89
|
|
|
*/ |
90
|
|
|
public function setClient($client) |
91
|
|
|
{ |
92
|
|
|
$this->client = $client; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return LoggerInterface |
97
|
|
|
*/ |
98
|
|
|
public function getLogger() |
99
|
|
|
{ |
100
|
|
|
return $this->logger; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param LoggerInterface $logger |
105
|
|
|
*/ |
106
|
|
|
public function setLogger($logger) |
107
|
|
|
{ |
108
|
|
|
$this->logger = $logger; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return Config |
113
|
|
|
*/ |
114
|
|
|
public function getConfig() |
115
|
|
|
{ |
116
|
|
|
return $this->config; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param Config $config |
121
|
|
|
*/ |
122
|
|
|
public function setConfig($config) |
123
|
|
|
{ |
124
|
|
|
$this->config = $config; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getWeburgClient() |
128
|
|
|
{ |
129
|
|
|
if (!isset($this->weburgClient)) { |
130
|
|
|
$this->weburgClient = $this->createWeburgClient(); |
131
|
|
|
} |
132
|
|
|
return $this->weburgClient; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function setWeburgClient($weburgClient) |
136
|
|
|
{ |
137
|
|
|
$this->weburgClient = $weburgClient; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function createWeburgClient() |
141
|
|
|
{ |
142
|
|
|
$config = $this->getConfig(); |
143
|
|
|
$requestDelay = $config->get('weburg-request-delay'); |
144
|
|
|
$httpClient = new GuzzleHttp\Client(); |
145
|
|
|
return new WeburgClient($httpClient, $requestDelay); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param $host |
150
|
|
|
* @param $port |
151
|
|
|
* @param $user |
152
|
|
|
* @param $password |
153
|
|
|
* @param $databaseName |
154
|
|
|
* @return InfluxDbClient |
155
|
|
|
* @throws InfluxDB\Database\Exception |
156
|
|
|
*/ |
157
|
|
|
public function getInfluxDbClient($host, $port, $user, $password, $databaseName) |
158
|
|
|
{ |
159
|
|
|
if (!isset($this->influxDbClient)) { |
160
|
|
|
$this->influxDbClient = $this->createInfluxDbClient($host, $port, $user, $password, $databaseName); |
161
|
|
|
} |
162
|
|
|
return $this->influxDbClient; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param InfluxDbClient $influxDbClient |
167
|
|
|
*/ |
168
|
|
|
public function setInfluxDbClient($influxDbClient) |
169
|
|
|
{ |
170
|
|
|
$this->influxDbClient = $influxDbClient; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param $host |
175
|
|
|
* @param $port |
176
|
|
|
* @param $user |
177
|
|
|
* @param $password |
178
|
|
|
* @param $databaseName |
179
|
|
|
* @return InfluxDbClient |
180
|
|
|
* @throws InfluxDB\Database\Exception |
181
|
|
|
*/ |
182
|
|
|
public function createInfluxDbClient($host, $port, $user, $password, $databaseName) |
183
|
|
|
{ |
184
|
|
|
$influxDb = new InfluxDB\Client($host, $port, $user, $password); |
185
|
|
|
$connect = ['host' => $host, 'port' => $port, 'user' => $user, 'password' => $password]; |
186
|
|
|
$this->logger->debug('Connect InfluxDB using: {user}:{password}@{host}:{port}', $connect); |
187
|
|
|
|
188
|
|
|
if (!$databaseName) { |
189
|
|
|
throw new \RuntimeException('InfluxDb database not defined'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$influxDbClient = new InfluxDbClient($influxDb, $databaseName); |
193
|
|
|
$influxDbClient->setLogger($this->logger); |
194
|
|
|
|
195
|
|
|
return $influxDbClient; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.