Completed
Push — master ( 1fe24e...c4923e )
by Pieter
03:31
created

Server::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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\BindContext;
11
use Amp\Socket\Server as SocketServer;
12
use Amp\Socket\ServerTlsContext;
13
use Amp\Websocket\Server\Websocket;
14
use Psr\Log\LoggerInterface;
15
use ekinhbayar\GitAmp\Event\Factory as EventFactory;
16
use ekinhbayar\GitAmp\Provider\GitHub;
17
use ekinhbayar\GitAmp\Response\Factory as EventCollectionFactory;
18
use ekinhbayar\GitAmp\Websocket\Handler;
19
20
final class Server
21
{
22
    private LoggerInterface $logger;
23
24
    private Configuration $configuration;
25
26 1
    public function __construct(LoggerInterface $logger, Configuration $configuration)
27
    {
28 1
        $this->logger        = $logger;
29 1
        $this->configuration = $configuration;
30
    }
31
32 1
    public function start(): Promise
33
    {
34 1
        $server = new HttpServer($this->getSockets(), $this->getRouter(), $this->logger);
35
36 1
        return $server->start();
37
    }
38
39
    /**
40
     * @return array<SocketServer>
41
     */
42 1
    private function getSockets(): array
43
    {
44 1
        $sockets = array_map(
45 1
            fn (ServerAddress $address) => SocketServer::listen($address->getUri()),
46 1
            $this->configuration->getServerAddresses(),
47
        );
48
49 1
        $sockets = array_merge($sockets, array_map(
50 1
            fn (SslServerAddress $address) => SocketServer::listen(
51
                $address->getUri(),
52
                (new BindContext())
53
                    ->withTlsContext((new ServerTlsContext())->withDefaultCertificate($address->getCertificate())),
54 1
            ),
55 1
            $this->configuration->getSslServerAddresses(),
56
        ));
57
58 1
        return $sockets;
59
    }
60
61 1
    private function getRouter(): Router
62
    {
63 1
        $router = new Router();
64
65 1
        $router->addRoute('GET', '/ws', $this->getWebSocket());
66 1
        $router->setFallback(new DocumentRoot(__DIR__ . '/../public'));
67
68 1
        return $router;
69
    }
70
71 1
    private function getWebSocket(): Websocket
72
    {
73 1
        $gitHubListener = new GitHub(
74 1
            HttpClientBuilder::buildDefault(),
75 1
            $this->configuration->getGithubToken(),
76 1
            new EventCollectionFactory(new EventFactory($this->configuration->getSpecialRepositories()), $this->logger),
77 1
            $this->logger,
78
        );
79
80 1
        $clientHandler = new Handler($gitHubListener, $this->configuration, $this->logger);
81
82 1
        return new Websocket($clientHandler);
83
    }
84
}
85