Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16766 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Handler;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Log\LoggerInterface;
9
use LeadersLinked\Mapper\SessionMapper;
10
use Laminas\Session\SaveHandler\SaveHandlerInterface;
16766 efrain 11
use SessionHandlerInterface;
1 www 12
 
13
 
14
class SessionHandler implements SaveHandlerInterface
15
{
16
 
17
    /**
18
     *
19
     * @var SessionMapper
20
     */
21
    private $sessionMapper;
22
 
23
    /**
24
     *
25
     * @var string
26
     */
27
    private $session_name;
28
 
29
    /**
30
     *
31
     * @var int
32
     */
33
    private $session_lifetime;
34
 
35
    /**
36
     *
37
     * @param AdapterInterface $adapter
38
     * @param int $session_lifetime
39
     */
16954 efrain 40
    public function __construct(AdapterInterface $adapter, int $session_lifetime)
1 www 41
    {
16954 efrain 42
        $this->sessionMapper = SessionMapper::getInstance($adapter);
1 www 43
        $this->session_lifetime = $session_lifetime;
44
    }
16766 efrain 45
 
46
    /**
47
     *
48
     * {@inheritDoc}
49
     * @see SessionHandlerInterface::close()
50
     */
51
    public function close() : bool
1 www 52
    {
53
        return true;
54
    }
16766 efrain 55
 
56
    /**
57
     *
58
     * {@inheritDoc}
59
     * @see SessionHandlerInterface::destroy()
60
     */
61
    public function destroy(string $id) : bool
1 www 62
    {
16766 efrain 63
        $this->sessionMapper->delete($id);
1 www 64
        return true;
65
    }
66
 
16766 efrain 67
    /**
68
     *
69
     * {@inheritDoc}
70
     * @see SessionHandlerInterface::gc()
71
     */
72
    public function gc(int $max_lifetime) : int
1 www 73
    {
16766 efrain 74
        return $this->sessionMapper->deleteAllExpired($max_lifetime);
75
    }
76
 
77
    /**
78
     *
79
     * {@inheritDoc}
80
     * @see SessionHandlerInterface::open()
81
     */
82
    public function open(string $path, string $name) : bool
83
    {
84
        $this->session_name = $name;
85
        return true;
86
    }
87
 
88
    /**
89
     *
90
     * {@inheritDoc}
91
     * @see SessionHandlerInterface::read()
92
     */
93
    public function read(string $id) : string
94
    {
95
        $session = $this->sessionMapper->fetchOne($id);
96
        if($session && $session->data){
97
            return base64_decode($session->data);
1 www 98
        }else{
99
            return '';
100
        }
101
    }
16766 efrain 102
 
103
    /**
104
     *
105
     * {@inheritDoc}
106
     * @see SessionHandlerInterface::write()
107
     */
108
    public function write(string $id, string $data) : bool
1 www 109
    {
16766 efrain 110
        $data = base64_encode($data);
111
 
112
        $session = $this->read($id);
1 www 113
        if($session) {
16766 efrain 114
            return $this->sessionMapper->update($id, $data);
1 www 115
        } else {
16766 efrain 116
            return $this->sessionMapper->insert($id, $this->session_name, $data, $this->session_lifetime);
1 www 117
        }
118
    }
119
 
120
 
121
}