diff --git a/CHANGELOG.md b/CHANGELOG.md index e744728..c925d27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ +### [0.6.4] - 2016-10-03 + + * refactoring + ### [0.6.3] - 2016-10-02 * cs fixes diff --git a/src/Tidal/WampWatch/Adapter/React/DeferredAdapter.php b/src/Tidal/WampWatch/Adapter/React/DeferredAdapter.php index 42eb9bc..21bf94c 100644 --- a/src/Tidal/WampWatch/Adapter/React/DeferredAdapter.php +++ b/src/Tidal/WampWatch/Adapter/React/DeferredAdapter.php @@ -12,7 +12,6 @@ namespace Tidal\WampWatch\Adapter\React; use Tidal\WampWatch\Async\DeferredInterface; -use Tidal\WampWatch\Async\PromiseInterface; use React\Promise\Deferred; class DeferredAdapter implements DeferredInterface @@ -70,7 +69,7 @@ public function getPromiseClass() } /** - * @return PromiseInterface + * @return PromiseAdapter */ public function promise() { @@ -102,7 +101,7 @@ public function notify($update = null) } /** - * @return PromiseInterface + * @return PromiseAdapter */ private function createPromise() { diff --git a/src/Tidal/WampWatch/Adapter/React/PromiseAdapter.php b/src/Tidal/WampWatch/Adapter/React/PromiseAdapter.php index 95758cf..b45fe4e 100644 --- a/src/Tidal/WampWatch/Adapter/React/PromiseAdapter.php +++ b/src/Tidal/WampWatch/Adapter/React/PromiseAdapter.php @@ -56,7 +56,19 @@ public function getAdaptee() */ public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) { - $this->adaptee->then($onFulfilled, $onRejected, $onProgress); + $this->adaptee->then(function () use ($onFulfilled) { + if ($onFulfilled !== null) { + return call_user_func_array($onFulfilled, func_get_args()); + } + }, function () use ($onRejected) { + if ($onRejected !== null) { + return call_user_func_array($onRejected, func_get_args()); + } + }, function () use ($onProgress) { + if ($onProgress !== null) { + return call_user_func_array($onProgress, func_get_args()); + } + }); return $this; } @@ -70,7 +82,19 @@ public function then(callable $onFulfilled = null, callable $onRejected = null, */ public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) { - $this->adaptee->done($onFulfilled, $onRejected, $onProgress); + $this->adaptee->done(function () use ($onFulfilled) { + if ($onFulfilled !== null) { + return call_user_func_array($onFulfilled, func_get_args()); + } + }, function () use ($onRejected) { + if ($onRejected !== null) { + return call_user_func_array($onRejected, func_get_args()); + } + }, function () use ($onProgress) { + if ($onProgress !== null) { + return call_user_func_array($onProgress, func_get_args()); + } + }); return $this; } @@ -82,7 +106,9 @@ public function done(callable $onFulfilled = null, callable $onRejected = null, */ public function otherwise(callable $onRejected) { - $this->adaptee->otherwise($onRejected); + $this->adaptee->otherwise(function () use ($onRejected) { + return call_user_func_array($onRejected, func_get_args()); + }); return $this; } @@ -94,7 +120,9 @@ public function otherwise(callable $onRejected) */ public function always(callable $onAlways) { - $this->adaptee->always($onAlways); + $this->adaptee->always(function () use ($onAlways) { + return call_user_func_array($onAlways, func_get_args()); + }); return $this; } @@ -106,7 +134,9 @@ public function always(callable $onAlways) */ public function progress(callable $onProgress) { - $this->adaptee->progress($onProgress); + $this->adaptee->progress(function () use ($onProgress) { + return call_user_func_array($onProgress, func_get_args()); + }); return $this; } diff --git a/src/Tidal/WampWatch/Adapter/Thruway/ClientSession.php b/src/Tidal/WampWatch/Adapter/Thruway/ClientSession.php index ecfedf6..a0fc0b8 100644 --- a/src/Tidal/WampWatch/Adapter/Thruway/ClientSession.php +++ b/src/Tidal/WampWatch/Adapter/Thruway/ClientSession.php @@ -13,6 +13,8 @@ use Thruway\ClientSession as ThruwaySession; use Tidal\WampWatch\ClientSessionInterface; +use React\Promise\Promise as ReactPromise; +use Tidal\WampWatch\Adapter\React\PromiseAdapter; class ClientSession implements ClientSessionInterface { @@ -33,11 +35,13 @@ public function __construct(ThruwaySession $thruwaySession) * @param callable $callback * @param $options array * - * @return \React\Promise\Promise + * @return PromiseAdapter */ public function subscribe($topicName, callable $callback, $options = null) { - return $this->thruwaySession->subscribe($topicName, $callback, $options); + return $this->createPromiseAdapter( + $this->thruwaySession->subscribe($topicName, $callback, $options) + ); } /** @@ -48,11 +52,13 @@ public function subscribe($topicName, callable $callback, $options = null) * @param array|mixed $argumentsKw * @param array|mixed $options * - * @return \React\Promise\Promise + * @return PromiseAdapter */ public function publish($topicName, $arguments = null, $argumentsKw = null, $options = null) { - return $this->thruwaySession->publish($topicName, $arguments, $argumentsKw, $options); + return $this->createPromiseAdapter( + $this->thruwaySession->publish($topicName, $arguments, $argumentsKw, $options) + ); } /** @@ -62,11 +68,13 @@ public function publish($topicName, $arguments = null, $argumentsKw = null, $opt * @param callable $callback * @param array|mixed $options * - * @return \React\Promise\Promise + * @return PromiseAdapter */ public function register($procedureName, callable $callback, $options = null) { - return $this->thruwaySession->register($procedureName, $callback, $options); + return $this->createPromiseAdapter( + $this->thruwaySession->register($procedureName, $callback, $options) + ); } /** @@ -74,11 +82,13 @@ public function register($procedureName, callable $callback, $options = null) * * @param string $procedureName * - * @return \React\Promise\Promise + * @return PromiseAdapter */ public function unregister($procedureName) { - return $this->thruwaySession->unregister($procedureName); + return $this->createPromiseAdapter( + $this->thruwaySession->unregister($procedureName) + ); } /** @@ -89,11 +99,13 @@ public function unregister($procedureName) * @param array|mixed $argumentsKw * @param array|mixed $options * - * @return \React\Promise\Promise + * @return PromiseAdapter */ public function call($procedureName, $arguments = null, $argumentsKw = null, $options = null) { - return $this->thruwaySession->call($procedureName, $arguments, $argumentsKw, $options); + return $this->createPromiseAdapter( + $this->thruwaySession->call($procedureName, $arguments, $argumentsKw, $options) + ); } /** @@ -112,8 +124,21 @@ public function getSessionId() return $this->thruwaySession->getSessionId(); } + /** + * @param $msg + */ public function sendMessage($msg) { $this->thruwaySession->sendMessage($msg); } + + /** + * @param ReactPromise $promise + * + * @return PromiseAdapter + */ + private function createPromiseAdapter(ReactPromise $promise) + { + return new PromiseAdapter($promise); + } } diff --git a/src/Tidal/WampWatch/Model/Contract/RouterInterface.php b/src/Tidal/WampWatch/Model/Contract/RouterInterface.php index e0b4ccb..3e4d616 100644 --- a/src/Tidal/WampWatch/Model/Contract/RouterInterface.php +++ b/src/Tidal/WampWatch/Model/Contract/RouterInterface.php @@ -16,7 +16,7 @@ interface RouterInterface { /** - * @return string; + * @return string */ public function getUri(); diff --git a/src/Tidal/WampWatch/MonitorTrait.php b/src/Tidal/WampWatch/MonitorTrait.php index 86255d9..83ac817 100644 --- a/src/Tidal/WampWatch/MonitorTrait.php +++ b/src/Tidal/WampWatch/MonitorTrait.php @@ -15,6 +15,9 @@ use React\Promise\Promise; use Tidal\WampWatch\ClientSessionInterface as ClientSession; use Tidal\WampWatch\Subscription\Collection as SubscriptionCollection; +use React\Promise\Deferred; +use Tidal\WampWatch\Adapter\React\PromiseAdapter; +use Tidal\WampWatch\Adapter\React\DeferredAdapter; /** * Description of MonitorTrait. @@ -188,4 +191,39 @@ private function getErrorCallback() return $error; }; } + + private function retrieveCallData($procedure, callable $filter = null, $arguments = []) + { + $deferred = new DeferredAdapter( + new Deferred() + ); + + $filter = $filter ?: function ($res) { + return $res; + }; + + $this->session->call($procedure, $arguments) + ->then( + function ($res) use ($deferred, $filter) { + $deferred->resolve($filter($res)); + }, + $this->getErrorCallback() + ); + + return $deferred->promise(); + } + + /** + * @param callable $callback + * + * @return PromiseAdapter + */ + private function createPromiseAdapter(callable $callback) + { + return new PromiseAdapter( + new Promise( + $callback + ) + ); + } } diff --git a/src/Tidal/WampWatch/SessionMonitor.php b/src/Tidal/WampWatch/SessionMonitor.php index f4e7e66..af99656 100644 --- a/src/Tidal/WampWatch/SessionMonitor.php +++ b/src/Tidal/WampWatch/SessionMonitor.php @@ -12,8 +12,8 @@ namespace Tidal\WampWatch; use Evenement\EventEmitterInterface; -use React\Promise\Promise; use Tidal\WampWatch\ClientSessionInterface as ClientSession; +use Tidal\WampWatch\Adapter\React\PromiseAdapter; /** * Description of SessionMonitor. @@ -62,7 +62,7 @@ public function __construct(ClientSession $session) * * @param $sessionId * - * @return \React\Promise\Promise; + * @return PromiseAdapter */ public function getSessionInfo($sessionId) { @@ -83,7 +83,7 @@ function ($error) { * registered on the wamp-router in the monitor's realm * and populates the data via given callback,. * - * @return Promise + * @return PromiseAdapter */ public function getSessionIds() { @@ -91,9 +91,11 @@ public function getSessionIds() return $this->retrieveSessionIds(); } - return new Promise(function (callable $resolve) { - $resolve($this->sessionIds); - }); + return $this->createPromiseAdapter( + function (callable $resolve) { + $resolve($this->getList()); + } + ); } /** @@ -189,34 +191,42 @@ protected function initSetupCalls() /** * Retrieves the list of current sessionIds on the router. * - * @return \React\Promise\Promise; + * @return PromiseAdapter */ protected function retrieveSessionIds() { - return $this->session->call(self::SESSION_LIST_TOPIC, []) - ->then( - $this->getSessionIdRetrievalCallback(), - $this->getErrorCallback() - ); + return $this->retrieveCallData( + self::SESSION_LIST_TOPIC, + $this->getSessionIdRetrievalCallback(), + [] + ); } + /** + * @return \Closure + */ protected function getSessionIdRetrievalCallback() { return function ($res) { - // remove our own sessionID from the tracked sessions - $sessionIds = $this->removeOwnSessionId($res[0]); - $this->setList($sessionIds); - $this->emit('list', [$this->getList()]); + $this->setList($res[0]); + $sessionIds = $this->getList(); + $this->emit('list', [$sessionIds]); - return $this->getList(); + return $sessionIds; }; } + /** + * @param $list + */ protected function setList($list) { - $this->sessionIds = $list; + $this->sessionIds = $this->removeOwnSessionId($list); } + /** + * @return array + */ protected function getList() { return $this->sessionIds; @@ -232,7 +242,8 @@ protected function getList() protected function removeOwnSessionId(array $sessionsIds) { $key = array_search($this->session->getSessionId(), $sessionsIds); - if ($key >= 0) { + + if ($key !== false && $key >= 0) { unset($sessionsIds[$key]); $sessionsIds = array_values($sessionsIds); } diff --git a/src/Tidal/WampWatch/Stub/ClientSessionStub.php b/src/Tidal/WampWatch/Stub/ClientSessionStub.php index 8149781..c3dc61b 100644 --- a/src/Tidal/WampWatch/Stub/ClientSessionStub.php +++ b/src/Tidal/WampWatch/Stub/ClientSessionStub.php @@ -26,6 +26,8 @@ use Tidal\WampWatch\ClientSessionInterface; use Tidal\WampWatch\Exception\UnknownProcedureException; use Tidal\WampWatch\Exception\UnknownTopicException; +use React\Promise\Promise; +use Tidal\WampWatch\Adapter\React\PromiseAdapter; /** * !!! WARNING !!!! @@ -104,7 +106,7 @@ class ClientSessionStub implements ClientSessionInterface, EventEmitterInterface * @param callable $callback * @param $options array * - * @return \React\Promise\Promise + * @return PromiseAdapter */ public function subscribe($topicName, callable $callback, $options = null) { @@ -119,7 +121,9 @@ public function subscribe($topicName, callable $callback, $options = null) $topicName ); - return $futureResult->promise(); + return $this->createPromiseAdapter( + $futureResult->promise() + ); } /** @@ -157,7 +161,7 @@ public function hasSubscription($topicName) * @param array|mixed $argumentsKw * @param array|mixed $options * - * @return \React\Promise\Promise + * @return PromiseAdapter */ public function publish($topicName, $arguments = null, $argumentsKw = null, $options = null) { @@ -172,7 +176,9 @@ public function publish($topicName, $arguments = null, $argumentsKw = null, $opt $argumentsKw ); - return $futureResult->promise(); + return $this->createPromiseAdapter( + $futureResult->promise() + ); } /** @@ -196,6 +202,11 @@ public function confirmPublication($topicName, $requestId = 1, $publicationId = $futureResult->resolve($result); } + /** + * @param string $topicName + * @param mixed $error + * @param int $requestId + */ public function failPublication($topicName, $error, $requestId = 1) { if (!isset($this->publications[$topicName])) { @@ -215,7 +226,7 @@ public function failPublication($topicName, $error, $requestId = 1) * @param callable $callback * @param array|mixed $options * - * @return \React\Promise\Promise + * @return PromiseAdapter */ public function register($procedureName, callable $callback, $options = null) { @@ -230,7 +241,9 @@ public function register($procedureName, callable $callback, $options = null) $procedureName ); - return $futureResult->promise(); + return $this->createPromiseAdapter( + $futureResult->promise() + ); } /** @@ -280,7 +293,7 @@ public function callRegistration($procedureName, array $args = []) * * @param string $procedureName * - * @return \React\Promise\PromiseInterface + * @return PromiseAdapter */ public function unregister($procedureName) { @@ -288,7 +301,9 @@ public function unregister($procedureName) $this->unregistrations[$procedureName] = $futureResult; - return $futureResult->promise(); + return $this->createPromiseAdapter( + $futureResult->promise() + ); } /** @@ -319,7 +334,7 @@ public function confirmUnregistration($procedureName, $requestId = 1) * @param array|mixed $argumentsKw * @param array|mixed $options * - * @return \React\Promise\Promise + * @return PromiseAdapter */ public function call($procedureName, $arguments = null, $argumentsKw = null, $options = null) { @@ -334,7 +349,9 @@ public function call($procedureName, $arguments = null, $argumentsKw = null, $op $argumentsKw ); - return $futureResult->promise(); + return $this->createPromiseAdapter( + $futureResult->promise() + ); } /** @@ -355,6 +372,10 @@ public function respondToCall($procedureName, $result) $futureResult->resolve($result); } + /** + * @param string $procedureName + * @param mixed $error + */ public function failCall($procedureName, $error) { if (!isset($this->calls[$procedureName])) { @@ -367,6 +388,11 @@ public function failCall($procedureName, $error) $futureResult->reject($error); } + /** + * @param string $procedureName + * + * @return bool + */ public function hasCall($procedureName) { return isset($this->calls[$procedureName]); @@ -411,4 +437,14 @@ public function sendMessage($msg) { return $msg; } + + /** + * @param Promise $promise + * + * @return PromiseAdapter + */ + private function createPromiseAdapter(Promise $promise) + { + return new PromiseAdapter($promise); + } } diff --git a/src/Tidal/WampWatch/SubscriptionMonitor.php b/src/Tidal/WampWatch/SubscriptionMonitor.php index c138d76..b991ce6 100644 --- a/src/Tidal/WampWatch/SubscriptionMonitor.php +++ b/src/Tidal/WampWatch/SubscriptionMonitor.php @@ -76,7 +76,7 @@ public function getSubscriptionIds() return $this->retrieveSubscriptionIds(); } - return new Promise(function (callable $resolve) { + return $this->createPromiseAdapter(function (callable $resolve) { $resolve($this->subscriptionIds); }); } @@ -117,11 +117,11 @@ private function getSubscriptionHandler($event) protected function retrieveSubscriptionIds() { - return $this->session->call(self::SUBSCRIPTION_LIST_TOPIC, []) - ->then( - $this->getSubscriptionIdRetrievalCallback(), - $this->getErrorCallback() - ); + return $this->retrieveCallData( + self::SUBSCRIPTION_LIST_TOPIC, + $this->getSubscriptionIdRetrievalCallback(), + [] + ); } protected function setList($list) diff --git a/tests/unit/ClientSessionStubTest.php b/tests/unit/ClientSessionStubTest.php index 2d522df..6f54b50 100644 --- a/tests/unit/ClientSessionStubTest.php +++ b/tests/unit/ClientSessionStubTest.php @@ -20,10 +20,10 @@ class ClientSessionStubTest extends \PHPUnit_Framework_TestCase { - const PROMISE_CLS = 'React\Promise\Promise'; + const PROMISE_CLS = 'Tidal\WampWatch\Adapter\React\PromiseAdapter'; /** - * @var Tidal\WampWatch\Stub\ClientSessionStub + * @var ClientSessionStub */ protected $session; diff --git a/tests/unit/SessionMonitorTest.php b/tests/unit/SessionMonitorTest.php index 1c5f93c..41fcf3f 100644 --- a/tests/unit/SessionMonitorTest.php +++ b/tests/unit/SessionMonitorTest.php @@ -208,7 +208,7 @@ public function test_list_event_getsessionid_callback_return_same_value() $calledBack = $res; }); - $stub->respondToCall(SessionMonitor::SESSION_LIST_TOPIC, [[321, 654, 987]]); + $stub->respondToCall(SessionMonitor::SESSION_LIST_TOPIC, [[123, 456, 789]]); $this->assertSame($listCalled, $calledBack); } @@ -232,7 +232,7 @@ public function test_get_sessionids_removes_monitors_sessionid() public function test_second_get_sessionids_retrieves_ids_locally() { $stub = new ClientSessionStub(); - $stub->setSessionId(321); + $stub->setSessionId(123); $monitor = new SessionMonitor($stub); $firstResult = null; $secondResult = null; diff --git a/tests/unit/SubscriptionMonitorTest.php b/tests/unit/SubscriptionMonitorTest.php index 22eff7d..2c7bdb5 100644 --- a/tests/unit/SubscriptionMonitorTest.php +++ b/tests/unit/SubscriptionMonitorTest.php @@ -131,7 +131,7 @@ public function test_start_event_after_running() { $stub = new ClientSessionStub(); $monitor = new SubscriptionMonitor($stub); - $subIdMap = $this->getSubscriptionIdMap(); + $subIdMap = $this->getCallResultMock(); $stub->setSessionId(321); $response = null; @@ -347,23 +347,24 @@ public function test_unsubscribe_event() // SUBSCRIPTION DETAIL TESTS - public function test_get_seesion_ids_returns_subscription_map() + public function test_get_subscription_ids_returns_subscription_map() { $stub = new ClientSessionStub(); $monitor = new SubscriptionMonitor($stub); $subIdMap = $this->getSubscriptionIdMap(); + $callResult = $this->getCallResultMock(); $res = null; $monitor->getSubscriptionIds()->done(function ($r) use (&$res) { $res = $r; }); - $stub->respondToCall(SubscriptionMonitor::SUBSCRIPTION_LIST_TOPIC, $subIdMap); + $stub->respondToCall(SubscriptionMonitor::SUBSCRIPTION_LIST_TOPIC, $callResult); - $this->assertEquals($subIdMap->getResultMessage()->getArguments()[0], $res); + $this->assertEquals($subIdMap, $res); } - public function test_2nd_get_seesion_ids_returns_subscription_map_locally() + public function test_2nd_get_subscription_ids_returns_subscription_map_locally() { $stub = new ClientSessionStub(); $monitor = new SubscriptionMonitor($stub); @@ -396,7 +397,7 @@ private function getSubscriptionInfo() private function getSubscriptionIdMap() { - return $this->getCallResultMock(); + return json_decode('{"exact": [321], "prefix": [654], "wildcard": [987]}'); } /** @@ -430,7 +431,7 @@ private function getResultMessageMock() ->method('getArguments') ->willReturn( [ - json_decode('{"exact": [321], "prefix": [654], "wildcard": [987]}'), + $this->getSubscriptionIdMap(), ] );