aboutsummaryrefslogtreecommitdiff
path: root/lib/RssBridge.php
blob: 230488bf9e6254899f6d62a3f7f6e87950449f06 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php

final class RssBridge
{
    private static Container $container;

    public function __construct(
        Container $container
    ) {
        self::$container = $container;
    }

    public function main(Request $request): Response
    {
        $action = $request->get('action', 'Frontpage');
        $actionName = strtolower($action) . 'Action';
        $actionName = implode(array_map('ucfirst', explode('-', $actionName)));
        $filePath = __DIR__ . '/../actions/' . $actionName . '.php';
        if (!file_exists($filePath)) {
            return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'Invalid action']), 400);
        }

        $handler = self::$container[$actionName];

        $middlewares = [
            new SecurityMiddleware(),
            new MaintenanceMiddleware(),
            new BasicAuthMiddleware(),
            new TokenAuthenticationMiddleware(),
        ];
        $action = function ($req) use ($handler) {
            return $handler($req);
        };
        foreach (array_reverse($middlewares) as $middleware) {
            $action = fn ($req) => $middleware($req, $action);
        }
        return $action($request);
    }

    public static function getLogger(): Logger
    {
        // null logger is only for the tests not to fail
        return self::$container['logger'] ?? new NullLogger();
    }

    public static function getCache(): CacheInterface
    {
        return self::$container['cache'];
    }

    public static function getHttpClient(): HttpClient
    {
        return self::$container['http_client'];
    }
}