Completed
Push — master ( 4d0472...3ee7c2 )
by Stanislav
02:15
created

Application   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 12
Bugs 1 Features 5
Metric Value
wmc 14
c 12
b 1
f 5
lcom 1
cbo 11
dl 0
loc 129
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDefaultCommands() 0 14 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
1
<?php
2
3
namespace Popstas\Transmission\Console;
4
5
use GuzzleHttp;
6
use Popstas\Transmission\Console\Command;
7
use Psr\Log\LoggerInterface;
8
use Stecman\Component\Symfony\Console\BashCompletion;
9
use Symfony\Component\Console\Application as BaseApplication;
10
11
class Application extends BaseApplication
12
{
13
    const VERSION = 'dev';
14
15
    /**
16
     * @var Config $config
17
     */
18
    private $config;
19
20
    /**
21
     * @var LoggerInterface $logger
22
     */
23
    private $logger;
24
25
    /**
26
     * @var TransmissionClient $client
27
     */
28
    private $client;
29
30
    /**
31
     * @var WeburgClient
32
     */
33
    private $weburgClient;
34
35
    public function __construct($name = 'Transmission CLI', $version = '@git-version@')
36
    {
37
        parent::__construct($name, $version);
38
    }
39
40
    /**
41
     * @return array|\Symfony\Component\Console\Command\Command[]
42
     */
43
    protected function getDefaultCommands()
44
    {
45
        $commands = array_merge(parent::getDefaultCommands(), [
46
            new BashCompletion\CompletionCommand(),
47
48
            new Command\StatsSend(),
49
            new Command\TorrentClean(),
50
            new Command\TorrentList(),
51
            new Command\TorrentRemoveDuplicates(),
52
            new Command\WeburgDownload(),
53
            new Command\WeburgSeriesAdd(),
54
        ]);
55
        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...
56
    }
57
58
    public function getLongVersion()
59
    {
60
        if (('@' . 'git-version@') !== $this->getVersion()) {
61
            return sprintf(
62
                '<info>%s</info> version <comment>%s</comment> build <comment>%s</comment>',
63
                $this->getName(),
64
                $this->getVersion(),
65
                '@git-commit@'
66
            );
67
        }
68
        return '<info>' . $this->getName() . '</info> (repo)';
69
    }
70
71
    /**
72
     * @return TransmissionClient
73
     */
74
    public function getClient()
75
    {
76
        return $this->client;
77
    }
78
79
    /**
80
     * @param TransmissionClient $client
81
     */
82
    public function setClient($client)
83
    {
84
        $this->client = $client;
85
    }
86
87
    /**
88
     * @return LoggerInterface
89
     */
90
    public function getLogger()
91
    {
92
        return $this->logger;
93
    }
94
95
    /**
96
     * @param LoggerInterface $logger
97
     */
98
    public function setLogger($logger)
99
    {
100
        $this->logger = $logger;
101
    }
102
103
    /**
104
     * @return Config
105
     */
106
    public function getConfig()
107
    {
108
        return $this->config;
109
    }
110
111
    /**
112
     * @param Config $config
113
     */
114
    public function setConfig($config)
115
    {
116
        $this->config = $config;
117
    }
118
119
    public function getWeburgClient()
120
    {
121
        if (!isset($this->weburgClient)) {
122
            $this->weburgClient = $this->createWeburgClient();
123
        }
124
        return $this->weburgClient;
125
    }
126
127
    public function setWeburgClient($weburgClient)
128
    {
129
        $this->weburgClient = $weburgClient;
130
    }
131
132
    public function createWeburgClient()
133
    {
134
        $config = $this->getConfig();
135
        $requestDelay = $config->get('weburg-request-delay');
136
        $httpClient = new GuzzleHttp\Client();
137
        return new WeburgClient($httpClient, $requestDelay);
138
    }
139
}
140