Test Failed
Push — main ( b6e144...1a2341 )
by Rafael
05:34
created

Session::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Alxarafe. Development of PHP applications in a flash!
4
 * Copyright (C) 2018-2020 Alxarafe <[email protected]>
5
 */
6
7
namespace Alxarafe\Core\Singletons;
8
9
use Alxarafe\Core\Utils\ClassUtils;
10
use Aura\Session\Segment;
11
use Aura\Session\SessionFactory;
12
13
/**
14
 * Class Session.
15
 * Tools for managing sessions, including session segments and read-once messages
16
 *
17
 * @package Alxarafe\Core\Helpers
18
 */
19
abstract class Session
20
{
21
    /**
22
     * Información de sesión de la cookie.
23
     *
24
     * @var \Aura\Session\Session
25
     */
26
    protected static \Aura\Session\Session $session;
27
28
    /**
29
     * Nombre del segmento utilizado.
30
     *
31
     * @var string
32
     */
33
    protected static string $segmentName = 'Alxarafe';
34
35
    /**
36
     * Inicia la sesión.
37
     *
38
     * @author Rafael San José Tovar <[email protected]>
39
     * @source https://github.com/auraphp/Aura.Session#cross-site-request-forgery
40
     *
41
     * @param string $index
42
     */
43
    public static function load(string $index = 'main')
0 ignored issues
show
Unused Code introduced by
The parameter $index is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

43
    public static function load(/** @scrutinizer ignore-unused */ string $index = 'main')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45
        self::$session = (new SessionFactory())->newInstance($_COOKIE);
46
        if (session_status() === PHP_SESSION_NONE) {
47
            self::$session->start();
48
        }
49
    }
50
51
    /**
52
     * Obtiene un valor para el token CSRF.
53
     *
54
     * @author Rafael San José Tovar <[email protected]>
55
     *
56
     * @return string
57
     */
58
    public static function getCsrfToken(): string
59
    {
60
        return self::$session->getCsrfToken()->getValue();
61
    }
62
63
    /**
64
     * Comprueba si el token es válido.
65
     *
66
     * @author Rafael San José Tovar <[email protected]>
67
     *
68
     * @param string $csrfToken
69
     *
70
     * @return bool
71
     */
72
    public static function isValid(string $csrfToken): bool
73
    {
74
        return self::$session->getCsrfToken()->isValid($csrfToken);
75
    }
76
77
    /**
78
     * Retorna una instancia de la sesión.
79
     *
80
     * @author Rafael San José Tovar <[email protected]>
81
     *
82
     * @return \Aura\Session\Session
83
     */
84
    public static function getSession(): \Aura\Session\Session
85
    {
86
        return self::$session;
87
    }
88
89
    /**
90
     * Establece un nombre de segmento
91
     *
92
     * @author Rafael San José Tovar <[email protected]>
93
     *
94
     * @param string $segmentName
95
     *
96
     * @return static
97
     */
98
    public static function setSegment(string $segmentName): self
99
    {
100
        self::$segmentName = $segmentName;
101
        return self();
102
    }
103
104
    /**
105
     * Obtiene los datos del segmento
106
     *
107
     * @author Rafael San José Tovar <[email protected]>
108
     *
109
     * @param string $key
110
     *
111
     * @return mixed
112
     */
113
    public function get(string $key)
114
    {
115
        return self::getSegment()->get($key);
116
    }
117
118
    /**
119
     * Obtiene el segmento de sesión
120
     *
121
     * @author Rafael San José Tovar <[email protected]>
122
     *
123
     * @return Segment
124
     */
125
    public static function getSegment(): Segment
126
    {
127
        return self::$session->getSegment(self::$segmentName);
128
    }
129
130
    /**
131
     * Establece un valor a una clave.
132
     *
133
     * @author Rafael San José Tovar <[email protected]>
134
     *
135
     * @param string $key
136
     * @param        $value
137
     *
138
     * @return $this
139
     */
140
    public function set(string $key, $value): self
141
    {
142
        self::getSegment()->set($key, $value);
143
        return $this;
144
    }
145
146
    /**
147
     * Establece un valor a una clave 'flash'
148
     *
149
     * @author Rafael San José Tovar <[email protected]>
150
     *
151
     * @param string $key
152
     * @param        $value
153
     *
154
     * @return $this
155
     */
156
    public function setFlash(string $key, $value): self
157
    {
158
        $this->setFlashNext($key, $value);
159
        return $this;
160
    }
161
162
    /**
163
     * Establece un valor a una clave, para la próxima sesión
164
     *
165
     * @author Rafael San José Tovar <[email protected]>
166
     *
167
     * @param string $key
168
     * @param        $value
169
     */
170
    public static function setFlashNext(string $key, $value)
171
    {
172
        self::getSegment()->setFlash($key, $value);
173
    }
174
175
    /**
176
     * Obtiene datos de una clave actual
177
     *
178
     * @author Rafael San José Tovar <[email protected]>
179
     *
180
     * @param string $key
181
     *
182
     * @return mixed
183
     */
184
    public static function getFlash(string $key)
185
    {
186
        return self::getFlashNow($key);
187
    }
188
189
    /**
190
     * Obtiene datos de una clave actual
191
     *
192
     * @author Rafael San José Tovar <[email protected]>
193
     *
194
     * @param string $key
195
     *
196
     * @return mixed
197
     */
198
    public static function getFlashNow(string $key)
199
    {
200
        return self::getSegment()->getFlash($key);
201
    }
202
203
    /**
204
     * Establece un valor a una clave 'Flash'
205
     *
206
     * @author Rafael San José Tovar <[email protected]>
207
     *
208
     * @param string $key
209
     * @param        $value
210
     */
211
    public static function setFlashNow(string $key, $value)
212
    {
213
        self::getSegment()->setFlashNow($key, $value);
214
    }
215
216
    /**
217
     * Obtiene los datos de una clave 'flash' para la próxima sesión
218
     *
219
     * @author Rafael San José Tovar <[email protected]>
220
     *
221
     * @param string $key
222
     *
223
     * @return mixed
224
     */
225
    public static function getFlashNext(string $key)
226
    {
227
        return self::getSegment()->getFlashNext($key);
228
    }
229
}
230