Server   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 10
dl 0
loc 37
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A listen() 0 7 1
A httpd() 0 13 2
1
<?php
2
/**
3
 * HTTP Server
4
 * User: moyo
5
 * Date: 28/09/2017
6
 * Time: 4:40 PM
7
 */
8
9
namespace Carno\HTTP;
10
11
use Carno\HTTP\Powered\Native\Server as NVServer;
12
use Carno\HTTP\Powered\Swoole\Server as SWServer;
13
use Carno\HTTP\Server\Connection;
14
use Carno\HTTP\Standard\Response;
15
use Carno\Net\Address;
16
use Carno\Net\Contracts\HTTP;
17
use Carno\Net\Events;
18
use Closure;
19
use Throwable;
20
21
class Server
22
{
23
    /**
24
     * @param Address $address
25
     * @param Events $events
26
     * @param int $workers
27
     * @param string $serviced
28
     * @return HTTP
29
     */
30
    public static function listen(
31
        Address $address,
32
        Events $events,
33
        int $workers,
34
        string $serviced = 'server'
35
    ) : HTTP {
36
        return (new SWServer($serviced))->listen($address, $events, $workers);
37
    }
38
39
    /**
40
     * @param Address $listen
41
     * @param Closure $processor
42
     * @param string $serviced
43
     * @return HTTP
44
     */
45
    public static function httpd(
46
        Address $listen,
47
        Closure $processor,
48
        string $serviced = 'server'
49
    ) : HTTP {
50
        return new NVServer(
51
            $serviced,
52
            $listen,
53
            (new Events)->attach(Events\HTTP::REQUESTING, function (Connection $conn) use ($processor) {
54
                try {
55
                    $processor($conn);
56
                } catch (Throwable $e) {
57
                    $conn->reply(new Response(500, [], $e->getTraceAsString()));
58
                }
59
            })
60
        );
61
    }
62
}
63