Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16954 | | 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
    {
16971 efrain 74
        if($this->sessionMapper->deleteAllExpired($max_lifetime)) {
75
            return $this->sessionMapper->getAffectedRows();
76
        } else {
77
            return 0;
78
        }
16766 efrain 79
    }
80
 
81
    /**
82
     *
83
     * {@inheritDoc}
84
     * @see SessionHandlerInterface::open()
85
     */
86
    public function open(string $path, string $name) : bool
87
    {
88
        $this->session_name = $name;
89
        return true;
90
    }
91
 
92
    /**
93
     *
94
     * {@inheritDoc}
95
     * @see SessionHandlerInterface::read()
96
     */
97
    public function read(string $id) : string
98
    {
99
        $session = $this->sessionMapper->fetchOne($id);
100
        if($session && $session->data){
101
            return base64_decode($session->data);
1 www 102
        }else{
103
            return '';
104
        }
105
    }
16766 efrain 106
 
107
    /**
108
     *
109
     * {@inheritDoc}
110
     * @see SessionHandlerInterface::write()
111
     */
112
    public function write(string $id, string $data) : bool
1 www 113
    {
16766 efrain 114
        $data = base64_encode($data);
115
 
116
        $session = $this->read($id);
1 www 117
        if($session) {
16766 efrain 118
            return $this->sessionMapper->update($id, $data);
1 www 119
        } else {
16766 efrain 120
            return $this->sessionMapper->insert($id, $this->session_name, $data, $this->session_lifetime);
1 www 121
        }
122
    }
123
 
124
 
125
}