Completed
Push — 1.x ( c86fd3...4b8bd5 )
by Akihito
14s queued 12s
created

Bootstrap   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 14
c 1
b 0
f 0
dl 0
loc 36
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 18 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Compiler;
6
7
use BEAR\AppMeta\AbstractAppMeta;
8
use BEAR\Package\Injector;
9
use BEAR\Resource\ResourceInterface;
10
use BEAR\Sunday\Extension\Router\RouterInterface;
11
use BEAR\Sunday\Extension\Transfer\HttpCacheInterface;
12
use BEAR\Sunday\Extension\Transfer\TransferInterface;
13
use Throwable;
14
15
/**
16
 * @psalm-import-type Globals from RouterInterface
17
 * @psalm-import-type Server from RouterInterface
18
 */
19
20
final class Bootstrap
21
{
22
    /** @var string */
23
    private $appDir;
24
25
    public function __construct(AbstractAppMeta $meta)
26
    {
27
        $this->appDir = $meta->appDir;
28
    }
29
30
    /**
31
     * @psalm-param Globals $globals
32
     * @psalm-param Server  $server
33
     * @phpstan-param array<string, mixed> $globals
34
     * @phpstan-param array<string, mixed> $server
35
     *
36
     * @return 0|1
0 ignored issues
show
Documentation Bug introduced by
The doc comment 0|1 at position 0 could not be parsed: Unknown type name '0' at position 0 in 0|1.
Loading history...
37
     */
38
    public function __invoke(string $appName, string $context, array $globals, array $server): int
39
    {
40
        $injector =  Injector::getInstance($appName, $context, $this->appDir);
41
        $injector->getInstance(HttpCacheInterface::class);
42
        $router = $injector->getInstance(RouterInterface::class);
43
        $request = $router->match($globals, $server);
44
        try {
45
            /** @psalm-suppress all */
46
            $resource = $injector->getInstance(ResourceInterface::class);
47
            $resource->{$request->method}->uri($request->path)($request->query);
48
        } catch (Throwable $e) {
49
            $injector->getInstance(TransferInterface::class);
50
51
            return 1;
52
        }
53
54
        // @codeCoverageIgnoreStart
55
        return 0;
56
    }
57
}
58