Passed
Pull Request — master (#661)
by Maxim
07:57
created

HttpBootloader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Bootloader\Http;
13
14
use Composer\InstalledVersions;
15
use Psr\Container\ContainerInterface;
16
use Psr\Http\Message\ResponseFactoryInterface;
17
use Psr\Http\Server\RequestHandlerInterface;
18
use Spiral\Boot\AbstractKernel;
19
use Spiral\Boot\Bootloader\Bootloader;
20
use Spiral\Boot\KernelInterface;
21
use Spiral\Bootloader\ServerBootloader;
22
use Spiral\Config\ConfiguratorInterface;
23
use Spiral\Config\Patch\Append;
24
use Spiral\Core\Container\SingletonInterface;
25
use Spiral\Core\FactoryInterface;
26
use Spiral\Http\Config\HttpConfig;
27
use Spiral\Http\Emitter\SapiEmitter;
28
use Spiral\Http\EmitterInterface;
29
use Spiral\Http\Http;
30
use Spiral\Http\LegacyRrDispatcher;
31
use Spiral\Http\Pipeline;
32
use Spiral\Http\RrDispatcher;
33
use Spiral\Http\SapiDispatcher;
34
use Spiral\RoadRunner\Http\PSR7Worker;
0 ignored issues
show
Bug introduced by
The type Spiral\RoadRunner\Http\PSR7Worker 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...
35
use Spiral\RoadRunner\PSR7Client;
36
37
/**
38
 * Configures Http dispatcher in SAPI and RoadRunner modes (if available).
39
 */
40
final class HttpBootloader extends Bootloader implements SingletonInterface
41
{
42
    protected const DEPENDENCIES = [
43
        ServerBootloader::class,
44
    ];
45
46
    protected const SINGLETONS = [
47
        Http::class             => [self::class, 'httpCore'],
48
        EmitterInterface::class => [self::class, 'createEmitter'],
49
    ];
50
51
    /** @var ConfiguratorInterface */
52
    private $config;
53
54
    /**
55
     * @param ConfiguratorInterface $config
56
     */
57 200
    public function __construct(ConfiguratorInterface $config)
58
    {
59 200
        $this->config = $config;
60
    }
61
62
    /**
63
     * @param KernelInterface  $kernel
64
     * @param FactoryInterface $factory
65
     */
66 198
    public function boot(AbstractKernel $kernel, FactoryInterface $factory): void
67
    {
68 198
        $this->config->setDefaults(
69
            HttpConfig::CONFIG,
70
            [
71 198
                'basePath'   => '/',
72
                'headers'    => [
73
                    'Content-Type' => 'text/html; charset=UTF-8',
74
                ],
75
                'middleware' => [],
76
                'chunkSize' => null,
77
            ]
78
        );
79
80
        // Lowest priority
81 198
        $kernel->started(static function (AbstractKernel $kernel) use ($factory): void {
82 198
            $kernel->addDispatcher($factory->make(SapiDispatcher::class));
0 ignored issues
show
Bug introduced by
It seems like $factory->make(Spiral\Http\SapiDispatcher::class) can also be of type null; however, parameter $dispatcher of Spiral\Boot\AbstractKernel::addDispatcher() does only seem to accept Spiral\Boot\DispatcherInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
            $kernel->addDispatcher(/** @scrutinizer ignore-type */ $factory->make(SapiDispatcher::class));
Loading history...
83
        });
84
85 198
        if (!class_exists('Spiral\RoadRunnerBridge\Http\Dispatcher')) {
86 198
            if (class_exists(PSR7Client::class)) {
87 198
                $kernel->addDispatcher($factory->make(LegacyRrDispatcher::class));
0 ignored issues
show
Bug introduced by
It seems like $factory->make(Spiral\Ht...acyRrDispatcher::class) can also be of type null; however, parameter $dispatcher of Spiral\Boot\AbstractKernel::addDispatcher() does only seem to accept Spiral\Boot\DispatcherInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
                $kernel->addDispatcher(/** @scrutinizer ignore-type */ $factory->make(LegacyRrDispatcher::class));
Loading history...
88
            }
89
90 198
            if (class_exists(PSR7Worker::class)) {
91
                $kernel->addDispatcher($factory->make(RrDispatcher::class));
92
            }
93
        }
94
    }
95
96
    /**
97
     * Register new http middleware.
98
     *
99
     * @param mixed $middleware
100
     */
101 192
    public function addMiddleware($middleware): void
102
    {
103 192
        $this->config->modify('http', new Append('middleware', null, $middleware));
104
    }
105
106 2
    public function createEmitter(HttpConfig $config): EmitterInterface
107
    {
108 2
        $emitter = new SapiEmitter();
109
110 2
        if (($chunkSize = $config->getChunkSize()) !== null) {
111 1
            $emitter->bufferSize = $chunkSize;
112
        }
113
114 2
        return $emitter;
115
    }
116
117
    /**
118
     * @param HttpConfig               $config
119
     * @param Pipeline                 $pipeline
120
     * @param RequestHandlerInterface  $handler
121
     * @param ResponseFactoryInterface $responseFactory
122
     * @param ContainerInterface       $container
123
     * @return Http
124
     */
125 74
    protected function httpCore(
126
        HttpConfig $config,
127
        Pipeline $pipeline,
128
        RequestHandlerInterface $handler,
129
        ResponseFactoryInterface $responseFactory,
130
        ContainerInterface $container
131
    ): Http {
132 74
        $core = new Http($config, $pipeline, $responseFactory, $container);
133 74
        $core->setHandler($handler);
134
135 74
        return $core;
136
    }
137
}
138