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

Operatorability::operator()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 3
nop 0
dl 0
loc 20
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace HexMakina\kadro\Auth;
4
5
use HexMakina\BlackBox\Auth\OperatorInterface;
6
use HexMakina\BlackBox\ORM\ModelInterface;
7
8
trait Operatorability
9
{
10
    private $operator = null;
11
12
  // auto build an operator (once) then returns it
13
  // throws Exception if unable to build due to missing required property
14
    abstract public function get($prop_name);
15
    abstract public function extract(ModelInterface $extract_model, $ignore_nullable = false);
16
17
    public function set_operator(OperatorInterface $setter)
18
    {
19
        $this->operator = $setter;
20
    }
21
22
    public function load_operator($id = null)
23
    {
24
        if (!is_null($operator_id = $id ?? $this->get('operator_id'))) { // extraction failed but we have an fk
25
            $this->operator = Operator::exists($operator_id);
26
        }
27
    }
28
29
    public function operator(): ?OperatorInterface
30
    {
31
        if (is_null($this->operator)) {
32
            $extract_attempt = $this->extract(new Operator(), true);
33
            if (!is_null($extract_attempt)) {
34
                foreach (['permission_names', 'permission_ids'] as $permission_marker) {
35
                    if (property_exists($this, $permission_marker)) {
36
                        $extract_attempt->set($permission_marker, $this->$permission_marker);
37
                    }
38
                }
39
40
                $this->operator = $extract_attempt;
41
            }
42
          // elseif(!is_null($this->get('operator_id'))) // extraction failed but we have an fk
43
          // {
44
          //   $this->operator = Operator::exists($this->get('operator_id'));
45
          // }
46
        }
47
48
        return $this->operator;
49
    }
50
51
    public static function enhance_query_retrieve($Query, $filters, $options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
    public static function enhance_query_retrieve($Query, $filters, /** @scrutinizer ignore-unused */ $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        $Query->autoJoin([ACL::table(),'ACL'], null, 'LEFT OUTER');
54
        $permission_alias = $Query->autoJoin([Permission::table(), 'permission'], null, 'LEFT OUTER');
55
56
        $permission_ids_and_names = [];
57
        $permission_ids_and_names [] = sprintf('GROUP_CONCAT(DISTINCT %s.%s) as %s', $permission_alias, 'id', $permission_alias . '_ids');
58
        $permission_ids_and_names [] = sprintf('GROUP_CONCAT(DISTINCT %s.%s) as %s', $permission_alias, 'name', $permission_alias . '_names');
59
        $Query->selectAlso($permission_ids_and_names);
60
61
        $Query->selectAlso(['operator.name as operator_name', 'operator.active as operator_active']);
62
63
        if (isset($filters['username'])) {
64
            $Query->whereEQ('username', $filters['username'], 'operator');
65
        }
66
67
        if (isset($filters['email'])) {
68
            $Query->whereEQ('email', $filters['email'], 'operator');
69
        }
70
71
        if (isset($filters['active'])) {
72
            $Query->whereEQ('active', $filters['active'], 'operator');
73
        }
74
75
        return $Query;
76
    }
77
78
    public function isActive(): bool
79
    {
80
        return is_null($this->operator()) ? false : $this->operator()->isActive();
81
    }
82
83
    public function operatorId()
84
    {
85
        return is_null($this->operator()) ? null : $this->operator()->operatorId();
86
    }
87
88
    public function username()
89
    {
90
        return is_null($this->operator()) ? null : $this->operator()->username();
91
    }
92
93
    public function password()
94
    {
95
        return is_null($this->operator()) ? null : $this->operator()->password();
96
    }
97
98
    public function passwordChange($string)
99
    {
100
        $this->operator()->passwordChange($string);
101
    }
102
103
    public function passwordVerify($string): bool
104
    {
105
        return $this->operator()->passwordVerify($string);
106
    }
107
108
    public function name()
109
    {
110
        return is_null($this->operator()) ? null : $this->operator()->name();
111
    }
112
113
    public function email()
114
    {
115
        return is_null($this->operator()) ? null : $this->operator()->email();
116
    }
117
118
    public function phone()
119
    {
120
        return is_null($this->operator()) ? null : $this->operator()->phone();
121
    }
122
123
    public function languageCode()
124
    {
125
        return is_null($this->operator()) ? null : $this->operator()->languageCode();
126
    }
127
128
    public function hasPermission($p): bool
129
    {
130
        return is_null($this->operator()) ? false : $this->operator()->hasPermission($p);
131
    }
132
}
133