Test Failed
Push — master ( 13c8bf...f549ef )
by Florian
11:18
created

CakeORMHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 148
rs 10
c 0
b 0
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 11 2
A gc() 0 5 1
A destroy() 0 8 1
A setTimeout() 0 5 1
A __construct() 0 4 1
A close() 0 3 1
A read() 0 24 4
A open() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
0 ignored issues
show
Coding Style introduced by
The file-level docblock must follow the opening PHP tag in the file header
Loading history...
6
 * Database Session save handler. Allows saving session information into a model.
7
 *
8
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
9
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
10
 *
11
 * Licensed under The MIT License
12
 * For full copyright and license information, please see the LICENSE.txt
13
 * Redistributions of files must retain the above copyright notice.
14
 *
15
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
16
 * @link          https://cakephp.org CakePHP(tm) Project
17
 * @since         2.0.0
18
 * @license       https://opensource.org/licenses/mit-license.php MIT License
19
 */
20
21
namespace Phauthentic\Infrastructure\Http\Session\Handler;
22
23
use Cake\Datasource\RepositoryInterface;
0 ignored issues
show
Bug introduced by
The type Cake\Datasource\RepositoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Cake\ORM\Entity;
0 ignored issues
show
Bug introduced by
The type Cake\ORM\Entity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Cake\ORM\Locator\LocatorAwareTrait;
0 ignored issues
show
Bug introduced by
The type Cake\ORM\Locator\LocatorAwareTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use SessionHandlerInterface;
27
28
/**
29
 * DatabaseSession provides methods to be used with Session.
30
 */
31
class CakeORMHandler implements SessionHandlerInterface
32
{
33
    /*
34
    use LocatorAwareTrait;
35
    */
36
    /**
37
     * Reference to the table handling the session data
38
     *
39
     * @var \Cake\ORM\Table
0 ignored issues
show
Bug introduced by
The type Cake\ORM\Table was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
     */
41
    protected $_table;
42
43
    /**
44
     * Number of seconds to mark the session as expired
45
     *
46
     * @var int
47
     */
48
    protected $_timeout;
49
50
    /**
51
     * Constructor. Looks at Session configuration information and
52
     * sets up the session model.
53
     *
54
     * @param array $config The configuration for this engine. It requires the 'model'
55
     * key to be present corresponding to the Table to use for managing the sessions.
56
     */
57
    public function __construct(RepositoryInterface $repository)
58
    {
59
        $this->_table = $repository;
60
        $this->_timeout = (int)ini_get('session.gc_maxlifetime');
61
    }
62
63
    /**
64
     * Set the timeout value for sessions.
65
     *
66
     * Primarily used in testing.
67
     *
68
     * @param int $timeout The timeout duration.
69
     * @return $this
70
     */
71
    public function setTimeout($timeout)
72
    {
73
        $this->_timeout = $timeout;
74
75
        return $this;
76
    }
77
78
    /**
79
     * Method called on open of a database session.
80
     *
81
     * @param string $savePath The path where to store/retrieve the session.
82
     * @param string $name The session name.
83
     * @return bool Success
84
     */
85
    public function open($savePath, $name)
86
    {
87
        return true;
88
    }
89
90
    /**
91
     * Method called on close of a database session.
92
     *
93
     * @return bool Success
94
     */
95
    public function close()
96
    {
97
        return true;
98
    }
99
100
    /**
101
     * Method used to read from a database session.
102
     *
103
     * @param string|int $id ID that uniquely identifies session in database.
104
     * @return string Session data or empty string if it does not exist.
105
     */
106
    public function read($id)
107
    {
108
        $result = $this->_table
109
            ->find('all')
110
            ->select(['data'])
111
            ->where([$this->_table->getPrimaryKey() => $id])
112
            ->enableHydration(false)
113
            ->first();
114
115
        if (empty($result)) {
116
            return '';
117
        }
118
119
        if (is_string($result['data'])) {
120
            return $result['data'];
121
        }
122
123
        $session = stream_get_contents($result['data']);
124
125
        if ($session === false) {
126
            return '';
127
        }
128
129
        return $session;
130
    }
131
132
    /**
133
     * Helper function called on write for database sessions.
134
     *
135
     * @param string|int $id ID that uniquely identifies session in database.
136
     * @param mixed $data The data to be saved.
137
     * @return bool True for successful write, false otherwise.
138
     */
139
    public function write($id, $data)
140
    {
141
        if (!$id) {
142
            return false;
143
        }
144
        $expires = time() + $this->_timeout;
145
        $record = compact('data', 'expires');
146
        $record[$this->_table->getPrimaryKey()] = $id;
147
        $result = $this->_table->save(new Entity($record));
148
149
        return (bool)$result;
150
    }
151
152
    /**
153
     * Method called on the destruction of a database session.
154
     *
155
     * @param string|int $id ID that uniquely identifies session in database.
156
     * @return bool True for successful delete, false otherwise.
157
     */
158
    public function destroy($id)
159
    {
160
        $this->_table->delete(new Entity(
161
            [$this->_table->getPrimaryKey() => $id],
162
            ['markNew' => false]
163
        ));
164
165
        return true;
166
    }
167
168
    /**
169
     * Helper function called on gc for database sessions.
170
     *
171
     * @param int $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
172
     * @return bool True on success, false on failure.
173
     */
174
    public function gc($maxlifetime)
175
    {
176
        $this->_table->deleteAll(['expires <' => time()]);
177
178
        return true;
179
    }
180
}
181