Completed
Push — master ( 07e153...ca5d76 )
by Stanislav
02:20
created

Application::setClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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\TorrentClean(),
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
     * @return InfluxDbClient
150
     * @throws InfluxDB\Database\Exception
151
     */
152
    public function getInfluxDbClient($host, $port, $user, $password, $databaseName)
153
    {
154
        if (!isset($this->influxDbClient)) {
155
            $this->influxDbClient = $this->createInfluxDbClient($host, $port, $user, $password, $databaseName);
156
        }
157
        return $this->influxDbClient;
158
    }
159
160
    /**
161
     * @param InfluxDbClient $influxDbClient
162
     */
163
    public function setInfluxDbClient($influxDbClient)
164
    {
165
        $this->influxDbClient = $influxDbClient;
166
    }
167
168
    /**
169
     * @param $host
170
     * @param $port
171
     * @param $user
172
     * @param $password
173
     * @param $databaseName
174
     * @return InfluxDbClient
175
     * @throws InfluxDB\Database\Exception
176
     */
177
    public function createInfluxDbClient($host, $port, $user, $password, $databaseName)
178
    {
179
        $influxDb = new InfluxDB\Client($host, $port, $user, $password);
180
        $connect = ['host' => $host, 'port' => $port, 'user' => $user, 'password' => $password];
181
        $this->logger->debug('Connect InfluxDB using: {user}:{password}@{host}:{port}', $connect);
182
183
        if (!$databaseName) {
184
            throw new \RuntimeException('InfluxDb database not defined');
185
        }
186
187
        $influxDbClient = new InfluxDbClient($influxDb, $databaseName);
188
        $influxDbClient->setLogger($this->logger);
189
190
        return $influxDbClient;
191
    }
192
}
193