Completed
Push — master ( 2515f9...556e6f )
by Stanislav
03:05
created

src/Application.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

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;
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\Docs(),
55
            new Command\StatsGet(),
56
            new Command\StatsSend(),
57
            new Command\TorrentAdd(),
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
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
     * @param $host
151
     * @param $port
152
     * @param $user
153
     * @param $password
154
     * @param $databaseName
155
     * @return InfluxDbClient
156
     * @throws InfluxDB\Database\Exception
157
     */
158
    public function getInfluxDbClient($host, $port, $user, $password, $databaseName)
159
    {
160
        if (!isset($this->influxDbClient)) {
161
            $this->influxDbClient = $this->createInfluxDbClient($host, $port, $user, $password, $databaseName);
162
        }
163
        return $this->influxDbClient;
164
    }
165
166
    /**
167
     * @param InfluxDbClient $influxDbClient
168
     */
169
    public function setInfluxDbClient($influxDbClient)
170
    {
171
        $this->influxDbClient = $influxDbClient;
172
    }
173
174
    /**
175
     * @param $host
176
     * @param $port
177
     * @param $user
178
     * @param $password
179
     * @param $databaseName
180
     * @return InfluxDbClient
181
     * @throws InfluxDB\Database\Exception
182
     */
183
    public function createInfluxDbClient($host, $port, $user, $password, $databaseName)
184
    {
185
        $influxDb = new InfluxDB\Client($host, $port, $user, $password);
186
        $connect = ['host' => $host, 'port' => $port, 'user' => $user, 'password' => $password];
187
        $this->logger->debug('Connect InfluxDB using: {user}:{password}@{host}:{port}', $connect);
188
189
        if (!$databaseName) {
190
            throw new \RuntimeException('InfluxDb database not defined');
191
        }
192
193
        $influxDbClient = new InfluxDbClient($influxDb, $databaseName);
194
        $influxDbClient->setLogger($this->logger);
195
196
        return $influxDbClient;
197
    }
198
}
199