Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 1 | Rev 16954 | 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 LoggerInterface $logger
39
     * @param int $session_lifetime
40
     */
41
    public function __construct(AdapterInterface $adapter, LoggerInterface $logger, int $session_lifetime)
42
    {
43
        $this->sessionMapper = SessionMapper::getInstance($adapter, $logger);
44
        $this->session_lifetime = $session_lifetime;
45
    }
16766 efrain 46
 
47
    /**
48
     *
49
     * {@inheritDoc}
50
     * @see SessionHandlerInterface::close()
51
     */
52
    public function close() : bool
1 www 53
    {
54
        return true;
55
    }
16766 efrain 56
 
57
    /**
58
     *
59
     * {@inheritDoc}
60
     * @see SessionHandlerInterface::destroy()
61
     */
62
    public function destroy(string $id) : bool
1 www 63
    {
16766 efrain 64
        $this->sessionMapper->delete($id);
1 www 65
        return true;
66
    }
67
 
16766 efrain 68
    /**
69
     *
70
     * {@inheritDoc}
71
     * @see SessionHandlerInterface::gc()
72
     */
73
    public function gc(int $max_lifetime) : int
1 www 74
    {
16766 efrain 75
        return $this->sessionMapper->deleteAllExpired($max_lifetime);
76
    }
77
 
78
    /**
79
     *
80
     * {@inheritDoc}
81
     * @see SessionHandlerInterface::open()
82
     */
83
    public function open(string $path, string $name) : bool
84
    {
85
        $this->session_name = $name;
86
        return true;
87
    }
88
 
89
    /**
90
     *
91
     * {@inheritDoc}
92
     * @see SessionHandlerInterface::read()
93
     */
94
    public function read(string $id) : string
95
    {
96
        $session = $this->sessionMapper->fetchOne($id);
97
        if($session && $session->data){
98
            return base64_decode($session->data);
1 www 99
        }else{
100
            return '';
101
        }
102
    }
16766 efrain 103
 
104
    /**
105
     *
106
     * {@inheritDoc}
107
     * @see SessionHandlerInterface::write()
108
     */
109
    public function write(string $id, string $data) : bool
1 www 110
    {
16766 efrain 111
        $data = base64_encode($data);
112
 
113
        $session = $this->read($id);
1 www 114
        if($session) {
16766 efrain 115
            return $this->sessionMapper->update($id, $data);
1 www 116
        } else {
16766 efrain 117
            return $this->sessionMapper->insert($id, $this->session_name, $data, $this->session_lifetime);
1 www 118
        }
119
    }
120
 
121
 
122
}