diff --git a/psalm.xml b/psalm.xml index 3240886..8822f66 100644 --- a/psalm.xml +++ b/psalm.xml @@ -5,6 +5,8 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" + findUnusedBaselineEntry="true" + findUnusedCode="false" > diff --git a/src/Request/RequestType.php b/src/Request/RequestType.php index 8ac4b20..c92c495 100644 --- a/src/Request/RequestType.php +++ b/src/Request/RequestType.php @@ -4,6 +4,8 @@ namespace RoadRunner\Centrifugo\Request; +use RoadRunner\Centrifugo\Exception\InvalidRequestTypeException; + enum RequestType: string { case Connect = 'connect'; @@ -13,4 +15,17 @@ enum RequestType: string case Subscribe = 'subscribe'; case RPC = 'rpc'; case Invalid = 'invalid'; + + public static function createFrom(RequestInterface $request): self + { + return match (true) { + $request instanceof Connect => RequestType::Connect, + $request instanceof Subscribe => RequestType::Subscribe, + $request instanceof Refresh => RequestType::Refresh, + $request instanceof SubRefresh => RequestType::SubRefresh, + $request instanceof Publish => RequestType::Publish, + $request instanceof RPC => RequestType::RPC, + $request instanceof Invalid => RequestType::Invalid, + }; + } } diff --git a/tests/Unit/Request/RequestTypeTest.php b/tests/Unit/Request/RequestTypeTest.php new file mode 100644 index 0000000..39ddaab --- /dev/null +++ b/tests/Unit/Request/RequestTypeTest.php @@ -0,0 +1,66 @@ +assertSame($expected, RequestType::createFrom($request)); + } + + public static function requestTypeDataProvider(): \Traversable + { + $worker = \Mockery::mock(\Spiral\RoadRunner\WorkerInterface::class); + + yield 'Connect' => [ + new Connect($worker, '', '', '', '', [], '', '', [], []), + RequestType::Connect, + ]; + + yield 'Subscribe' => [ + new Subscribe($worker, '', '', '', '', '', '', '', [], [], []), + RequestType::Subscribe, + ]; + + yield 'Refresh' => [ + new Refresh($worker, '', '', '', '', '', [], []), + RequestType::Refresh, + ]; + + yield 'SubRefresh' => [ + new SubRefresh($worker, '', '', '', '', '', '', [], []), + RequestType::SubRefresh, + ]; + + yield 'Publish' => [ + new Publish($worker, '', '', '', '', '', '', [], [], []), + RequestType::Publish, + ]; + + yield 'RPC' => [ + new RPC($worker, '', '', '', '', '', '', [], [], []), + RequestType::RPC, + ]; + + yield 'Invalid' => [ + new Invalid(new \Exception('Test Exception')), + RequestType::Invalid, + ]; + } +}