Passed
Push — master ( 6ce97f...1fe2f9 )
by huang
05:13
created

Server   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 42.86%

Importance

Changes 0
Metric Value
dl 0
loc 175
rs 10
c 0
b 0
f 0
ccs 30
cts 70
cp 0.4286
wmc 22
lcom 1
cbo 5

13 Methods

Rating   Name   Duplication   Size   Complexity  
A stop() 0 4 1
A watch() 0 4 1
A restart() 0 4 1
A reload() 0 4 1
A status() 0 4 1
C run() 0 28 7
A __construct() 0 17 1
A getSwoole() 0 4 1
A bootstrap() 0 4 1
A initListeners() 0 13 3
A initProcesses() 0 9 2
A daemon() 0 6 1
A start() 0 6 1
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      https://fastdlabs.com
8
 */
9
10
namespace FastD;
11
12
use FastD\ServiceProvider\SwooleServiceProvider;
13
use FastD\Servitization\Server\HTTPServer;
14
use swoole_server;
15
use Symfony\Component\Console\Input\InputInterface;
16
17
/**
18
 * Class App.
19
 */
20
class Server
21
{
22
    /**
23
     * @var Application
24
     */
25
    protected $application;
26
27
    /**
28
     * @var \FastD\Swoole\Server
29
     */
30
    protected $server;
31
32
    /**
33
     * Server constructor.
34
     *
35
     * @param Application $application
36
     */
37 1
    public function __construct(Application $application)
38
    {
39 1
        $application->register(new SwooleServiceProvider());
40
41 1
        $server = config()->get('server.class', HTTPServer::class);
42
43 1
        $this->server = $server::createServer(
44 1
            $application->getName(),
45 1
            config()->get('server.host'),
46 1
            config()->get('server.options', [])
47 1
        );
48
49 1
        $application->add('server', $this->server);
50
51 1
        $this->initListeners();
52 1
        $this->initProcesses();
53 1
    }
54
55
    /**
56
     * @return swoole_server
57
     */
58
    public function getSwoole()
59
    {
60
        return $this->server->getSwoole();
61
    }
62
63
    /**
64
     * @return Swoole\Server
65
     */
66 1
    public function bootstrap()
67
    {
68 1
        return $this->server->bootstrap();
69
    }
70
71
    /**
72
     * @return $this
73
     */
74 1
    public function initListeners()
75
    {
76 1
        $listeners = config()->get('server.listeners', []);
77 1
        foreach ($listeners as $listener) {
78 1
            $this->server->listen(new $listener['class'](
79 1
                app()->getName().' ports',
80 1
                $listener['host'],
81 1
                isset($listener['options']) ? $listener['options'] : []
82 1
            ));
83 1
        }
84
85 1
        return $this;
86
    }
87
88
    /**
89
     * @return $this
90
     */
91 1
    public function initProcesses()
92
    {
93 1
        $processes = config()->get('server.processes', []);
94 1
        foreach ($processes as $process) {
95 1
            $this->server->process(new $process(app()->getName().' process'));
96 1
        }
97
98 1
        return $this;
99
    }
100
101
    /**
102
     * @return $this
103
     */
104
    public function daemon()
105
    {
106
        $this->server->daemon();
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return int
113
     */
114
    public function start()
115
    {
116
        $this->bootstrap();
117
118
        return $this->server->start();
119
    }
120
121
    /**
122
     * @return int
123
     */
124
    public function stop()
125
    {
126
        return $this->server->shutdown();
127
    }
128
129
    /**
130
     * @return int
131
     */
132
    public function restart()
133
    {
134
        return $this->server->restart();
135
    }
136
137
    /**
138
     * @return int
139
     */
140
    public function reload()
141
    {
142
        return $this->server->reload();
143
    }
144
145
    /**
146
     * @return int
147
     */
148
    public function status()
149
    {
150
        return $this->server->status();
151
    }
152
153
    /**
154
     * @param array $dir
155
     *
156
     * @return int
157
     */
158
    public function watch(array $dir = ['.'])
159
    {
160
        return $this->server->watch($dir);
161
    }
162
163
    /**
164
     * @param InputInterface $input
165
     */
166
    public function run(InputInterface $input)
167
    {
168
        if ($input->hasParameterOption(['--daemon', '-d'], true)) {
169
            $this->daemon();
170
        }
171
172
        switch ($input->getArgument('action')) {
173
            case 'start':
174
                if ($input->hasParameterOption(['--dir'])) {
175
                    $this->watch([$input->getOption('dir')]);
176
                } else {
177
                    $this->start();
178
                }
179
180
                break;
181
            case 'stop':
182
                $this->stop();
183
184
                break;
185
            case 'reload':
186
                $this->reload();
187
188
                break;
189
            case 'status':
190
            default:
191
                $this->status();
192
        }
193
    }
194
}
195