Kadro::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace HexMakina\kadro\Controllers;
4
5
use HexMakina\kadro\Auth\AccessRefusedException;
6
use HexMakina\BlackBox\Auth\OperatorInterface;
7
use HexMakina\BlackBox\Controllers\AuthControllerInterface;
8
use HexMakina\BlackBox\Controllers\IntlControllerInterface;
9
10
class Kadro extends Display implements AuthControllerInterface, IntlControllerInterface
11
{
12
    private $translation_function_name = 'L';
13
    private $operator = null;
14
15
    public function __toString()
16
    {
17
        return get_called_class();
18
    }
19
20
    public function requiresOperator(): bool
21
    {
22
        return true; // security by default
23
    }
24
25
    public function operator(): OperatorInterface
26
    {
27
        $op_class = get_class($this->get('HexMakina\BlackBox\Auth\OperatorInterface'));
28
        if (is_null($this->operator) && !empty($op_id = $this->get('HexMakina\BlackBox\StateAgentInterface')->operatorId())) {
29
            $op = $op_class::safeLoading($op_id);
30
            $this->operator = $op;
31
        }
32
        return $this->operator ?? new $op_class;
33
    }
34
35
    // returns true or throws AccessRefusedException
36
    public function authorize($permission = null): bool
37
    {
38
        if (!$this->requiresOperator()) {
39
            return true;
40
        }
41
42
        $operator = $this->operator();
43
        if (is_null($operator) || $operator->isNew() || !$operator->isActive()) {
44
            throw new AccessRefusedException();
45
        }
46
47
        if (!is_null($permission) && !$operator->hasPermission($permission)) {
48
            throw new AccessRefusedException();
49
        }
50
51
        return true;
52
    }
53
54
    public function execute($method)
55
    {
56
      // kadro controller is a display controller with authentification and intl
57
        $this->authorize();
58
        return parent::execute($method);
59
    }
60
61
    public function prepare()
62
    {
63
        parent::prepare();
64
        $this->trim_request_data();
65
    }
66
67
    // intl function, calls to lezer
68
    public function l($message, $context = []): string
69
    {
70
        return call_user_func($this->translation_function_name, $message, $context);
71
    }
72
73
    private function trim_request_data()
74
    {
75
        array_walk_recursive($_GET, function (&$value) {
76
            $value = trim($value);
77
        });
78
        array_walk_recursive($_REQUEST, function (&$value) {
79
            $value = trim($value);
80
        });
81
82
        if ($this->router()->submits()) {
83
            array_walk_recursive($_POST, function (&$value) {
84
                $value = trim($value);
85
            });
86
        }
87
    }
88
}
89