Passed
Branch main (166306)
by Nelson
03:35
created

PageHandler::handle()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
nc 5
nop 1
dl 0
loc 24
rs 9.5222
c 1
b 0
f 0
1
<?php
2
3
namespace App\Handlers;
4
5
use App\Libs\Template;
6
use App\Libs\Session;
7
8
/**
9
 * PageHandler
10
 * Handler para la gestión de páginas
11
 */
12
class PageHandler extends Handler
13
{
14
    public function handle(array $params = [])
15
    {
16
        // Validar autenticación
17
        if (Session::get("is_logged_in") !== true) {
18
            Session::set("flash", "Debe iniciar sesión para acceder al recurso *<i>page</i>*");
19
            $this->redirect("");
20
            return;
21
        }
22
23
        // Determinar la acción basada en el método HTTP y parámetros
24
        $action = $_SERVER['REQUEST_METHOD'];
25
        $slug = $params[0] ?? '';
26
27
        if ($action === 'GET') {
28
            if (isset($params[0])) {
29
                // GET /page/show/{slug}
30
                $this->show($slug);
31
            } else {
32
                // GET /page/edit
33
                $this->edit();
34
            }
35
        } elseif ($action === 'POST') {
36
            // POST /page/update
37
            $this->update();
38
        }
39
    }
40
41
    private function show($slug = '')
42
    {
43
        $template = new Template();
44
        $template->set("slug", $slug);
45
        $template->render("page/show");
46
    }
47
48
    private function edit()
49
    {
50
        $template = new Template();
51
        $template->render("page/edit");	
52
    }
53
54
    private function update()
55
    {
56
        $template = new Template();
57
        Session::set("flash", "Página actualizada correctamente");
58
        $template->render("page/edit");	
59
    }
60
61
    private function redirect($path)
62
    {
63
        $url = PUBLIC_PATH . $path;
64
        header("Location: $url", true, 301);
65
    }
66
}
67