Completed
Push — master ( 6a6fc8...07e153 )
by Stanislav
02:24
created

Application   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 15

Importance

Changes 15
Bugs 1 Features 5
Metric Value
wmc 19
c 15
b 1
f 5
lcom 2
cbo 15
dl 0
loc 180
rs 9.1666

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDefaultCommands() 0 15 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 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 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 InfluxDbClient
40
     */
41
    private $influxDbClient;
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 InfluxDbClient
151
     * @throws InfluxDB\Database\Exception
152
     */
153
    public function getInfluxDbClient($host, $port, $user, $password, $databaseName)
154
    {
155
        if (!isset($this->influxDbClient)) {
156
            $this->influxDbClient = $this->createInfluxDbClient($host, $port, $user, $password, $databaseName);
157
        }
158
        return $this->influxDbClient;
159
    }
160
161
    /**
162
     * @param InfluxDbClient $influxDbClient
163
     */
164
    public function setInfluxDbClient($influxDbClient)
165
    {
166
        $this->influxDbClient = $influxDbClient;
167
    }
168
169
    /**
170
     * @param $host
171
     * @param $port
172
     * @param $user
173
     * @param $password
174
     * @param $databaseName
175
     * @return InfluxDbClient
176
     * @throws InfluxDB\Database\Exception
177
     */
178
    public function createInfluxDbClient($host, $port, $user, $password, $databaseName)
179
    {
180
        $influxDb = new InfluxDB\Client($host, $port, $user, $password);
181
        $connect = ['host' => $host, 'port' => $port, 'user' => $user, 'password' => $password];
182
        $this->logger->debug('Connect InfluxDB using: {user}:{password}@{host}:{port}', $connect);
183
184
        if (!$databaseName) {
185
            throw new \RuntimeException('InfluxDb database not defined');
186
        }
187
188
        $influxDbClient = new InfluxDbClient($influxDb, $databaseName);
189
        $influxDbClient->setLogger($this->logger);
190
191
        return $influxDbClient;
192
    }
193
}
194