Completed
Push — master ( 7f5313...51c1be )
by Ekin
03:37
created

Server   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 52
ccs 23
cts 23
cp 1
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 5 1
A getSockets() 0 5 1
A getRouter() 0 8 1
A __construct() 0 4 1
A getWebSocket() 0 12 1
1
<?php declare(strict_types=1);
2
3
namespace ekinhbayar\GitAmp;
4
5
use Amp\Http\Client\HttpClientBuilder;
6
use Amp\Http\Server\HttpServer;
7
use Amp\Http\Server\Router;
8
use Amp\Http\Server\StaticContent\DocumentRoot;
9
use Amp\Promise;
10
use Amp\Socket\Server as SocketServer;
11
use Amp\Websocket\Server\Websocket;
12
use ekinhbayar\GitAmp\Event\Factory as EventFactory;
13
use ekinhbayar\GitAmp\Provider\GitHub;
14
use ekinhbayar\GitAmp\Response\Factory as EventCollectionFactory;
15
use ekinhbayar\GitAmp\Websocket\Handler;
16
use Psr\Log\LoggerInterface;
17
18
final class Server
19
{
20
    private LoggerInterface $logger;
21
22
    private Configuration $configuration;
23
24 1
    public function __construct(LoggerInterface $logger, Configuration $configuration)
25
    {
26 1
        $this->logger        = $logger;
27 1
        $this->configuration = $configuration;
28
    }
29
30 1
    public function start(): Promise
31
    {
32 1
        $server = new HttpServer($this->getSockets(), $this->getRouter(), $this->logger);
33
34 1
        return $server->start();
35
    }
36
37
    /**
38
     * @return array<SocketServer>
39
     */
40 1
    private function getSockets(): array
41
    {
42 1
        return array_map(
43 1
            fn (ServerAddress $address) => SocketServer::listen($address->getUri()),
44 1
            $this->configuration->getServerAddresses(),
45
        );
46
    }
47
48 1
    private function getRouter(): Router
49
    {
50 1
        $router = new Router();
51
52 1
        $router->addRoute('GET', '/ws', $this->getWebSocket());
53 1
        $router->setFallback(new DocumentRoot(__DIR__ . '/../public'));
54
55 1
        return $router;
56
    }
57
58 1
    private function getWebSocket(): Websocket
59
    {
60 1
        $gitHubListener = new GitHub(
61 1
            HttpClientBuilder::buildDefault(),
62 1
            $this->configuration->getGithubToken(),
63 1
            new EventCollectionFactory(new EventFactory(), $this->logger),
64 1
            $this->logger,
65
        );
66
67 1
        $clientHandler = new Handler($gitHubListener, $this->configuration, $this->logger);
68
69 1
        return new Websocket($clientHandler);
70
    }
71
}
72