RevisarSesion::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace src;
3
4
use src\ValidarSesionIdInterface;
5
6
use const SESSION_ID;
7
use const SESSION_CREACION;
8
use const SESSION_CADUCIDAD;
9
10
/**
11
 * La clase devuelve TRUE si la sesión es valida, false si es invalida
12
 */
13
14
class RevisarSesion
15
{
16
    private $_validarId;
17
18
    public function __construct(ValidarSesionIdInterface $validarSesionId)
19
    {
20
        $this->_validarId = $validarSesionId;
21
    }
22
23
    public function revisar(): bool
24
    {
25
        return $this->existeId();
26
    }
27
28
    private function existeId(): bool
29
    {
30
        if (!isset($_SESSION[SESSION_ID])) {
31
            return false;
32
        }
33
34
        return $this->idEstaVacia();
35
    }
36
37
    private function idEstaVacia(): bool
38
    {
39
        if (empty($_SESSION[SESSION_ID])) {
40
            return false;
41
        }
42
43
        return $this->idEsValido();
44
    }
45
46
    private function idEsValido(): bool
47
    {
48
        if (!$this->_validarId->validar($_SESSION[SESSION_ID])) {
49
            return false;
50
        }
51
52
        return $this->caduco();
53
    }
54
    
55
    private function caduco(): bool
56
    {
57
        if ((time() - $_SESSION[SESSION_CREACION]) > $_SESSION[SESSION_CADUCIDAD]) {
58
            return false;
59
        }
60
61
        return true;
62
    }
63
}
64