Passed
Push — main ( 578b49...caeba9 )
by Sammy
18:38 queued 16:08
created

Kadro   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 77
rs 10
c 2
b 0
f 0
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A trim_request_data() 0 12 2
B authorize() 0 16 7
A requiresOperator() 0 3 1
A operator() 0 10 2
A l() 0 3 1
A execute() 0 5 1
A __toString() 0 3 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
        if(is_null($this->operator)){
28
          $op_id = $this->get('HexMakina\BlackBox\StateAgentInterface')->operatorId();
29
          $op_class = get_class($this->get('HexMakina\BlackBox\Auth\OperatorInterface'));
30
          $op = $op_class::safeLoading($op_id);
31
32
          $this->operator = $op;
33
        }
34
        return $this->operator;
35
    }
36
37
    // returns true or throws AccessRefusedException
38
    public function authorize($permission = null): bool
39
    {
40
        if (!$this->requiresOperator()) {
41
            return true;
42
        }
43
44
        $operator = $this->operator();
45
        if (is_null($operator) || $operator->isNew() || !$operator->isActive()) {
46
            throw new AccessRefusedException();
47
        }
48
49
        if (!is_null($permission) && !$operator->hasPermission($permission)) {
50
            throw new AccessRefusedException();
51
        }
52
53
        return true;
54
    }
55
56
    public function execute($method)
57
    {
58
      // kadro controller is a display controller with authentification and intl
59
        $this->authorize();
60
        return parent::execute($method);
61
    }
62
63
    public function prepare()
64
    {
65
        parent::prepare();
66
        $this->trim_request_data();
67
    }
68
69
    // intl function, calls to lezer
70
    public function l($message, $context = []): string
71
    {
72
        return call_user_func($this->translation_function_name, $message, $context);
73
    }
74
75
    private function trim_request_data()
76
    {
77
        array_walk_recursive($_GET, function (&$value) {
78
            $value = trim($value);
79
        });
80
        array_walk_recursive($_REQUEST, function (&$value) {
81
            $value = trim($value);
82
        });
83
84
        if ($this->router()->submits()) {
85
            array_walk_recursive($_POST, function (&$value) {
86
                $value = trim($value);
87
            });
88
        }
89
    }
90
}
91