Kadro   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 30
c 3
b 1
f 0
dl 0
loc 75
rs 10
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A requiresOperator() 0 3 1
A __toString() 0 3 1
A trim_request_data() 0 12 2
B authorize() 0 16 7
A operator() 0 8 3
A l() 0 3 1
A execute() 0 5 1
A prepare() 0 4 1
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