Completed
Pull Request — 1.x (#357)
by Akihito
03:02 queued 01:33
created

Bootstrap::__invoke()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 3
nc 5
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Compiler;
6
7
use BEAR\Package\Compiler\Module\App;
8
use BEAR\Package\Injector;
9
use BEAR\Resource\ResourceObject;
10
use BEAR\Sunday\Extension\Application\AppInterface;
11
use Throwable;
12
13
use function assert;
14
use function dirname;
15
16
/**
17
 * @psalm-import-type Globals from \BEAR\Sunday\Extension\Router\RouterInterface
18
 * @psalm-import-type Server from \BEAR\Sunday\Extension\Router\RouterInterface
19
 */
20
final class Bootstrap
21
{
22
    /**
23
     * @psalm-param Globals $globals
24
     * @psalm-param Server  $server
25
     * @phpstan-param array<string, mixed> $globals
26
     * @phpstan-param array<string, mixed> $server
27
     *
28
     * @return 0|1
0 ignored issues
show
Documentation introduced by
The doc-type 0|1 could not be parsed: Unknown type name "0" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
29
     */
30
    public function __invoke(string $context, array $globals, array $server): int
31
    {
32
        $tmpDir = dirname(__DIR__, 2) . '/tests/tmp';
33
        $app = Injector::getInstance('BEAR\Package\Compiler', $context, $tmpDir)->getInstance(AppInterface::class);
34
        assert($app instanceof App);
35
        if ($app->httpCache->isNotModified($server)) {
36
            $app->httpCache->transfer();
37
38
            return 0;
39
        }
40
41
        $request = $app->router->match($globals, $server);
42
        try {
43
            /** @psalm-suppress all */
44
            $response = $app->resource->{$request->method}->uri($request->path)($request->query);
45
            assert($response instanceof ResourceObject);
46
            $response->transfer($app->responder, $server);
47
48
            return 0;
49
        } catch (Throwable $e) {
50
            $app->throwableHandler->handle($e, $request)->transfer();
51
52
            return 1;
53
        }
54
    }
55
}
56