Passed
Push — 2.x.x ( e27d4f...264fd4 )
by Koldo
03:30
created

DriftKernelAdapter::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\React;
6
7
use Antidot\Application\Http\Application;
8
use Drift\Console\OutputPrinter;
9
use Drift\Server\Adapter\KernelAdapter;
10
use Drift\Server\Context\ServerContext;
11
use Drift\Server\Mime\MimeTypeChecker;
12
use Psr\Container\ContainerInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use React\EventLoop\LoopInterface;
15
use React\Filesystem\FilesystemInterface;
0 ignored issues
show
Bug introduced by
The type React\Filesystem\FilesystemInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use React\Promise\PromiseInterface;
17
use function React\Promise\resolve;
18
19
final class DriftKernelAdapter implements KernelAdapter
20
{
21
    private ServerContext $serverContext;
22
    private MimeTypeChecker $mimeTypeChecker;
23
    private string $rootPath;
24
    private ContainerInterface $container;
25
    private ReactApplication $application;
26
    /** @var object|FilesystemInterface|null */
27
    private ?object $filesystem = null;
28
29
    /**
30
     * DriftKernelAdapter constructor.
31
     * @param ServerContext $serverContext
32
     * @param MimeTypeChecker $mimeTypeChecker
33
     * @param string $rootPath
34
     * @psalm-suppress UndefinedClass
35
     * @param FilesystemInterface|null $filesystem
36
     */
37
    public function __construct(
38
        ServerContext $serverContext,
39
        MimeTypeChecker $mimeTypeChecker,
40
        string $rootPath,
41
        ?FilesystemInterface $filesystem
42
    ) {
43
        $container = require $rootPath . '/config/container.php';
44
        assert($container instanceof ContainerInterface);
45
        $application = $container->get(Application::class);
46
        assert($application instanceof ReactApplication);
47
        (require $rootPath . '/router/middleware.php')($application, $container);
48
        (require $rootPath . '/router/routes.php')($application, $container);
49
        $this->container = $container;
50
        $this->application = $application;
51
        $this->serverContext = $serverContext;
52
        $this->filesystem = $filesystem;
53
        $this->mimeTypeChecker = $mimeTypeChecker;
54
        $this->rootPath = $rootPath;
55
    }
56
57
    /** @psalm-suppress MixedReturnTypeCoercion */
58
    public static function create(
59
        LoopInterface $loop,
60
        string $rootPath,
61
        ServerContext $serverContext,
62
        OutputPrinter $outputPrinter,
63
        MimeTypeChecker $mimeTypeChecker,
64
        ?FilesystemInterface $filesystem = null
65
    ): PromiseInterface {
66
        if ($filesystem && !class_exists(FilesystemInterface::class)) {
67
            throw new \RuntimeException('Install react/filesystem package.');
68
        }
69
70
        return resolve(new self($serverContext, $mimeTypeChecker, $rootPath, $filesystem))
71
            ->then(fn (KernelAdapter $adapter): KernelAdapter => $adapter);
72
    }
73
74
    /**
75
     * @psalm-suppress LessSpecificImplementedReturnType
76
     * @param ServerRequestInterface $request
77
     * @return PromiseResponse
78
     */
79
    public function handle(ServerRequestInterface $request): PromiseResponse
80
    {
81
        return $this->application->handle($request);
82
    }
83
84
    public static function getStaticFolder(): ?string
85
    {
86
        return 'public';
87
    }
88
89
    public function shutDown(): PromiseInterface
90
    {
91
        return resolve('nothing to do');
92
    }
93
94
    /**
95
     * Get watcher folders.
96
     *
97
     * @return string[]
98
     */
99
    public static function getObservableFolders(): array
100
    {
101
        return ['src', 'public', 'templates'];
102
    }
103
104
    /**
105
     * Get watcher folders.
106
     *
107
     * @return string[]
108
     */
109
    public static function getObservableExtensions(): array
110
    {
111
        return ['php', 'yml', 'yaml', 'xml', 'css', 'js', 'html', 'twig'];
112
    }
113
114
    /**
115
     * Get watcher ignoring folders.
116
     *
117
     * @return string[]
118
     */
119
    public static function getIgnorableFolders(): array
120
    {
121
        return ['var'];
122
    }
123
}
124