Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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