Test Failed
Pull Request — master (#67)
by
unknown
05:53
created

Server::useQueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 5
cp 0
crap 2
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\Process\Queue;
13
use FastD\ServiceProvider\SwooleServiceProvider;
14
use FastD\Servitization\Server\HTTPServer;
15
use swoole_server;
16
use Symfony\Component\Console\Input\InputInterface;
17
18
/**
19
 * Class App.
20
 */
21
class Server
22
{
23
    /**
24
     * @var Application
25
     */
26
    protected $application;
27
28
    /**
29
     * @var \FastD\Swoole\Server
30
     */
31
    protected $server;
32
33
    /**
34
     * Server constructor.
35
     *
36
     * @param Application $application
37
     */
38 1
    public function __construct(Application $application)
39
    {
40 1
        $application->register(new SwooleServiceProvider());
41
42 1
        $server = config()->get('server.class', HTTPServer::class);
43
44 1
        $this->server = $server::createServer(
45 1
            $application->getName(),
46 1
            config()->get('server.host'),
47 1
            config()->get('server.options', [])
48 1
        );
49
50 1
        $this->initListeners();
51 1
        $this->initProcesses();
52 1
    }
53
54
    /**
55
     * @return swoole_server
56
     */
57
    public function getSwoole()
58
    {
59
        return $this->server->getSwoole();
60
    }
61
62
    /**
63
     * @return Swoole\Server
64
     */
65 1
    public function bootstrap()
66
    {
67 1
        return $this->server->bootstrap();
68
    }
69
70
    /**
71
     * @return $this
72
     */
73 1
    public function initListeners()
74
    {
75 1
        $listeners = config()->get('server.listeners', []);
76 1
        foreach ($listeners as $listener) {
77 1
            $this->server->listen(new $listener['class'](
78 1
                app()->getName().' ports',
79 1
                $listener['host'],
80 1
                isset($listener['options']) ? $listener['options'] : []
81 1
            ));
82 1
        }
83
84 1
        return $this;
85
    }
86
87
    /**
88
     * @return $this
89
     */
90 1
    public function initProcesses()
91
    {
92 1
        $processes = config()->get('server.processes', []);
93 1
        foreach ($processes as $process) {
94 1
            $this->server->process(new $process(app()->getName().' process'));
95 1
        }
96
97 1
        return $this;
98
    }
99
100
    /**
101
     * @return $this
102
     */
103
    public function daemon()
104
    {
105
        $this->server->daemon();
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return $this
112
     */
113
    public function useQueue()
114
    {
115
        $queue = new Queue();
116
117
        app()->add('queue', $queue);
118
119
        $this->server->process($queue);
120
121
        return $this;
122
    }
123
124
    /**
125
     * @return int
126
     */
127
    public function start()
128
    {
129
        $server = $this->bootstrap();
130
131
        app()->add('server', $server->getSwoole());
132
133
        return $this->server->start();
134
    }
135
136
    /**
137
     * @return int
138
     */
139
    public function stop()
140
    {
141
        return $this->server->shutdown();
142
    }
143
144
    /**
145
     * @return int
146
     */
147
    public function restart()
148
    {
149
        return $this->server->restart();
150
    }
151
152
    /**
153
     * @return int
154
     */
155
    public function reload()
156
    {
157
        return $this->server->reload();
158
    }
159
160
    /**
161
     * @return int
162
     */
163
    public function status()
164
    {
165
        return $this->server->status();
166
    }
167
168
    /**
169
     * @param array $dir
170
     *
171
     * @return int
172
     */
173
    public function watch(array $dir = ['.'])
174
    {
175
        return $this->server->watch($dir);
176
    }
177
178
    /**
179
     * @param InputInterface $input
180
     */
181
    public function run(InputInterface $input)
182
    {
183
        if ($input->hasParameterOption(['--daemon', '-d'], true)) {
184
            $this->daemon();
185
        }
186
187
        if ($input->hasParameterOption(['--queue', '-q'], true)) {
188
            $this->useQueue();
189
        }
190
191
        switch ($input->getArgument('action')) {
192
            case 'start':
193
                if ($input->hasParameterOption(['--dir'])) {
194
                    $this->watch([$input->getOption('dir')]);
195
                } else {
196
                    $this->start();
197
                }
198
199
                break;
200
            case 'stop':
201
                $this->stop();
202
203
                break;
204
            case 'reload':
205
                $this->reload();
206
207
                break;
208
            case 'status':
209
            default:
210
                $this->status();
211
        }
212
    }
213
}
214