Passed
Push — master ( 18bd2b...fc5ac5 )
by Craig
09:29
created

DoctrineSessionHandler   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 147
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A setStorage() 0 4 1
A open() 0 4 1
A close() 0 4 1
A read() 0 13 4
A write() 0 20 3
A destroy() 0 8 2
A gc() 0 12 2
A getCurrentIp() 0 7 3
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\Bridge\HttpFoundation;
13
14
use Symfony\Component\HttpFoundation\RequestStack;
15
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
16
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
17
use Zikula\UsersModule\Constant;
18
use Zikula\UsersModule\Entity\RepositoryInterface\UserSessionRepositoryInterface;
19
use Zikula\UsersModule\Entity\UserSessionEntity;
20
21
/**
22
 * Class DoctrineSessionHandler
23
 */
24
class DoctrineSessionHandler implements \SessionHandlerInterface
25
{
26
    /**
27
     * @var SessionStorageInterface
28
     */
29
    private $storage;
30
31
    /**
32
     * @var UserSessionRepositoryInterface
33
     */
34
    private $userSessionRepository;
35
36
    /**
37
     * @var VariableApiInterface
38
     */
39
    private $variableApi;
40
41
    /**
42
     * @var RequestStack
43
     */
44
    private $requestStack;
45
46
    /**
47
     * @var bool is Zikula installed?
48
     */
49
    private $installed;
50
51
    /**
52
     * @param UserSessionRepositoryInterface $userSessionRepository
53
     * @param VariableApiInterface $variableApi
54
     * @param $installed
55
     */
56
    public function __construct(
57
        UserSessionRepositoryInterface $userSessionRepository,
58
        VariableApiInterface $variableApi,
59
        RequestStack $requestStack,
60
        $installed
61
    ) {
62
        $this->userSessionRepository = $userSessionRepository;
63
        $this->variableApi = $variableApi;
64
        $this->requestStack = $requestStack;
65
        $this->installed = $installed;
66
    }
67
68
    public function setStorage(SessionStorageInterface $storage)
69
    {
70
        $this->storage = $storage;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function open($savePath, $sessionName)
77
    {
78
        return true;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function close()
85
    {
86
        return true;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function read($sessionId)
93
    {
94
        if (!$this->installed) {
95
            return '';
96
        }
97
98
        $sessionEntity = $this->userSessionRepository->find($sessionId);
99
        if ($sessionEntity) {
100
            $vars = $sessionEntity->getVars();
101
        }
102
103
        return !empty($vars) ? $vars : '';
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function write($sessionId, $vars)
110
    {
111
        if (!$this->installed) {
112
            return true;
113
        }
114
115
        $sessionEntity = $this->userSessionRepository->find($sessionId);
116
        if (!$sessionEntity) {
117
            $sessionEntity = new UserSessionEntity();
118
        }
119
        $sessionEntity->setSessid($sessionId);
120
        $sessionEntity->setIpaddr($this->getCurrentIp());
121
        $sessionEntity->setLastused(date('Y-m-d H:i:s', $this->storage->getMetadataBag()->getLastUsed()));
122
        $sessionEntity->setUid($this->storage->getBag('attributes')->get('uid', Constant::USER_ID_ANONYMOUS));
123
        $sessionEntity->setRemember($this->storage->getBag('attributes')->get('rememberme', 0));
124
        $sessionEntity->setVars($vars);
125
        $this->userSessionRepository->persistAndFlush($sessionEntity);
126
127
        return true;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function destroy($sessionId)
134
    {
135
        // expire the cookie
136
        if (php_sapi_name() != 'cli') {
137
            setcookie(session_name(), '', 0, ini_get('session.cookie_path'));
138
        }
139
        $this->userSessionRepository->removeAndFlush($sessionId);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function gc($lifetime)
146
    {
147
        if (!$this->installed) {
148
            return true;
149
        }
150
151
        return $this->userSessionRepository->gc(
152
            $this->variableApi->getSystemVar('seclevel', 'Medium'),
153
            $this->variableApi->getSystemVar('secinactivemins', 20),
154
            $this->variableApi->getSystemVar('secmeddays', 7)
155
        );
156
    }
157
158
    /**
159
     * find the current IP address
160
     * @param string $default
161
     * @return string
162
     */
163
    private function getCurrentIp($default = '127.0.0.1')
164
    {
165
        $ipAddress = $this->requestStack->getCurrentRequest()->getClientIp();
166
        $ipAddress = !empty($ipAddress) ? $ipAddress : $this->requestStack->getCurrentRequest()->server->get('HTTP_HOST');
167
168
        return !empty($ipAddress) ? $ipAddress : $default;
169
    }
170
}
171