Completed
Push — master ( c93d1e...39b788 )
by Stanislav
02:17
created

Application   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 15

Importance

Changes 16
Bugs 1 Features 5
Metric Value
wmc 19
c 16
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 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\TorrentList(),
57
            new Command\TorrentRemove(),
58
            new Command\TorrentRemoveDuplicates(),
59
            new Command\WeburgDownload(),
60
            new Command\WeburgSeriesAdd(),
61
        ]);
62
        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...
63
    }
64
65
    public function getLongVersion()
66
    {
67
        if (('@' . 'git-version@') !== $this->getVersion()) {
68
            return sprintf(
69
                '<info>%s</info> version <comment>%s</comment> build <comment>%s</comment>',
70
                $this->getName(),
71
                $this->getVersion(),
72
                '@git-commit@'
73
            );
74
        }
75
        return '<info>' . $this->getName() . '</info> (repo)';
76
    }
77
78
    /**
79
     * @return TransmissionClient
80
     */
81
    public function getClient()
82
    {
83
        return $this->client;
84
    }
85
86
    /**
87
     * @param TransmissionClient $client
88
     */
89
    public function setClient($client)
90
    {
91
        $this->client = $client;
92
    }
93
94
    /**
95
     * @return LoggerInterface
96
     */
97
    public function getLogger()
98
    {
99
        return $this->logger;
100
    }
101
102
    /**
103
     * @param LoggerInterface $logger
104
     */
105
    public function setLogger($logger)
106
    {
107
        $this->logger = $logger;
108
    }
109
110
    /**
111
     * @return Config
112
     */
113
    public function getConfig()
114
    {
115
        return $this->config;
116
    }
117
118
    /**
119
     * @param Config $config
120
     */
121
    public function setConfig($config)
122
    {
123
        $this->config = $config;
124
    }
125
126
    public function getWeburgClient()
127
    {
128
        if (!isset($this->weburgClient)) {
129
            $this->weburgClient = $this->createWeburgClient();
130
        }
131
        return $this->weburgClient;
132
    }
133
134
    public function setWeburgClient($weburgClient)
135
    {
136
        $this->weburgClient = $weburgClient;
137
    }
138
139
    public function createWeburgClient()
140
    {
141
        $config = $this->getConfig();
142
        $requestDelay = $config->get('weburg-request-delay');
143
        $httpClient = new GuzzleHttp\Client();
144
        return new WeburgClient($httpClient, $requestDelay);
145
    }
146
147
    /**
148
     * @return InfluxDbClient
149
     * @throws InfluxDB\Database\Exception
150
     */
151
    public function getInfluxDbClient($host, $port, $user, $password, $databaseName)
152
    {
153
        if (!isset($this->influxDbClient)) {
154
            $this->influxDbClient = $this->createInfluxDbClient($host, $port, $user, $password, $databaseName);
155
        }
156
        return $this->influxDbClient;
157
    }
158
159
    /**
160
     * @param InfluxDbClient $influxDbClient
161
     */
162
    public function setInfluxDbClient($influxDbClient)
163
    {
164
        $this->influxDbClient = $influxDbClient;
165
    }
166
167
    /**
168
     * @param $host
169
     * @param $port
170
     * @param $user
171
     * @param $password
172
     * @param $databaseName
173
     * @return InfluxDbClient
174
     * @throws InfluxDB\Database\Exception
175
     */
176
    public function createInfluxDbClient($host, $port, $user, $password, $databaseName)
177
    {
178
        $influxDb = new InfluxDB\Client($host, $port, $user, $password);
179
        $connect = ['host' => $host, 'port' => $port, 'user' => $user, 'password' => $password];
180
        $this->logger->debug('Connect InfluxDB using: {user}:{password}@{host}:{port}', $connect);
181
182
        if (!$databaseName) {
183
            throw new \RuntimeException('InfluxDb database not defined');
184
        }
185
186
        $influxDbClient = new InfluxDbClient($influxDb, $databaseName);
187
        $influxDbClient->setLogger($this->logger);
188
189
        return $influxDbClient;
190
    }
191
}
192