Completed
Push — master ( df650f...2515f9 )
by Stanislav
07:24
created

Application   B

Complexity

Total Complexity 19

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 16

Importance

Changes 18
Bugs 1 Features 6
Metric Value
wmc 19
c 18
b 1
f 6
lcom 2
cbo 16
dl 0
loc 186
rs 8.4614

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getLongVersion() 0 12 2
A getClient() 0 4 1
A setClient() 0 4 1
A getLogger() 0 4 1
A setLogger() 0 4 1
A getConfig() 0 4 1
A setConfig() 0 4 1
A getWeburgClient() 0 7 2
A setWeburgClient() 0 4 1
A createWeburgClient() 0 7 1
A getDefaultCommands() 0 16 1
A getInfluxDbClient() 0 7 2
A setInfluxDbClient() 0 4 1
A createInfluxDbClient() 0 15 2
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;
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...
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