LoginHandler::handle()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 10
nc 8
nop 1
dl 0
loc 16
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
namespace App\Handlers;
4
5
use Pin\Libs\Session;
6
7
/**
8
 * LoginHandler
9
 * Handler para la autenticación
10
 */
11
class LoginHandler extends Handler
12
{
13
    public function handle(array $params = [])
14
    {
15
        $action = $_SERVER['REQUEST_METHOD'];
16
17
        if ($action === 'POST') {
18
            if (isset($params[0]) && $params[0] === 'signin') {
19
                $this->signin();
20
            } elseif (isset($params[0]) && $params[0] === 'signout') {
21
                $this->signout();
22
            }
23
        }
24
25
        if ($action === 'GET') {
26
            Session::set("is_logged_in", true);
27
            Session::set("flash", "Bienvenido!");
28
            $this->redirect("");
29
        }
30
    }
31
32
    
33
34
    private function signout()
35
    {
36
        Session::destroy();
37
        Session::set("flash", "Adios!");
38
        $this->redirect("");
39
    }
40
41
    private function signin()
42
    {
43
        Session::set("is_logged_in", true);
44
        Session::set("flash", "Bienvenido!");
45
        $this->redirect("");
46
    }
47
48
    private function redirect($path)
49
    {
50
        $url = PUBLIC_PATH . $path;
51
        header("Location: $url", true, 301);
52
    }
53
}
54