Completed
Push — master ( 390e4e...b0ec12 )
by Taosikai
12:57
created

Application   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 15
dl 0
loc 155
rs 9.1666

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getKernel() 0 4 1
A doRun() 0 19 2
A doRunClient() 0 10 2
A getEvents() 0 7 1
A onAuthError() 0 5 1
A onRegisterTunnelError() 0 8 1
A runClient() 0 8 2
A getDefaultCommands() 0 8 1
A getSubscribers() 0 7 1
A getDefaultInputDefinition() 0 11 1
1
<?php
2
/**
3
 * Spike library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Spike\Client;
7
8
use Slince\Event\Event;
9
use Spike\Application as BaseApplication;
10
use Slince\Event\SubscriberInterface;
11
use Spike\Client\Command\InitCommand;
12
use Spike\Client\Command\SpikeCommand;
13
use Spike\Client\Command\ShowProxyHostsCommand;
14
use Spike\Client\Subscriber\LoggerSubscriber;
15
use Spike\Logger\Logger;
16
use Symfony\Component\Console\Input\InputDefinition;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
class Application extends BaseApplication implements SubscriberInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    const NAME = 'spike-client';
27
28
    /**
29
     * @var string
30
     */
31
    const VERSION = '1.0.0.dev';
32
33
    /**
34
     * The client instance
35
     * @var Client
36
     */
37
    protected $client;
38
39
    /**
40
     * The server address
41
     * @var string
42
     */
43
    protected $serverAddress;
44
45
    public function __construct(Configuration $configuration)
46
    {
47
        parent::__construct($configuration,static::NAME, static::VERSION);
48
        $this->client = new Client(
49
            $configuration->getServerAddress(),
50
            $configuration->getTunnels(),
51
            $configuration->get('auth') ?: [],
52
            null,
53
            $this->dispatcher
54
        );
55
    }
56
57
    /**
58
     * Gets the client instance
59
     * @return Client
60
     */
61
    public function getKernel()
62
    {
63
        return $this->client;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function doRun(InputInterface $input, OutputInterface $output)
70
    {
71
        $this->input = $input;
72
        $this->output = $output;
73
        //Logger
74
        $this->logger = new Logger(
75
            $this->getConfiguration()->getLogLevel(),
76
            $this->getConfiguration()->getLogFile(),
77
            $this->output
78
        );
79
        $this->client->setLogger($this->logger);
80
        $commandName = $input->getFirstArgument();
81
        if ($commandName) {
82
            $exitCode = parent::doRun($input, $output);
83
        } else {
84
            $exitCode = $this->doRunClient();
85
        }
86
        return $exitCode;
87
    }
88
89
    protected function doRunClient()
90
    {
91
        if (true === $this->input->hasParameterOption(array('--help', '-h'), true)) {
92
            $command = $this->get('spike');
93
            $exitCode = $this->doRunCommand($command, $this->input, $this->output);
94
        } else {
95
            $exitCode = $this->runClient();
96
        }
97
        return $exitCode;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getEvents()
104
    {
105
        return [
106
            EventStore::AUTH_ERROR => 'onAuthError',
107
            EventStore::REGISTER_TUNNEL_ERROR => 'onRegisterTunnelError',
108
        ];
109
    }
110
111
    public function onAuthError(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        $this->output->writeln('Auth error, please checks your config file');
114
        $this->client->close();
115
    }
116
117
    public function onRegisterTunnelError(Event $event)
118
    {
119
        $this->output->writeln(sprintf('Registers the tunnel "%s" error, message: %s',
120
            $event->getArgument('tunnel'),
121
            $event->getArgument('errorMessage')
122
        ));
123
        $this->client->close();
124
    }
125
    /**
126
     * Start the client
127
     */
128
    protected function runClient()
129
    {
130
        foreach ($this->getSubscribers() as $subscriber) {
131
            $this->dispatcher->addSubscriber($subscriber);
132
        }
133
        $this->client->run();
134
        return 0;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function getDefaultCommands()
141
    {
142
        return array_merge(parent::getDefaultCommands(), [
0 ignored issues
show
Best Practice introduced by
The expression return array_merge(paren...d\InitCommand($this))); seems to be an array, but some of its elements' types (Spike\Client\Command\Spi...ent\Command\InitCommand) 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...
143
            new SpikeCommand($this),
144
            new ShowProxyHostsCommand($this),
145
            new InitCommand($this),
146
        ]);
147
    }
148
149
    /**
150
     * Gets all subscribers
151
     * @return array
152
     */
153
    public function getSubscribers()
154
    {
155
        return [
156
            $this,
157
            new LoggerSubscriber($this)
158
        ];
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    protected function getDefaultInputDefinition()
165
    {
166
        $definition = new InputDefinition([
167
            new InputOption('config', null, InputOption::VALUE_OPTIONAL,
168
                'The configuration file, support json,ini,xml and yaml format')
169
        ]);
170
        $defaultDefinition = parent::getDefaultInputDefinition();
171
        $definition->addArguments($defaultDefinition->getArguments());
172
        $definition->addOptions($defaultDefinition->getOptions());
173
        return $definition;
174
    }
175
}