<?php
namespace ModulesAbstract\FluxRest; class FluXEmulatorWithAutomaticSessionManagement extends FluXEmulator { protected $storage = null; protected $applicationToken = null; protected $authenticationToken = null; public function setStorageObj($storage) { $this->storage = $storage; return $this; } public function restore() { $cacheKey = sprintf("%s:%s", __CLASS__, $this->wsEndpoint); $internalState = $this->storage->get($cacheKey); if ($internalState && $internalState['serverSentVars'] && $internalState['applicationToken']) { $this->serverSentVars = $internalState['serverSentVars']; $this->instanceSessionId = $internalState['instanceSessionId']; $this->agent = $internalState['agent']; $this->authenticationToken = $internalState['authenticationToken']; $this->applicationToken = $internalState['applicationToken']; return true; } return false; } public function save() { $cacheKey = sprintf("%s:%s", __CLASS__, $this->wsEndpoint); $internalState = [ 'lastAccess' => microtime(true), 'serverSentVars' => $this->serverSentVars, 'instanceSessionId' => $this->instanceSessionId, 'agent' => $this->agent, 'authenticationToken' => $this->authenticationToken, 'applicationToken' => $this->applicationToken, ]; $this->storage->set($cacheKey, $internalState); } protected function buildResponseHandler($rawResponse) { if ($rawResponse->action == 'Authenticate') { $handler = new \ModulesAbstract\FluxRest\ResponseHandler\Operation\Authenticate($this, $this->logger); $handler->appToken = $this->applicationToken; $this->authenticationToken = null; return $handler; } return parent::buildResponseHandler($rawResponse); } protected function requestRaw($request) { if ($request->action=='Authenticate' && !$this->applicationToken) { $this->applicationToken = $request->appToken; } return parent::requestRaw($request); } } 