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

Application::createErrorResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
/**
3
 * Spike library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Spike\Server;
7
8
use Spike\Application as BaseApplication;
9
use GuzzleHttp\Psr7\Response;
10
use Slince\Event\SubscriberInterface;
11
use Spike\Server\Command\InitCommand;
12
use Spike\Server\Command\SpikeCommand;
13
use Spike\Server\Subscriber\LoggerSubscriber;
14
use Spike\Logger\Logger;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class Application extends BaseApplication implements SubscriberInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    const NAME = 'spike-server';
25
26
    /**
27
     * @var string
28
     */
29
    const VERSION = '1.0.0.dev';
30
31
    /**
32
     * @var Server
33
     */
34
    protected $server;
35
36
    public function __construct(Configuration $configuration)
37
    {
38
        parent::__construct($configuration,static::NAME, static::VERSION);
39
        $this->server = new Server(
40
            $configuration->getAddress(),
41
            $configuration->getAuthentication(),
42
            null,
43
            $this->dispatcher
44
        );
45
    }
46
47
    public function doRun(InputInterface $input, OutputInterface $output)
48
    {
49
        $this->input = $input;
50
        $this->output = $output;
51
        //Logger
52
        $this->logger = new Logger(
53
            $this->getConfiguration()->getLogLevel(),
54
            $this->getConfiguration()->getLogFile(),
55
            $this->output
56
        );
57
        $this->server->setLogger($this->logger);
58
        $commandName = $input->getFirstArgument();
59
        if ($commandName) {
60
            $exitCode = parent::doRun($input, $output);
61
        } else {
62
            $exitCode = $this->doRunServer();
63
        }
64
        return $exitCode;
65
    }
66
67
    protected function doRunServer()
68
    {
69
        if (true === $this->input->hasParameterOption(array('--help', '-h'), true)) {
70
            $command = $this->get('spike');
71
            $exitCode = $this->doRunCommand($command, $this->input, $this->output);
72
        } else {
73
            $exitCode = $this->runServer();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $exitCode is correct as $this->runServer() (which targets Spike\Server\Application::runServer()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
74
        }
75
        return $exitCode;
76
    }
77
78
    /**
79
     * Start the server
80
     */
81
    protected function runServer()
82
    {
83
        foreach ($this->getSubscribers() as $subscriber) {
84
            $this->dispatcher->addSubscriber($subscriber);
85
        }
86
        $this->server->run();
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getEvents()
93
    {
94
        return [
95
        ];
96
    }
97
98
    /**
99
     * Gets all subscribers
100
     * @return array
101
     */
102
    public function getSubscribers()
103
    {
104
        return [
105
            $this,
106
            new LoggerSubscriber($this)
107
        ];
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getDefaultCommands()
114
    {
115
        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\Server\Command\Spi...ver\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...
116
            new SpikeCommand($this),
117
            new InitCommand($this)
118
        ]);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    protected function getDefaultInputDefinition()
125
    {
126
        $definition = parent::getDefaultInputDefinition();
127
        $definition->addOption(new InputOption('config', null, InputOption::VALUE_OPTIONAL,
128
            'The configuration file, support json,ini,xml and yaml format'));
129
        $definition->addOption(new InputOption('address', null, InputOption::VALUE_OPTIONAL,
130
            'The ip address that bind to'));
131
        return $definition;
132
    }
133
}