EventController::onOpen()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 5
nop 2
1
<?php
2
3
namespace BeyondCode\DuskDashboard\Ratchet\Http;
4
5
use BeyondCode\DuskDashboard\Ratchet\Socket;
6
use Exception;
7
use GuzzleHttp\Psr7\Response;
8
use function GuzzleHttp\Psr7\str;
9
use Psr\Http\Message\RequestInterface;
10
use Ratchet\ConnectionInterface;
11
12
class EventController extends Controller
13
{
14
    public function onOpen(ConnectionInterface $conn, RequestInterface $request = null)
15
    {
16
        try {
17
18
            /*
19
             * This is the post payload from our PHPUnit tests.
20
             * Send it to the connected connections.
21
             */
22
            foreach (Socket::$connections as $connection) {
23
                $connection->send($request->getBody());
0 ignored issues
show
Bug introduced by
It seems like $request is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
24
            }
25
26
            $conn->send(str(new Response(200)));
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Psr7\str() has been deprecated with message: str will be removed in guzzlehttp/psr7:2.0. Use Message::toString instead.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
27
        } catch (Exception $e) {
28
            $conn->send(str(new Response(500, [], $e->getMessage())));
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Psr7\str() has been deprecated with message: str will be removed in guzzlehttp/psr7:2.0. Use Message::toString instead.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
29
        }
30
31
        $conn->close();
32
    }
33
}
34