Application   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 18

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 18
dl 0
loc 196
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDefaultCommands() 0 18 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 15 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\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\WeburgInfo(),
63
            new Command\WeburgSeriesAdd(),
64
        ]);
65
        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...
66
    }
67
68
    public function getLongVersion()
69
    {
70
        if (('@' . 'git-version@') !== $this->getVersion()) {
71
            return sprintf(
72
                '<info>%s</info> version <comment>%s</comment> build <comment>%s</comment>',
73
                $this->getName(),
74
                $this->getVersion(),
75
                '@git-commit@'
76
            );
77
        }
78
        return '<info>' . $this->getName() . '</info> (repo)';
79
    }
80
81
    /**
82
     * @return TransmissionClient
83
     */
84
    public function getClient()
85
    {
86
        return $this->client;
87
    }
88
89
    /**
90
     * @param TransmissionClient $client
91
     */
92
    public function setClient($client)
93
    {
94
        $this->client = $client;
95
    }
96
97
    /**
98
     * @return LoggerInterface
99
     */
100
    public function getLogger()
101
    {
102
        return $this->logger;
103
    }
104
105
    /**
106
     * @param LoggerInterface $logger
107
     */
108
    public function setLogger($logger)
109
    {
110
        $this->logger = $logger;
111
    }
112
113
    /**
114
     * @return Config
115
     */
116
    public function getConfig()
117
    {
118
        return $this->config;
119
    }
120
121
    /**
122
     * @param Config $config
123
     */
124
    public function setConfig($config)
125
    {
126
        $this->config = $config;
127
    }
128
129
    public function getWeburgClient()
130
    {
131
        if (!isset($this->weburgClient)) {
132
            $this->weburgClient = $this->createWeburgClient();
133
        }
134
        return $this->weburgClient;
135
    }
136
137
    public function setWeburgClient($weburgClient)
138
    {
139
        $this->weburgClient = $weburgClient;
140
    }
141
142
    public function createWeburgClient()
143
    {
144
        $config = $this->getConfig();
145
        $requestDelay = $config->get('weburg-request-delay');
146
        $httpClient = new GuzzleHttp\Client([
147
            'allow_redirects' => [
148
                'max'             => 10,
149
                'strict'          => false,
150
                'referer'         => false,
151
                'protocols'       => ['http', 'https'],
152
                'track_redirects' => false,
153
            ],
154
        ]);
155
        return new WeburgClient($httpClient, $requestDelay);
156
    }
157
158
    /**
159
     * @param $host
160
     * @param $port
161
     * @param $user
162
     * @param $password
163
     * @param $databaseName
164
     * @return InfluxDbClient
165
     * @throws InfluxDB\Database\Exception
166
     */
167
    public function getInfluxDbClient($host, $port, $user, $password, $databaseName)
168
    {
169
        if (!isset($this->influxDbClient)) {
170
            $this->influxDbClient = $this->createInfluxDbClient($host, $port, $user, $password, $databaseName);
171
        }
172
        return $this->influxDbClient;
173
    }
174
175
    /**
176
     * @param InfluxDbClient $influxDbClient
177
     */
178
    public function setInfluxDbClient($influxDbClient)
179
    {
180
        $this->influxDbClient = $influxDbClient;
181
    }
182
183
    /**
184
     * @param $host
185
     * @param $port
186
     * @param $user
187
     * @param $password
188
     * @param $databaseName
189
     * @return InfluxDbClient
190
     * @throws InfluxDB\Database\Exception
191
     */
192
    public function createInfluxDbClient($host, $port, $user, $password, $databaseName)
193
    {
194
        $influxDb = new InfluxDB\Client($host, $port, $user, $password);
195
        $connect = ['host' => $host, 'port' => $port, 'user' => $user, 'password' => $password];
196
        $this->logger->debug('Connect InfluxDB using: {user}:{password}@{host}:{port}', $connect);
197
198
        if (!$databaseName) {
199
            throw new \RuntimeException('InfluxDb database not defined');
200
        }
201
202
        $influxDbClient = new InfluxDbClient($influxDb, $databaseName);
203
        $influxDbClient->setLogger($this->logger);
204
205
        return $influxDbClient;
206
    }
207
}
208