HasOperator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 60
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A enhance_query_retrieve() 0 25 4
B operator() 0 24 8
1
<?php
2
3
namespace HexMakina\kadro\Auth;
4
5
use HexMakina\BlackBox\Auth\OperatorInterface;
6
use HexMakina\BlackBox\ORM\ModelInterface;
7
use HexMakina\Crudites\Queries\AutoJoin;
8
9
trait HasOperator
10
{
11
    use \HexMakina\TightORM\HasOne;
12
13
    private $operator = null;
14
15
    abstract public function get($prop_name);
16
17
    public function operator(OperatorInterface $setter = null)
18
    {
19
        if (!is_null($setter)) {
20
            $this->operator = $setter;
21
        }
22
23
        if (is_null($this->operator)) {
24
            $extract_attempt = self::extract(new Operator(), $this, true);
0 ignored issues
show
Bug introduced by
$this of type HexMakina\kadro\Auth\HasOperator is incompatible with the type HexMakina\BlackBox\ORM\ModelInterface expected by parameter $from_model of HexMakina\kadro\Auth\HasOperator::extract(). ( Ignorable by Annotation )

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

24
            $extract_attempt = self::extract(new Operator(), /** @scrutinizer ignore-type */ $this, true);
Loading history...
25
            if (!is_null($extract_attempt)) {
26
                foreach (['permission_names', 'permission_ids'] as $permission_marker) {
27
                    if (property_exists($this, $permission_marker)) {
28
                        $extract_attempt->set($permission_marker, $this->$permission_marker);
29
                    }
30
                }
31
32
                $this->operator = $extract_attempt;
33
            }
34
        }
35
36
        if (is_null($this->operator) && !empty($this->get('operator_id'))) {
37
            $this->operator = Operator::exists($this->get('operator_id'));
38
        }
39
40
        return $this->operator;
41
    }
42
43
44
    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

44
    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...
45
    {
46
        AutoJoin::join($Query,[ACL::table(),'ACL'], null, 'LEFT OUTER');
47
        $permission_alias = AutoJoin::join($Query,[Permission::table(), 'permission'], null, 'LEFT OUTER');
48
49
        $permission_ids_and_names = [];
50
        $permission_ids_and_names [] = sprintf('GROUP_CONCAT(DISTINCT %s.%s) as %s', $permission_alias, 'id', $permission_alias . '_ids');
51
        $permission_ids_and_names [] = sprintf('GROUP_CONCAT(DISTINCT %s.%s) as %s', $permission_alias, 'name', $permission_alias . '_names');
52
        $Query->selectAlso($permission_ids_and_names);
53
54
        $Query->selectAlso(['operator.name as operator_name', 'operator.active as operator_active']);
55
56
        if (isset($filters['username'])) {
57
            $Query->whereEQ('username', $filters['username'], 'operator');
58
        }
59
60
        if (isset($filters['email'])) {
61
            $Query->whereEQ('email', $filters['email'], 'operator');
62
        }
63
64
        if (isset($filters['active'])) {
65
            $Query->whereEQ('active', $filters['active'], 'operator');
66
        }
67
68
        return $Query;
69
    }
70
}
71