Completed
Push — master ( 70daad...2eb0da )
by Stanislav
02:30
created

Application::getInfluxDb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Popstas\Transmission\Console;
4
5
use GuzzleHttp;
6
use GuzzleHttp\Exception\ConnectException;
7
use InfluxDB;
8
use Popstas\Transmission\Console\Command;
9
use Psr\Log\LoggerInterface;
10
use Stecman\Component\Symfony\Console\BashCompletion;
11
use Symfony\Component\Console\Application as BaseApplication;
12
use Symfony\Component\Console\Input\InputInterface;
13
14
class Application extends BaseApplication
15
{
16
    const VERSION = 'dev';
17
18
    /**
19
     * @var Config $config
20
     */
21
    private $config;
22
23
    /**
24
     * @var LoggerInterface $logger
25
     */
26
    private $logger;
27
28
    /**
29
     * @var TransmissionClient $client
30
     */
31
    private $client;
32
33
    /**
34
     * @var WeburgClient
35
     */
36
    private $weburgClient;
37
38
    /**
39
     * @var InfluxDB\Client $influxDb
40
     */
41
    private $influxDb;
42
43
    public function __construct($name = 'Transmission CLI', $version = '@git-version@')
44
    {
45
        parent::__construct($name, $version);
46
    }
47
48
    /**
49
     * @return array|\Symfony\Component\Console\Command\Command[]
50
     */
51
    protected function getDefaultCommands()
52
    {
53
        $commands = array_merge(parent::getDefaultCommands(), [
54
            new BashCompletion\CompletionCommand(),
55
56
            new Command\StatsSend(),
57
            new Command\TorrentClean(),
58
            new Command\TorrentList(),
59
            new Command\TorrentRemove(),
60
            new Command\TorrentRemoveDuplicates(),
61
            new Command\WeburgDownload(),
62
            new Command\WeburgSeriesAdd(),
63
        ]);
64
        return $commands;
0 ignored issues
show
Best Practice introduced by
The expression return $commands; seems to be an array, but some of its elements' types (Stecman\Component\Symfon...Command\WeburgSeriesAdd) are incompatible with the return type of the parent method Symfony\Component\Consol...ion::getDefaultCommands of type array<Symfony\Component\...le\Command\ListCommand>.

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:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
65
    }
66
67
    public function getLongVersion()
68
    {
69
        if (('@' . 'git-version@') !== $this->getVersion()) {
70
            return sprintf(
71
                '<info>%s</info> version <comment>%s</comment> build <comment>%s</comment>',
72
                $this->getName(),
73
                $this->getVersion(),
74
                '@git-commit@'
75
            );
76
        }
77
        return '<info>' . $this->getName() . '</info> (repo)';
78
    }
79
80
    /**
81
     * @return TransmissionClient
82
     */
83
    public function getClient()
84
    {
85
        return $this->client;
86
    }
87
88
    /**
89
     * @param TransmissionClient $client
90
     */
91
    public function setClient($client)
92
    {
93
        $this->client = $client;
94
    }
95
96
    /**
97
     * @return LoggerInterface
98
     */
99
    public function getLogger()
100
    {
101
        return $this->logger;
102
    }
103
104
    /**
105
     * @param LoggerInterface $logger
106
     */
107
    public function setLogger($logger)
108
    {
109
        $this->logger = $logger;
110
    }
111
112
    /**
113
     * @return Config
114
     */
115
    public function getConfig()
116
    {
117
        return $this->config;
118
    }
119
120
    /**
121
     * @param Config $config
122
     */
123
    public function setConfig($config)
124
    {
125
        $this->config = $config;
126
    }
127
128
    public function getWeburgClient()
129
    {
130
        if (!isset($this->weburgClient)) {
131
            $this->weburgClient = $this->createWeburgClient();
132
        }
133
        return $this->weburgClient;
134
    }
135
136
    public function setWeburgClient($weburgClient)
137
    {
138
        $this->weburgClient = $weburgClient;
139
    }
140
141
    public function createWeburgClient()
142
    {
143
        $config = $this->getConfig();
144
        $requestDelay = $config->get('weburg-request-delay');
145
        $httpClient = new GuzzleHttp\Client();
146
        return new WeburgClient($httpClient, $requestDelay);
147
    }
148
149
    /**
150
     * @return InfluxDB\Client $influxDb
151
     */
152
    public function getInfluxDb()
153
    {
154
        return $this->influxDb;
155
    }
156
157
    /**
158
     * @param InfluxDB\Client $influxDb
159
     */
160
    public function setInfluxDb($influxDb)
161
    {
162
        $this->influxDb = $influxDb;
163
    }
164
165
    public function createInfluxDb($host, $port, $user, $password)
166
    {
167
        $logger = $this->getLogger();
168
169
        $connect = ['host' => $host, 'port' => $port, 'user' => $user, 'password' => $password];
170
171
        $influxDb = new InfluxDB\Client(
172
            $connect['host'],
173
            $connect['port'],
174
            $connect['user'],
175
            $connect['password']
176
        );
177
        $logger->debug('Connect InfluxDB using: {user}:{password}@{host}:{port}', $connect);
178
179
        return $influxDb;
180
    }
181
182
    /**
183
     * @param InputInterface $input
184
     * @return InfluxDB\Database
185
     * @throws InfluxDB\Database\Exception
186
     */
187
    public function getDatabase(InputInterface $input)
188
    {
189
        $config = $this->getConfig();
190
        $logger = $this->getLogger();
191
192
        $influxDb = $this->getInfluxDb();
193
        if (!isset($influxDb)) {
194
            $this->setInfluxDb($this->createInfluxDb(
195
                $config->overrideConfig($input, 'influxdb-host'),
196
                $config->overrideConfig($input, 'influxdb-port'),
197
                $config->overrideConfig($input, 'influxdb-user'),
198
                $config->overrideConfig($input, 'influxdb-password')
199
            ));
200
            $influxDb = $this->getInfluxDb();
201
        }
202
203
        $databaseName = $config->overrideConfig($input, 'influxdb-database');
204
        if (!$databaseName) {
205
            throw new \RuntimeException('InfluxDb database not defined');
206
        }
207
208
        $database = $influxDb->selectDB($databaseName);
209
210
        try {
211
            $databaseExists = $database->exists();
212
        } catch (ConnectException $e) {
213
            throw new \RuntimeException('InfluxDb connection error: ' . $e->getMessage());
214
        }
215
        if (!$databaseExists) {
216
            $logger->info('Database ' . $databaseName . ' not exists, creating');
217
            $database->create();
218
        }
219
220
        return $database;
221
    }
222
}
223