Passed
Pull Request — master (#125)
by Dmitriy
12:55
created

DevServerController::stream()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 33
ccs 0
cts 21
cp 0
rs 9.2888
c 0
b 0
f 0
cc 5
nc 2
nop 1
crap 30
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Api\Controller;
6
7
use Psr\Http\Message\ResponseFactoryInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Yiisoft\Http\Header;
10
use Yiisoft\Yii\Debug\Api\ServerSentEventsStream;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Debug\Api\ServerSentEventsStream 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...
11
use Yiisoft\Yii\Debug\DevServer\Connection;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Debug\DevServer\Connection 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...
12
13
final class DevServerController
14
{
15
    public function stream(
16
        ResponseFactoryInterface $responseFactory
17
    ): ResponseInterface {
18
        if (\function_exists('pcntl_signal')) {
19
            \pcntl_signal(\SIGINT, static function (): void {
20
                exit(1);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
21
            });
22
        }
23
24
        $socket = Connection::create();
25
        $socket->bind();
26
27
        return $responseFactory->createResponse()
28
            ->withHeader(Header::CONTENT_TYPE, 'text/event-stream')
29
            ->withHeader(Header::CACHE_CONTROL, 'no-cache')
30
            ->withHeader(Header::CONNECTION, 'keep-alive')
31
            ->withBody(
32
                new ServerSentEventsStream(function () use ($socket) {
33
                    foreach ($socket->read() as $message) {
34
                        switch ($message[0]) {
35
                            case Connection::TYPE_ERROR:
36
                                return '';
37
                            default:
38
                                /**
39
                                 * Break the loop if the client aborted the connection (closed the page)
40
                                 */
41
                                if (connection_aborted()) {
42
                                    return $message[1];
43
                                }
44
                                yield $message[1];
45
                        }
46
                    }
47
                    return '';
48
                })
49
            );
50
    }
51
}
52