According to Flow documentation Session behaves like a singleton, whenever a session is started the object is automatically serialized and saved in current user’s session. I followed the similar process provided here http://flowframework.readthedocs.io/en/stable/TheDefinitiveGuide/PartIII/SessionHandling.html but it seems to be not working for me.
Here is what I am doing. I have a service which connects to an external website to retrieve the token, now I want that token to be stored in the session so it does not have to retrieve it again (saving a round trip to server here)
The expected behavior according to the document, is that the protected property $token of MagentoRestClient should be serialized and stored in the session, but it gets reset and not retrieved unless I misunderstood it or I am doing something wrong here?
/**
* @Flow\Scope("session")
*/
class MagentoRestClient
{
/**
* @var \GuzzleHttp\Client;
* //@Flow\Transient
*/
protected $client = null;
/**
* [$token description]
* @var string
*/
protected $token = '';
/**
* @todo baseUri/username/password pass through configuration
* @param string $baseUri [description]
*
* @Flow\Session(autoStart = TRUE)
*/
public function connect(string $baseUri = 'http://magento2.local', string $userName = 'neos', string $password = 'neos123' )
{
//TODO inject through Dependency Injection
$this->client = new Client(['base_uri' => $baseUri]);
if(!$this->token) {
try {
$response = $this->client->request('POST', '/rest/V1/integration/admin/token', [
'json' => [
'username' => $userName,
'password' => $password
]
]);
$this->token = str_replace('"', '', $response->getBody()->getContents());
} catch(ClientException $e) {
var_dump($e->getResponse());
}
}
else {
var_dump($this->token);
}
}
public function setToken(string $token)
{
$this->token = $token;
}
public function getToken()
{
return $this->token;
}
public function getClient()
{
return $this->client;
}
}
This is how I injected MagentoRestClient service in my controller.
class StandardController extends ActionController
{
/**
* @var MagentoRestClient
* @Flow\Inject
*/
protected $client;
/**
*
* @return void
*/
public function indexAction()
{
try {
if(!$this->client->getToken()) { var_dump('connect'); //Token is not stored in session...
$this->client->connect();
}
$response = $this->client->getClient()->request('GET', '/rest/V1/categories/2/products', [
'headers' => [
'Authorization' => 'Bearer ' . $this->client->getToken()
]
]);
$productList = json_decode(($response->getBody()->read(2048)));
} catch(ClientException $e) {
//var_dump($e->getResponse());
}
$this->view->assign('productList', $productList);
}