Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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