Rev 16954 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Handler;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Log\LoggerInterface;
use LeadersLinked\Mapper\SessionMapper;
use Laminas\Session\SaveHandler\SaveHandlerInterface;
use SessionHandlerInterface;
class SessionHandler implements SaveHandlerInterface
{
/**
*
* @var SessionMapper
*/
private $sessionMapper;
/**
*
* @var string
*/
private $session_name;
/**
*
* @var int
*/
private $session_lifetime;
/**
*
* @param AdapterInterface $adapter
* @param int $session_lifetime
*/
public function __construct(AdapterInterface $adapter, int $session_lifetime)
{
$this->sessionMapper = SessionMapper::getInstance($adapter);
$this->session_lifetime = $session_lifetime;
}
/**
*
* {@inheritDoc}
* @see SessionHandlerInterface::close()
*/
public function close() : bool
{
return true;
}
/**
*
* {@inheritDoc}
* @see SessionHandlerInterface::destroy()
*/
public function destroy(string $id) : bool
{
$this->sessionMapper->delete($id);
return true;
}
/**
*
* {@inheritDoc}
* @see SessionHandlerInterface::gc()
*/
public function gc(int $max_lifetime) : int
{
if($this->sessionMapper->deleteAllExpired($max_lifetime)) {
return $this->sessionMapper->getAffectedRows();
} else {
return 0;
}
}
/**
*
* {@inheritDoc}
* @see SessionHandlerInterface::open()
*/
public function open(string $path, string $name) : bool
{
$this->session_name = $name;
return true;
}
/**
*
* {@inheritDoc}
* @see SessionHandlerInterface::read()
*/
public function read(string $id) : string
{
$session = $this->sessionMapper->fetchOne($id);
if($session && $session->data){
return base64_decode($session->data);
}else{
return '';
}
}
/**
*
* {@inheritDoc}
* @see SessionHandlerInterface::write()
*/
public function write(string $id, string $data) : bool
{
$data = base64_encode($data);
$session = $this->read($id);
if($session) {
return $this->sessionMapper->update($id, $data);
} else {
return $this->sessionMapper->insert($id, $this->session_name, $data, $this->session_lifetime);
}
}
}